I'm using docker-compose to start my Django application. I am having problems keeping the data volumes between development and test separated. The development version of the code looks like this, and has data stored in Postgres, Elasticsearch, and Redis:
version: '3'
services:
db:
image: postgres:9.6.6
ports:
- "5435:5432"
environment:
- POSTGRES_PASSWORD=example
volumes:
- pg-data:/var/lib/postgresql/data
redis:
image: redis:3.2.6
volumes:
- redis-data:/data
redis_cache:
image: redis:3.2.6
elasticsearch:
image: elasticsearch:5.6.6
volumes:
- elasticsearch-data:/usr/share/elasticsearch/data
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgresql://postgres:example@db/postgres
- ENVIRONMENT=development
- REDIS_URL=redis://redis:6379
- REDIS_CACHE_URL=redis://redis_cache:6379
- ELASTIC_ENDPOINT=elasticsearch:9200
env_file: docker.env
depends_on:
- db
- redis
- redis_cache
- elasticsearch
volumes:
- .:/code
volumes:
pg-data: {}
redis-data: {}
elasticsearch-data: {}
I created another version of this document for testing the app, which is called docker-compose.test.yml. Each time I start this version of the app I want to use a fresh copy of Postgres, Elasticsearch, etc. But it does not work that way. The goal is to populate some data during the test, then have it all removed on shutdown.
I thought I could simply start services without a named volume to accomplish this. Like so:
db:
image: postgres:9.6.6
environment:
- POSTGRES_PASSWORD=example
Then I ran the test with:
docker-compose -f docker-compose.test.yml run --rm web python manage.py test --verbosity=2
But what I noticed is the tests are referring to the same data that is the named volume pg-data. So I do not have a fresh copy of the database. I did not expect that behavior. I can name the volumes something different in the test version of docker-compose, like pg-test-data, but then they will persist after running the tests and need to be manually removed.
So what is the proper solution for an issue like this?