Search code examples
postgresqldockerexecdocker-composewait

Executing wait-for-it.sh in python Docker container


I have a Python docker container that needs to wait until another container (postgres server) finishes setup. I tried the standard wait-for-it.sh but several commands weren't included. I tried a basic sleep (again in an sh file) but now it's reporting exec: 300: not found when trying to finally execute the command I'm waiting on.

How do I get around this (preferably without changing the image, or having to extend an image.)

I know I could also just run a Python script, but ideally I'd like to use wait-for-it.sh to wait for the server to turn up rather than just sleep.

Dockerfile (for stuffer):

 FROM python:2.7.13

 ADD ./stuff/bin /usr/local/bin/
 ADD ./stuff /usr/local/stuff
 WORKDIR /usr/local/bin

 COPY requirements.txt /opt/updater/requirements.txt
 COPY internal_requirements.txt /opt/stuff/internal_requirements.txt

 RUN pip install -r /opt/stuff/requirements.txt
 RUN pip install -r /opt/stuff/other_requirements.txt

docker-compose.yml:

 version: '3'
 services:
   local_db:
     build: ./local_db
     ports:
     - "localhost:5432:5432"

   stuffer:
     build: ./
     depends_on:
     - local_db
     command: ["./wait-for-postgres.sh", "-t", "300", "localhost:5432", "--", "python", "./stuffing.py", "--file", "./afile"]

Script I want to use (but can't because no psql or exec):

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

Solution

  • Sergey's comment. I had wrong argument order. This issue had nothing to do with docker and everything to do with my inability to read.