2

I am super embarrassed to ask this question, because it seems a very basic one, but somehow I can't find the answer in docs.

I have a django app that uses postgres. In docker-compose.yaml there is the following requirement:

version: "2"

services:

database:
  image: postgres:9.5
  environment:
    POSTGRES_DB: ${POSTGRES_DATABASE}
    POSTGRES_USER: ${POSTGRES_USER}
    POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    POSTGRES_DATA: /var/lib/postgresql/data/pgdata

when I run my docker image: docker run -it --name myapp myimage it keeps repeating:

The database is not ready.
wait for postgres to start...

I ran postgres in detached mode: docker run -it -d postgres:9.5 but it does not help

0

3 Answers 3

3

With Docker Compose 2.1 syntax, you can specify healthchecks to control container start-up:

version: '2.1'

services:
    application:
        depends_on:
            database:
                condition: service_healthy

Check out https://github.com/docker-library/healthcheck/tree/master/postgres for an example Dockerfile for building Postgres with healthchecks.

Sign up to request clarification or add additional context in comments.

Comments

1

Please have a look at this doc.

Second example is exacly what you need:

You create sh script and add it to your app container by using ADD or COPY:

#!/bin/bash
# wait-for-postgres.sh

set -e

host="$1"
shift
cmd="$@"

until psql -h "$host" -U "postgres" -c '\l'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - executing command"
exec $cmd

Then you modify your docker-compose.yaml like this:

version: "2"
services:
  web:
    build: .
    ports:
      - "80:8000"
    depends_on:
      - "db"
    command: ["./wait-for-postgres.sh", "db", "python", "app.py"]
  db:
    image: postgres

In command you're orerriding default command of your container. Of course "python", "app.py" part is depedent on how you start your app. Fo Java it would be for example "java", "-jar", "my-app.jar" etc.

2 Comments

but it tells me that the role 'my_user_name' is not found. In db: section I have now: database: image: postgres:9.5 environment: POSTGRES_DB: "django_db" POSTGRES_USER: "my_user_name" POSTGRES_PASSWORD: "my_password" I should somehow 'tell' ti postgres image to create my_user_name user, right? But how?
Please check this page. You can set admin credentials by setting environment variables like POSTGRES_PASSWORD. You can check how to set env variables in docker-compose here.
0

Everything is OK with the way you start the container, however, the issues you are experiencing it is because the DB and the APP containers are starting one after the other, however, the DB container, needs to prepare the DB, do the migrations and so on, so therefore, by the time your app container tries to reach your DB container it is not ready so that's why you are getting this error.

You have 2 choices, one is to edit your APP Dockerfile and add WAIT for 30 - 60 s or so, or you could just start the DB on its own, wait for it to be ready to go and then start your APP container.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.