I am setting up a database for running integration tests. In my docker-compose.yml
file I have:
services:
...
postgres:
image: "postgres:16"
volumes:
- "postgres:/var/lib/psql/data"
env_file: ".env"
ports:
- "5432:5432"
volumes:
postgres:
In my docker-compose.dev.yml
I have only this:
services:
postgres:
volumes:
- "test_postgres:/var/lib/psql/data"
volumes:
test_postgres:
The premise is it’s the same container that mounts a different volume if run with docker compose -f docker-compose.yml -f docker-compose.dev.yml up postgres
. However the changes I make on one volume persist on the other. Is this even possible?
Here’s the docker config output:
$ docker compose -f docker-compose.yml -f docker-compose.dev.yml config postgres
name: mercury
services:
postgres:
environment:
POSTGRES_DB: mercury
POSTGRES_PASSWORD: mercury
POSTGRES_USER: mercury
image: postgres:16
networks:
default: null
ports:
- mode: ingress
target: 5432
published: "5432"
protocol: tcp
volumes:
- type: volume
source: test_postgres
target: /var/lib/psql/data
volume: {}
networks:
default:
name: mercury_default
volumes:
test_postgres:
name: mercury_test_postgres
$ docker compose -f docker-compose.yml config postgres
name: mercury
services:
postgres:
environment:
POSTGRES_DB: mercury
POSTGRES_PASSWORD: mercury
POSTGRES_USER: mercury
image: postgres:16
networks:
default: null
ports:
- mode: ingress
target: 5432
published: "5432"
protocol: tcp
volumes:
- type: volume
source: postgres
target: /var/lib/psql/data
volume: {}
networks:
default:
name: mercury_default
volumes:
postgres:
name: mercury_postgres
Note the volumes’ source
and target
.