Reputation: 10291
What would be the best approach to wait for postgres
to fully start up inside my ENTRYPOINT
before moving on executing nosetests
?
Right now I've timed the startup on my machine to around 50 seconds. So I'm just sleeping for 60 seconds. This does not feel good as this might not work when being run on another machine.
ENTRYPOINT \
runuser -l postgres -c '/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf & ' && \
sleep 60 && \
nosetests --verbose --cover-erase --with-coverage --cover-package=stalker
This is the output of starting up postgres
:
2017-02-13 13:46:49.541 UTC [9] LOG: database system was interrupted; last known up at 2017-02-13 12:53:23 UTC
2017-02-13 13:47:37.951 UTC [9] LOG: database system was not properly shut down; automatic recovery in progress
2017-02-13 13:47:37.994 UTC [9] LOG: redo starts at 0/1783EA0
2017-02-13 13:47:37.995 UTC [9] LOG: record with zero length at 0/17841E8
2017-02-13 13:47:37.995 UTC [9] LOG: redo done at 0/17841B8
2017-02-13 13:47:37.995 UTC [9] LOG: last completed transaction was at log time 2017-02-13 12:53:23.731984+00
2017-02-13 13:47:38.384 UTC [9] LOG: MultiXact member wraparound protections are now enabled
2017-02-13 13:47:38.387 UTC [7] LOG: database system is ready to accept connections
2017-02-13 13:47:38.387 UTC [13] LOG: autovacuum launcher started
Please, I do understand that this goes against the convention of running multiple commands in ENTRYPOINT
. I have good reasons to do this in this case.
Upvotes: 12
Views: 10117
Reputation: 10291
Thanks to @zeppelin for suggesting pg_isready
. I ended up using this:
#!/bin/bash
# wait-for-postgres.sh
set -e
cmd="$@"
timer="5"
until runuser -l postgres -c 'pg_isready' 2>/dev/null; do
>&2 echo "Postgres is unavailable - sleeping for $timer seconds"
sleep $timer
done
>&2 echo "Postgres is up - executing command"
exec $cmd
I'm using this in my ENTRYPOINT
:
ENTRYPOINT \
# Start PostgreSQL
runuser -l postgres -c '/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf & ' && \
# Exectute tests when db is up
./wait-for-postgres.sh nosetests --verbose --cover-erase --with-coverage --cover-package=stalker
Upvotes: 16
Reputation: 9365
I will normally use a small "tcp-port-wait" script, like this:
#!/bin/bash
set -e
if [ -z "$1" -o -z "$2" ]
then
echo "tcp-port-wait - block until specified TCP port becomes available"
echo "Usage: ntcp-port-wait HOST PORT"
exit 1
fi
echo Waiting for port $1:$2 to become available...
while ! nc -z $1 $2 2>/dev/null
do
let elapsed=elapsed+1
if [ "$elapsed" -gt 90 ]
then
echo "TIMED OUT !"
exit 1
fi
sleep 1;
done
echo "READY !"
To wait until a certain service is up and ready.
In case of Postgresql, the default port it will listen on is 5432, so the command would be:
tcp-port-wait localhost 5432
That will block until Postgresql service is ready to serve connections, on :5432 on the loopback interface inside the container (when run in context of the ENTRYPOINT script).
You certainly have to copy this script into your container, by adding a line like that to your Dockerfile:
COPY tcp-port-wait /usr/local/bin/
before you can use it.
And also install the netcat utility.
You can use this for other types of services, like Tomcat or Mysql, too.
And, if you need, you can also wait "outside the container", like this:
docker exec my_container tcp-port-wait localhost 5432
Note, there are certainly other ways to do this, e.g. by using some orchestration tool, like docker-compose with healthcheck directive, or some process manager inside the container itself.
Upvotes: 7