【问题标题】:Docker is not saving django media files into project 'media' directory on productionDocker 没有将 django 媒体文件保存到项目的“媒体”目录中
【发布时间】:2021-10-09 18:48:10
【问题描述】:

应用说明


我有一个应用程序,后端使用 django-gunicorn,前端使用 reactjs-nginx,所有容器化并托管在 aws ec2 实例上。

问题


在开发环境中,媒体文件被永久保存在“媒体”目录中。但是,这些文件仅在生产时保存在当前正在运行的 docker 容器中。因此,当我重建/停止容器以进行新的代码推送时,这些文件将被删除。

期待


我想将文件存储在“媒体”文件夹中以供永久使用。

重要代码


settings.py

ENV_PATH = Path(__file__).resolve().parent.parent
STATIC_ROOT = BASE_DIR / 'django_static'
STATIC_URL = '/django_static/'

MEDIA_ROOT = BASE_DIR / 'media/'

MEDIA_URL = '/media/'

docker-compose-production.yml

version: "3.3"

services:
    db:
       image: postgres
       restart: always #Prevent postgres from stopping the container
        volumes:
               - ./data/db:/var/lib/postgresql/data
        environment:
                 - POSTGRES_DB=postgres
                 - POSTGRES_USER=postgres
                 - POSTGRES_PASSWORD=postgres
    ports:
        - 5432:5432

nginx:
    restart: unless-stopped
    build:
        context: .
        dockerfile: ./docker/nginx/Dockerfile
    ports:
        - 80:80
        - 443:443
    volumes:
        - static_volume:/code/backend/server/django_static
        - ./docker/nginx/production:/etc/nginx/conf.d
        - ./docker/nginx/certbot/conf:/etc/letsencrypt
        - ./docker/nginx/certbot/www:/var/www/certbot
    depends_on:
        - backend

# Volume for certificate renewal
certbot:
    image: certbot/certbot
    restart: unless-stopped
    volumes:
        - ./docker/nginx/certbot/conf:/etc/letsencrypt
        - ./docker/nginx/certbot/www:/var/www/certbot
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
backend:
    restart: unless-stopped
    build:
        context: .
        dockerfile: ./docker/backend/Dockerfile

    entrypoint: /code/docker/backend/wsgi-entrypoint.sh
    volumes:
        - .:/code
        - static_volume:/code/backend/server/django_static
    expose:
        - 8000
    depends_on:
        -   db

volumes:
       static_volume: { }
       pgdata: { }

【问题讨论】:

    标签: reactjs django docker nginx gunicorn


    【解决方案1】:

    我终于弄清楚了这个问题。我忘记在 docker-compose 文件中将 .:/code 添加到我的 nginx 卷配置中。 Thank to this answer

    更新了 nginx 卷配置

            volumes:
            - .:/code
            - static_volume:/code/backend/server/django_static
            - ./docker/nginx/production:/etc/nginx/conf.d
            - ./docker/nginx/certbot/conf:/etc/letsencrypt
            - ./docker/nginx/certbot/www:/var/www/certbot
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-17
      • 2021-09-11
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多