4

I have the following docker-compose.yml:

version: '2'
services:
  server:
    build: .
    command: ["./setup/wait-for-postgres.sh", "tide_server::5432", "cd /app", "npm install", "npm run start"]
    ports:
      - 3030:3030
    links:
      - database
    depends_on:
      - database
  database:
    image: postgres
    environment:
      - "POSTGRES_USER=postgres"
      - "POSTGRES_PASSWORD=postgres"
      - "POSTGRES_DB=tide_server"
    ports:
      - 5432:5432

I tried following this tutorial and using the following shell script to determine when postgres is ready.

#!/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

My node Dockerfile is minimal but I have added it for reference:

FROM node:latest

ADD . /app

WORKDIR /app

EXPOSE 3030

Now when I try to run docker-compose up I get the following (after the postgres container is ready:

server_1    | Postgres is unavailable - sleeping
server_1    | ./setup/wait-for-postgres.sh: line 10: psql: command not found
server_1    | Postgres is unavailable - sleeping
server_1    | ./setup/wait-for-postgres.sh: line 10: psql: command not found
server_1    | Postgres is unavailable - sleeping
server_1    | ./setup/wait-for-postgres.sh: line 10: psql: command not found
server_1    | Postgres is unavailable - sleeping
server_1    | ./setup/wait-for-postgres.sh: line 10: psql: command not found

Now I am not sure if this is a linking issue, or something wrong with my script but I have tried every variation I can think of and have had no luck getting this up/

2
  • 1
    you need postgres client inside you "node" app container.
    – papey
    May 16, 2017 at 10:36
  • I had similar issue although my problem was silly, the psql host connection command was wrong ('localhost' as host instead of 'postgres'). Mar 7, 2018 at 13:42

2 Answers 2

7

This will successfully wait for Postgres to start. (Specifically line 6)

services:
  practice_docker: 
    image: dockerhubusername/practice_docker
    ports: 
      - 80:3000
    command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; npm start'
    depends_on:
      - db
    environment:
      - DATABASE_URL=postgres://postgres:password@db:5432/practicedocker
      - PORT=3000   
  db:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=practicedocker
1
  • ./docker-entrypoint.sh: 4: cannot open /dev/tcp/db/5432: No such file
    – PirateApp
    Aug 27, 2022 at 14:30
3

When you declare command in docker-compose.yml, the command itself is executed in the container it is declared to start in. Catch my drift?

Your ./setup/wait-for-postgres.sh is being executed in the container, which has no postgres installed. And you can't change it.

But no, actually you can. Run your script in the postgres container. But if you define command in the database section, it will override the default CMD defined in postgres:latest, which is just CMD ["postgres"].

This means, you have to slightly rewrite your script:

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

set -e

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

postgres

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
6
  • 1
    I got error when using this script: ./wait-for-postgres.sh: line 10: postgres: command not found
    – Son Tran
    Aug 30, 2017 at 11:23
  • @ronin1184 and you execute it with postgres installed in postgres container?
    – Kevin Kopf
    Aug 30, 2017 at 20:46
  • My docker-compose.yml file exactly same as Otis Wright's file except command in web service: command: ["./wait-for-postgres.sh", "database", "python3", "manage.py", "runserver", "0.0.0.0:8000"]
    – Son Tran
    Sep 2, 2017 at 5:58
  • your problem must be elsewhere, a typo of some sort perhaps?
    – Kevin Kopf
    Sep 2, 2017 at 8:11
  • I got mistake. I put this command inside web container. But when I move it to postgres container in this simple form: command: ["./wait-for-postgres.sh", "db"] then I got error: "root" execution of the PostgreSQL server is not permitted.
    – Son Tran
    Sep 2, 2017 at 9:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.