我正在使用 docker compose 和 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
虽然用户模型是在迁移时写下来的,它应该是从 django 本身的 AbstractUser
继承的,但那里没有引发权限错误
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"
关于python - docker 中的 django 迁移权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100860/