Docker-compose: postgres data not persisting
I have a main service in my file
that uses the image
and although I seem to be connecting to the database successfully, the data I write to you is not persisting outside of the container's life (what I did is based on this tutorial ) ...
main: build: . volumes: .:/code links: postgresdb command: python manage.py insert_into_database environment: DEBUG=true postgresdb: build: utils/sql/ volumes_from: postgresdbdata ports: "5432" environment: DEBUG=true postgresdbdata: build: utils/sql/ volumes: /var/lib/postgresql command: true environment: DEBUG=true
and here's the Dockerfile I'm using for services
(which essentially creates a database and adds a user):
FROM postgres ADD make-db.sh /docker-entrypoint-initdb.d/
How can I force the data to remain after the service ends
so that I can use it in the future (for example, when I call something like
python manage.py retrieve_from_database
)? Is it
even the correct directory and has
access to it, given that it appears to be restricted
source to share
The problem is that Compose creates a new version of the container
every time it restarts, so the old container and its data are lost.
The second problem is that your data container shouldn't be running; data containers are just a namespace for a volume that can be imported with
, which still works with stopped containers.
Currently the best solution is to extract the container
from the Compose configuration. Do something like:
docker run --name postgresdbdata postgresdb echo "Postgres data container" Postgres data container
The echo command will run and the container will exit, but for now
, you can still reference it in
and your Compose app should work fine.
source to share