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

Viewed 7329

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?

3 Answers

Leaving a compose file here. I am persisting the DB, media and elastic search data. This should be useful for those who have a hard time figuring where to place what the current answers here give.

version: '3.3'

services:
  # elastic search service
  elasticsearch:
    image: elasticsearch:6.5.4
    ports:
      - '9400:9200'
    volumes:
      - elastic-data:/usr/share/elasticsearch/data
  # Postgres database service
  postgres:
    image: postgres:9.6-alpine
    environment:
      POSTGRES_DB: dilirena
      POSTGRES_USER: dilirenauser
      POSTGRES_PASSWORD: pa55w0rd
    ports:
      - '5435:5432'
    volumes:
      - postgres-data:/var/lib/postgresql/data

  # drf restful api service
  api:
    build: .
    command: python manage.py runserver 0.0.0.0:8081
    volumes:
      - .:/app
      - media:<media-path>
    ports:
      - '8081:8081'
    env_file:
      - ./.env
    links:
      - postgres
      - elasticsearch

    depends_on:
      - postgres
      - elasticsearch

volumes:
  elastic-data:
  postgres-data:
  media:

I am on mac, not sure if it makes a difference.

Related