权限被拒绝访问docker-compose中的卷

权限被拒绝访问docker-compose中的卷

我已经关注本教程使用 django、gunicorn、nginx 和 postgresql 部署 web 应用程序。

当使用教程中的空应用程序时,一切似乎都正常,但是当我尝试使用我的 Django 应用程序重新创建相同的配置时,尝试收集静态文件时出现“权限被拒绝”的情况。

配置yml为:

version: '3.7'

services:
  postgres:
    image: postgres:11.2-alpine
    volumes:
      - database:/var/lib/postgresql/data
    env_file: .env.pg

  django:
    build: .
    command: gunicorn mysite.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static:/usr/src/django/static
    expose:
      - 8000
    env_file: .env
    depends_on:
      - postgres

  nginx:
    build: ./nginx
    volumes:
      - static:/usr/src/django/static
    ports:
      - 8009:80
    depends_on:
      - django

volumes:
  database:
  static:

我执行时得到的错误docker-compose exec django python manage.py collectstatic --clear --noinput

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 167, in handle
    any(self.storage.listdir(destination_path))
  File "/usr/local/lib/python3.7/site-packages/django/core/files/storage.py", line 315, in listdir
    for entry in os.scandir(path):
PermissionError: [Errno 13] Permission denied: '/usr/src/django/static'

我尝试在容器内执行 shell:“静态”目录位于其应在的位置,但我无法在其中写入任何内容(即使以 root 身份)。postgres 容器中的数据库卷似乎运行良好,因此我认为在两个容器之间共享卷时存在问题。

任何想法?

相关内容