我是新来的docker,我想dockerize我的django应用程序生产。我的操作系统:Ubuntu 20.04.1 LTS,Docker版本20.10.5,内部版本55c4c88,我的Docker-compose. yml文件如下所示:
version: '3.1'
services:
nginx-proxy:
image: jwilder/nginx-proxy
restart: "always"
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./nginx/vhost/:/etc/nginx/vhost.d:ro
- ./nginx/conf.d/client_max_body_size.conf:/etc/nginx/conf.d/client_max_body_size.conf:ro
- ./static/:/amd/static
- ./media/:/amd/media
postgres:
image: postgres:9.6.6
restart: always
volumes:
- ./pgdb/:/var/lib/postgresql/
ports:
- "5432:5432"
env_file: ./.env
redis:
image: redis
ports:
- 6379:6379
restart: always
web:
container_name: amd
build: .
restart: "always"
ports:
- "8000:8000"
volumes:
- .:/code/
# - ./static/:/code/static
# - ./media/:/code/media
depends_on:
- "postgres"
env_file: .env
celery:
build:
context: .
dockerfile: celery.dockerfile
volumes:
- .:/code
command: celery -A amdtelecom worker -l info
links:
- redis
- postgres
depends_on:
- "redis"
- "postgres"
env_file: ./.env
networks:
default:
external:
name: nginx-proxy
我的Dockerfile如下所示:
FROM python:3.7
ENV PYTHONUNBUFFERED 1
ENV DEBUG False
COPY requirements.txt /code/requirements.txt
WORKDIR /code
RUN pip install -r requirements.txt
ADD . .
CMD [ "gunicorn", "--bind", "0.0.0.0", "-p", "8000", "amdtelecom.wsgi" ]
在我项目设置文件中:
CORS_ALLOWED_ORIGINS = [
"http://localhost:8000",
"http://127.0.0.1:8000",
"http://localhost"
]
STATIC_URL = '/static/'
if PROD:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
SITE_URL = 'http://localhost:80'
我在终端中使用以下步骤:
- docker-compose up-d--生成
1.多克PS-A
1.码头执行官-这是我的gunicorn id狂欢
1../www.example.com迁移 manage.py migrate
1../www.example.com收集静态 manage.py collectstatic
1../www.example.com加载数据数据. json manage.py loaddata data.json
并打开尝试在Chrome浏览器的urllocalhost中打开项目,项目打开时没有静态文件(没有js,没有css)。
在我的Docker日志中:
> (docker-compose logs -f):
2021/03/25 10:51:32 [error] 51#51: *18 open() "/amd/static/images/fashion/product/13.jpg" failed (13: Permission denied), client: 172.23.0.1, server: localhost, request:
"GET /static/images/fashion/product/13.jpg HTTP/1.1", host: "localhost", referrer: "http://localhost/"
,我需要注意一下我的代码-此应用程序在Mac OS中已经运行良好,静态文件打开良好。我认为我的问题与Linux操作系统有关。
我怎样才能解决这个问题?
提前感谢。有关我的代码的其他信息,请访问github repo:
1条答案
按热度按时间fnx2tebb1#
这个问题可能存在于权限中,更确切地说,存在于文件所有者中--您可以在静态(或媒体)卷中的所有文件上使用
chmod 777
轻松地解决这个问题(但不太安全)。第二个选项是向容器用户显示,如此处所述https://stackoverflow.com/a/66910717/7113416或可能更好的https://stackoverflow.com/a/56904335/7113416