Reputation: 486
My docker-compose file:
version: '3'
services:
app:
container_name: application
build: .cloud/php
image: app-application
depends_on:
- pgres
ports:
- "9050:9000"
volumes:
- ./:/var/www:cached
networks:
- application_network
nginx:
container_name: application.nginx
image: nginx
ports:
- "8050:8000"
volumes:
- .cloud/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:cached
- ./:/var/www:cached
depends_on:
- app
networks:
- application_network
pgres:
container_name: application.postgres
image: postgres
restart: always
ports:
- "54325:5432"
environment:
POSTGRES_DB: application
POSTGRES_USER: postgres
POSTGRES_PASSWORD: example
PGDATA: /tmp
volumes:
- .cloud/postgres/data:/var/lib/postgresql/data
networks:
- application_network
networks:
application_network:
driver: bridge
When I run docker-compose down, postgresql data is not persisting and database resets completely. I tried to put the postgresql volume into its own /postgresql/data folder but still same result. What am I missing here?
Upvotes: 0
Views: 2499
Reputation: 2013
The problem is here
environment:
PGDATA: /tmp
volumes:
- .cloud/postgres/data:/var/lib/postgresql/data
With the PGDATA
env var, you are setting the cluster data directory explicitly. So, what it does is, once you start your container (e.g. via docker-compose up
), Postgres server will persist your db data as per your PGDATA
setting within the /tmp
directory.
On the other hand you are mounting a different path in the container (/var/lib/postgresql/data
) instead of the /tmp
path. Thus once you run docker-compose down
, the container and the persisted data in /tmp
will be gone for good, since /tmp
is not set as mounting point. So, unless you really need to do so, you better not touch it at all.
A working configuration might look so (shortened for the sake of brevity):
version: '3'
services:
pgres:
image: "postgres" # use latest official postgres version or a specific version e.g. postgres:10.12
volumes:
- .cloud/postgres/:/var/lib/postgresql/data/ # persist data even if container shuts down
Another thing is, even let's say you configured it via PGDATA
like so
environment:
PGDATA: /tmp
volumes:
- .cloud/postgres/data:/tmp
It wouldn't be a good idea, since the path /tmp
is in your case, as the name tmp
(temporary) suggests, really not reliable. docker-compose down
will probably not affect it at all, but on the next boot, i.e. once you run docker-compose up
again, the underlying OS (Linux) will very much likely delete the content of the directory /tmp
, therefore the mounted local dir .cloud/postgres/data
will be also immediately emptied and your persisted data will be gone. In any case the /tmp
dir is somewhat managed by Linux, thus the data saved there might simply disappear any time - so don't rely on it!
Upvotes: 4