如何解决权限被拒绝,无法打开静态文件,Docker,Django
我是 docker 新手,我想将我的 django 应用程序dockerize 到生产环境中。 我的操作系统: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 --build
- docker ps -a
- docker exec -it 'my gunicorn id' bash
- ./manage.py 迁移
- ./manage.py collectstatic
- ./manage.py loaddata data.json
并在 chrome 浏览器中的 url localhost 中打开 try open project 并且项目打开时没有静态文件(没有 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:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。