Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for postgres startup in ENTRYPOINT?

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.

like image 803
fredrik Avatar asked Feb 13 '17 14:02

fredrik


People also ask

How to start PostgreSQL on startup?

If you want to start postgres on startup so that you wont have to restart is all the time simply do: Show activity on this post. From Ubuntu 15.04 onwards do: Show activity on this post. So I figured out how to boot postgresql so i dont need to do the reinstall newb move.

How to run PostgreSQL client in an until loop?

If the backend application itself has a PostgreSQL client, you can use the pg_isready command in an until loop. For example, suppose we have the following project directory structure, version: "3" services: postgres: image: postgres backend: build: ./backend

Where can I find autostart scripts for PostgreSQL?

Autostart scripts are operating-system-specific. There are a few example scripts distributed with PostgreSQL in the contrib/start-scripts directory. Installing one will require root privileges. Different systems have different conventions for starting up daemons at boot time.

How to wait for PG_CTL to start the database?

If you use the pg_ctl command to start the database, use the "-w" parameters for that to wait until startup is finished before returning. It doesn't do anything fancy--it just does the "is it ready yet?"


1 Answers

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.

like image 175
zeppelin Avatar answered Sep 18 '22 17:09

zeppelin