Search code examples
djangodockerdocker-composecookiecutter-django

Django app with docker-compose keep the data in media volume


I'm using Django cookiecutter with docker and docker-compose.

On production I'm using dj-static to serve my media files.

Whenever I use 'docker-compose down' command everything that is in my media volume gets deleted. I think this is the expected outcome of that command but everything that is in 'postgreSQL' is kept.

How can I do that with the 'media' volume?


Solution

  • I've managed to make this work.

    In the docker-compose.yml I've added:

    volumes:
      media: {}
    
    django:
      ....
      volumes:
        - media:/app/project-name/same-path-as-MEDIA_URL
    

    In the compose/django Dockerfile I've added:

    VOLUME /app/PROJECT_NAME/same-path-as-MEDIA_URL
    

    After this changes I've run docker-compose build, docker-compose up and got a 500 error. To pass this (if you haven't modified django cookiecutter's default settings):

    docker ps - here you'll get your django container id
    
    docker exec -u root THE_CONTAINER_ID chown django:user PROJECT_NAME/same-path-as-MEDIA_URL