Dockerized PostgreSQL: psql: FATAL: the database system is starting up

The problem is related to the default shutdown method of the pg_ctl stop mode (pg_ctl gets called by pg_ctlcluster). Stopping the cluster via pg_ctlcluster with the pg_ctl option -m smart during the build process solves this problem:

pg_ctlcluster 9.6 master stop -- -m smart

The "smart" method waits for active clients to disconnect and online backups to finish before shutting down in contrast to the default "fast" method. This is explained in the documentation of pg_ctl.

In addition, the container would exit once the pg_ctlcontrol process successfully started the database cluster via postgres (pg_ctlcontrol -> pg_ctl -> postgres). To prevent this, postgres can be called directly. The container.yml file would then look as follows:

version: '2'

services:

  database_master:
    image: hackermd/ubuntu-trusty-python
    user: postgres
    expose:
      - 5043
    command: ['dumb-init', '/usr/lib/postgresql/9.6/bin/postgres', '-D', '/var/lib/postgresql/9.6/master']
    links:
      - database_worker
    depends_on:
      - database_worker

  database_worker:
    image: hackermd/ubuntu-trusty-python
    user: postgres
    expose:
      - 9700
    command: ['dumb-init', '/usr/lib/postgresql/9.6/bin/postgres', '-D', '/var/lib/postgresql/9.6/worker']