docker 中的 Django 迁移权限被拒绝

Permission denied with django migrations in docker

我正在使用 docker compose with django - postgres - rabbitmq

一切顺利,即使 django 容器已经启动 运行 我想要的点 makemigrations 它会随着 django-celery-beat

停止
Migrations for 'django_celery_beat':
django_1        |   /usr/local/lib/python2.7/site-packages/django_celery_beat/migrations/0005_auto_20190512_1531.py
django_1        |     - Alter field event on solarschedule
django_1        | Traceback (most recent call last):
django_1        |   File "/app/manage.py", line 10, in <module>
django_1        |     execute_from_command_line(sys.argv)
django_1        |   File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
django_1        |     utility.execute()
django_1        |   File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
django_1        |     self.fetch_command(subcommand).run_from_argv(self.argv)
django_1        |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
django_1        |     self.execute(*args, **cmd_options)
django_1        |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
django_1        |     output = self.handle(*args, **options)
django_1        |   File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 193, in handle
django_1        |     self.write_migration_files(changes)
django_1        |   File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 232, in write_migration_files
django_1        |     with io.open(writer.path, "w", encoding='utf-8') as fh:
django_1        | IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/site-packages/django_celery_beat/migrations/0005_auto_20190512_1531.py

虽然用户模型是在迁移时写下来的,它应该是从 AbstractUser 在 django 本身继承的,但那里没有引发权限错误

django_1        | Migrations for 'users':
django_1        |   users/migrations/0001_initial.py
django_1        |     - Create model UserAccount
django_1        |     - Create model Branch
django_1        |     - Create model Organization
django_1        |     - Add field organization to branch
django_1        |     - Add field bills_groups to useraccount
django_1        |     - Add field branch to useraccount
django_1        |     - Add field groups to useraccount
django_1        |     - Add field user_permissions to useraccount

所以我尝试将 sudo 用户添加到 python-2.7 alpine,但实际上我未能 运行 使用 sudo 权限执行此命令

那么我该怎么办,我应该 运行 在 docker 中使用 virtualenv 图像,还是有任何调整来修复这个

Docker 文件

FROM python:2.7-alpine

ENV PYTHONUNBUFFERED 1
RUN addgroup -S django \
    && adduser -S -G django django

RUN apk update \
  && apk add sudo \
  # psycopg2 dependencies
  && apk add --virtual build-deps gcc python-dev musl-dev \
  && apk add postgresql-dev \
  # CFFI dependencies
  && apk add libffi-dev py-cffi \
  # Translations dependencies
  && apk add gettext \
  # https://docs.djangoproject.com/en/dev/ref/django-admin/#dbshell
  && apk add postgresql-client

# Requirements are installed here to ensure they will be cached.
COPY ./requirements.txt /requirements.txt
RUN pip install --no-cache-dir --default-timeout=100 -r /requirements.txt \
        && rm -rf /requirements

COPY ./compose/testing/django/entrypoint /entrypoint
RUN sed -i 's/\r//' /entrypoint
RUN chmod +x /entrypoint
RUN chown django /entrypoint

COPY ./compose/testing/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"]

docker-compose.yml

version: '3'

volumes:
  production_postgres_data: {}
  production_postgres_data_backups: {}

services:
  django: &django
    build:
      context: .
      dockerfile: ./compose/testing/django/Dockerfile
    image: panel_testing_django
    depends_on:
      - postgres
      - rabbitmq
    env_file:
      - ./.envs/.production/.django
      - ./.envs/.production/.postgres
    command: /start
    ports:
      - "8000"
    expose:
      - "8000"

  postgres:
    build:
      context: .
      dockerfile: ./compose/staging/postgres/Dockerfile
    image: panel_testing_postgres
    env_file:
      - ./.envs/.production/.postgres
    volumes:
      - production_postgres_data:/var/lib/postgresql/data
      - production_postgres_data_backups:/backups
    ports:
     - "5432"

  rabbitmq:
    hostname: rabbit
    image: rabbitmq:3-management
    environment:
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=mypass
    ports:
      - "5672:5672"  
      - "15672:15672"

  celeryworker:
    <<: *django
    image: panel_testing_celeryworker
    command: /start-celeryworker

  celerybeat:
    <<: *django
    image: panel_testing_celerybeat
    command: /start-celerybeat

  flower:
    <<: *django
    image: panel_testing_flower
    ports:
      - "5555:5555"
    command: /start-flower

今天遇到同样的问题。我使用 docker 创建了一个项目,当第一次尝试 运行 makemigrations 时,出现了这个错误:

PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.7/site-packages/django/contrib/admin/migrations/0004_auto_20190515_1649.py'

我通过 运行 宁 makemigrations 解决了它指定应用程序名称(核心是应用程序名称),如下所示:

docker-compose run web_app sh -c "python manage.py makemigrations core"

我刚刚在 docker 中使用了 virtualenv 并将对环境 site-packages dir

的所有权授予我的用户
RUN pip install virtualenv && virtualenv -p python /app/venv
RUN /app/venv/bin/pip install -r req.txt
RUN /app/venv/bin/python /app/code/manage.py makemigrations

完成

我在 Linux,在我的情况下,这只是转到存储迁移的文件夹并为文件夹本身提供足够权限的情况。

例如:

chmod . 666