django 如何为Docker中的用户分配中的权限?

lyfkaqu1  于 2023-01-10  发布在  Go
关注(0)|答案(2)|浏览(130)

我正尝试在Docker中运行我的DjangoRestFramework项目。这是我的部署脚本,我正尝试在部署到远程计算机之前检查我的本地计算机。我能够使用
第一个月
但是当我尝试运行docker-compose -f docker-compose-deploy.yml up时,我得到了一个权限错误:[Errno 13]权限被拒绝。以下是详细信息
操作系统: windows 10
Dockerfile

FROM python:3.9.13-alpine3.16
LABEL maintainer=""

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
COPY ./app /app
COPY ./scripts /scripts

WORKDIR /app
EXPOSE 8000

RUN python -m venv /py && \
  /py/bin/pip install --upgrade pip && \
  apk add --update --no-cache postgresql-client && \
  apk add --update --no-cache --virtual .tmp-deps \
      build-base postgresql-dev musl-dev linux-headers && \
  /py/bin/pip install -r /requirements.txt && \
  apk del .tmp-deps && \
  adduser --disabled-password --no-create-home app && \
  mkdir -p /vol/web/static && \
  mkdir -p /vol/web/media && \
  chown -R app:app /vol && \    
  chmod -R 755 /vol && \
  chmod -R +x /scripts

ENV PATH="/scripts:/py/bin:$PATH"

USER app

CMD ["run.sh"]

这是我的docker-compose.yml文件

version: "3.9"

services:
  app:
    build:
      context: .
    restart: always
    volumes:
      - static-data:/vol/web
    environment:
      - DB_HOST=db
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASS=${DB_PASS}
      - SECRET_KEY=${SECRET_KEY}
      - ALLOWED_HOSTS=${ALLOWED_HOSTS}
    depends_on:
      - db

  db:
    image: postgres:13-alpine
    restart: always
    volumes:
      - postgres-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=${DB_NAME}
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASS}

  proxy:
    build:
      context: ./proxy
    restart: always
    depends_on:
      - app
    ports:
      - 80:8000
    volumes:
      - static-data:/vol/static

volumes:
  postgres-data:
  static-data:

这是错误的堆栈跟踪

app_1    | Traceback (most recent call last):
app_1    |   File "/app/manage.py", line 22, in <module>
app_1    |     main()
app_1    |   File "/app/manage.py", line 18, in main
app_1    |     execute_from_command_line(sys.argv)
app_1    |   File "/py/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
app_1    |     utility.execute()
app_1    |   File "/py/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
app_1    |     self.fetch_command(subcommand).run_from_argv(self.argv)
app_1    |   File "/py/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
app_1    |     self.execute(*args, **cmd_options)
app_1    |   File "/py/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
app_1    |     output = self.handle(*args, **options)
app_1    |   File "/py/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
app_1    |     collected = self.collect()
app_1    |   File "/py/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
app_1    |     handler(path, prefixed_path, storage)
app_1    |   File "/py/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 348, in copy_file
app_1    |     self.storage.save(prefixed_path, source_file)
app_1    |   File "/py/lib/python3.9/site-packages/django/core/files/storage.py", line 54, in save
app_1    |     name = self._save(name, content)
app_1    |   File "/py/lib/python3.9/site-packages/django/core/files/storage.py", line 260, in _save
app_1    |     os.makedirs(directory, exist_ok=True)
app_1    |   File "/usr/local/lib/python3.9/os.py", line 215, in makedirs
app_1    |     makedirs(head, exist_ok=exist_ok)
app_1    |   File "/usr/local/lib/python3.9/os.py", line 215, in makedirs
app_1    |     makedirs(head, exist_ok=exist_ok)
app_1    |   File "/usr/local/lib/python3.9/os.py", line 215, in makedirs
app_1    |     makedirs(head, exist_ok=exist_ok)
app_1    |   [Previous line repeated 1 more time]
app_1    |   File "/usr/local/lib/python3.9/os.py", line 225, in makedirs
app_1    |     mkdir(name, mode)
app_1    | PermissionError: [Errno 13] Permission denied: '/app/vol'

在我的Dockerfile中,我已经特别为/vol的用户应用程序提供了权限。我遗漏了什么?我该如何解决这个问题?

aoyhnmkz

aoyhnmkz1#

试试这个

RUN python -m venv /py && \
    /py/bin/pip install --upgrade pip && \
    apk add --update --no-cache postgresql-client && \
    apk add --update --no-cache --virtual .tmp-deps \
        build-base postgresql-dev musl-dev linux-headers && \
    /py/bin/pip install -r /requirements.txt && \
    apk del .tmp-deps && \
    adduser --disabled-password --no-create-home app && \
    mkdir -p /vol/web/static && \
    mkdir -p /vol/web/media && \
    chown -R app:app /app && \              **This has been added**
    chown -R app:app /vol && \
    chmod -R 755 /vol && \
    chmod -R +x /scripts

ENV PATH="/scripts:/py/bin:$PATH"       

USER app

CMD ["run.sh"]
mzmfm0qo

mzmfm0qo2#

尝试像这样组织你的Dockerfile:

FROM python:3.9.13-alpine3.16
LABEL maintainer="sohail.developes@gmail.com"

ENV PYTHONUNBUFFERED 1

# Create work user
RUN set -x ; \
    addgroup -g 83 -S app ; \
    adduser -u 83 -D -S -G app app && exit 0 ; exit 1 

COPY --chown=app:app ./requirements.txt /requirements.txt
COPY --chown=app:app ./app /app
COPY --chown=app:app ./scripts /scripts

WORKDIR /app
EXPOSE 8000

RUN python -m venv /py && \
  /py/bin/pip install --upgrade pip && \
  apk add --update --no-cache postgresql-client && \
  apk add --update --no-cache --virtual .tmp-deps \
      build-base postgresql-dev musl-dev linux-headers && \
  /py/bin/pip install -r /requirements.txt && \
  apk del .tmp-deps && \
  adduser --disabled-password --no-create-home app && \
  mkdir -p /vol/web/static && \
  mkdir -p /vol/web/media && \
  chown -R app:app /vol && \    
  chmod -R 755 /vol && \
  chmod -R +x /scripts

ENV PATH="/scripts:/py/bin:$PATH"

USER app

CMD ["run.sh"]

开始,您需要在映像中创建一个用户应用程序,并使用--chown选项将文件从主机复制到容器中。我还建议您将Python包的安装与程序的安装分开。

相关问题