我正在使用Docker Compose与Django、PostgreSQL和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
我试图在 Python-2.7 Alpine 中添加 sudo 用户,但实际上我没有成功以 sudo 权限运行此命令。
那么我该怎么办? 我应该在 Docker 中使用 virtualenv 运行镜像,还是有其他方法可以修复这个问题?
Dockerfile
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
Dockerfile
和docker-compose
文件吗? - JPG/usr/local/lib
中的文件上写入,这个文件可能是由root拥有的,但是Dockerfile切换到了django用户,因此它不再具有对该文件的写入权限。为什么将这些迁移文件存储在/usr/local/lib
中?这些应该存储在您具有写入访问权限的地方,例如/app
文件夹。 - solarc