How to create docker-composite version 2 to have persistent postgres db using volumes?

I understand that using an older version of docker-compose, we can create another container only with the data volume and associate it with volume_from to make it a β€œdata-only container”. However, I wanted to test the use of the new syntax.

version: '2' services: app: build: . links: - psql psql: image: postgres volumes_from: - psqldata ports: - "5432:5432" psqldata: image: postgres volumes: - psqlvolumes:/var/lib/postgresql/data/ volumes: psqlvolumes: driver: local 

This was based on this post .

I have another script waiting for this postgres container to start before other containers start:

 container: build: . volumes: - ./scripts/wait-for-postgres.sh:/code/wait-for-postgres.sh entrypoint: ./wait-for-postgres.sh "command" 

with the script is as follows:

 #!/bin/bash set -e export PGPASSWORD=postgres cmd=" $@ " until psql -h "postgres" -U "postgres" -c '\l'; do >&2 echo "Postgres is unavailable - sleeping" sleep 1 done >&2 echo "Postgres is up - executing command" exec $cmd 

This was taken from the docker website.

This only causes the containers to stop and not fit at all, and I can't even get the postgres container to initialize with the tables I need.

+6
source share
1 answer

With version 2, you do not need to run a script check, because postgres will start listening after it starts, and you can use depends_on to determine the dependency. This is how I configure postgres, volume and server (glassfish) running on postgres:

 version: '2' services: my-app: image: my-glassfish-image depends_on: - my-db my-db: image: my-postgres-image volumes: - postgres-db-volume:/data/postgres volumes: postgres-db-volume: 
+8
source

All Articles