我修改了 django-cookiecutter 默认生产模板,使caddy web 服务器提供静态文件。我正在使用卷通过主机./static
目录映射django和caddy./static
容器中的目录,但是当 dockerpython manage.py collectstatic --noinput
在尝试创建./static
.
但是,如果我不切换到django
django 容器的 Dockerfile 中的用户,因此collectstatic
以 root 身份执行,一切正常。我猜django
容器中的用户不允许写入主机目录,即使chown -R django /app/static
已成功执行。
回溯(最后一次调用):
文件“/app/manage.py”,第 30 行,在 <\module>
execute_from_command_line(sys.argv)
...
文件“/usr/local/lib/python3.6/site- packages/collectfast/management/commands/collectstatic.py”,第 111 行,在
copy_file self.do_copy_file(args)
文件“/usr/local/lib/python3.6/site-packages/collectfast/management/commands/collectstatic.py ",第 100 行,在 do_copy_file
路径中,prefixed_path,source_storage)
文件 "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",第 354 行,在 copy_file
self.storage.save(prefixed_path, source_file)
文件“/usr/local/lib/python3.6/site-packages/django/core/files/storage.py”,第 49 行,保存
返回 self._save(name, content)
文件“/usr/local/lib /python3.6/site-packages/django/core/files/storage.py”,第 236 行,在 _save
os.makedirs(目录)
文件“/usr/local/lib/python3.6/os.py”,行220,在 makedirs
mkdir(name, mode)
PermissionError:
[Errno 13] Permission denied: '/app/static/sass'
我尝试在主机内部,以 root 身份预先在主机chown -R systemd-timesync:root static
内部创建文件夹,并添加到 django 容器的 Dockerfile(以容器的 root 用户身份执行)。./static
RUN mkdir /app/static && chown -R django /app/static
码头工人-compose.yml
version: '3'
volumes:
production_postgres_data: {}
production_postgres_data_backups: {}
production_caddy: {}
services:
django:
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
volumes:
- ./static:/app/static
depends_on:
- postgres
- redis
env_file:
- ./.envs/.production/.django
- ./.envs/.production/.postgres
command: /start
postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
volumes:
- production_postgres_data:/var/lib/postgresql/data
- production_postgres_data_backups:/backups
env_file:
- ./.envs/.production/.postgres
caddy:
build:
context: .
dockerfile: ./compose/production/caddy/Dockerfile
depends_on:
- django
volumes:
- production_caddy:/root/.caddy
- ./static:/srv/static
env_file:
- ./.envs/.production/.caddy
ports:
- "0.0.0.0:80:80"
- "0.0.0.0:443:443"
redis:
image: redis:3.2
django 容器 Dockerfile
FROM nickgryg/alpine-pandas
ENV PYTHONUNBUFFERED 1
RUN apk update \
# psycopg2 dependencies
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev \
# Pillow dependencies
&& apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
# CFFI dependencies
&& apk add libffi-dev py-cffi \
# lxml dependencies
&& apk add libxml2-dev libxslt-dev
RUN addgroup -S django \
&& adduser -S -G django django
# Requirements are installed here to ensure they will be cached.
COPY ./requirements /requirements
RUN pip install --no-cache-dir -r /requirements/production.txt \
&& rm -rf /requirements
COPY ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r//' /entrypoint
RUN chmod +x /entrypoint
RUN chown django /entrypoint
COPY ./compose/production/django/start /start
RUN sed -i 's/\r//' /start
RUN chmod +x /start
RUN chown django /start
COPY . /app
RUN chown -R django /app
USER django
WORKDIR /app
ENTRYPOINT ["/entrypoint"]
django容器启动脚本
#!/bin/sh
set -o errexit
set -o pipefail
set -o nounset
python /app/manage.py collectstatic --noinput
/usr/local/bin/gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app
我不希望我的容器以 root 身份执行,所以我正在寻找任何解决方案/想法。