Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a docker-compose version 2 to have a persistent postgres db using volumes?

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.

like image 956
Joseph Song Avatar asked May 16 '16 20:05

Joseph Song


People also ask

Do docker-compose volumes persist?

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.

What feature in docker is used for data persistence?

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.

Which docker allows containers to run with persistent volumes?

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.


1 Answers

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:
like image 187
waterscar Avatar answered Sep 28 '22 08:09

waterscar