I understand that using the older version of docker-compose, we can create another container with just a data volume and link it using volumes_from to make it a "data-only container." However, I wanted to test out using 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 off of this post.
I have another script running to wait until this postgres container is up before the other containers run for example:
container:
build: .
volumes:
- ./scripts/wait-for-postgres.sh:/code/wait-for-postgres.sh
entrypoint: ./wait-for-postgres.sh "command"
with the script looking like:
#!/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 leads to the containers stalling and not coming up at all and I cannot even get the postgres container to initialize with the tables I need.
There are two ways where you can create a volume, bind mounts and volumes. Whichever you choose, once you have set up a volume to the folder where the data is stored in the container, if you do a docker-compose down , and then a docker-compose up , your data will not be erased and it will become persistent.
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.
Docker has an option to allow specific folders in a container to be mapped to the normal filesystem on the host. This allows us to have data in the container without making the data part of the Docker image, and without being bound to AUFS.
With version 2 is not needed to run the check script, because postgres will start listening once it's started, and you can use depends_on
to define the dependency. Here's how I setup a postgres, a volume and a 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With