7

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.

1 Answer 1

7

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:
Sign up to request clarification or add additional context in comments.

3 Comments

I see! But what if I need to have migrations set up on my flask app before I want to start other containers? In another script wait-for-flask.sh I do until psql -h "postgres" -U "postgres" -c 'SELECT * FROM this_table;'; do >&2 echo "Postgres is unavailable - sleeping" sleep 1 done This has been the only way I have been able to get my other containers from running before migrations are done in alembic. Any idea how I might be able to get over this?
Also, I thought that like "linked", "depends on" doesn't really worry about whether its up or not, just if its started. For example, it only cares that the postgres container is up but not ready to start. Is that wrong @waterscar?
@JosephSong you are right that depends_on will not wait until the service is ready, so you do need the script to check it. And you need to put this check script to all app containers that are depends_on the postgres container, to make sure it wait until the migration completed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.