Docker-compose: postgres data not persisting

I have a main service in my file docker-compose

that uses the image postgres

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 ) ...

Here's my docker-compose

file:

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 postgresdb

and postgresdbdata

(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 main

so that I can use it in the future (for example, when I call something like python manage.py retrieve_from_database

)? Is it /var/lib/postgresql

even the correct directory and has boot2docker

access to it, given that it appears to be restricted /Users/

?

Thank!

+3


source to share


1 answer


The problem is that Compose creates a new version of the container postgresdbdata

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 --volumes-from

, which still works with stopped containers.

Currently the best solution is to extract the container postgresdbdata

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 docker rm

, you can still reference it in --volumes-from

and your Compose app should work fine.

+7


source







All Articles