postgis / docker-postgis

Docker image for PostGIS
https://hub.docker.com/r/postgis/postgis/
MIT License
1.35k stars 462 forks source link

Reconnecting database during init process makes a dependency service management harder in "docker-compose.yml" #296

Closed sanak closed 2 years ago

sanak commented 2 years ago

From #290 and #292, reconnecting database during init process was introduced, but this makes a dependency service management harder in docker-compose.yml like the following redmine usecase, because redmine database migration process starts before reconnecting database and failed. https://github.com/gtt-project/docker-gtt/blob/feature/simplify-docker-settings/docker-compose.yml

services:
  postgis:
    image: postgis/postgis:14-3.2
    volumes:
      - postgis:/var/lib/postgresql/data

  redmine:
    :
    depends_on:
      - postgis

docker-compose.yml side workaround is possible by waiting for last postgis_tiger_geocoder extension like as follows, but this is not handy.

     volumes:
       - postgis:/var/lib/postgresql/data
+    healthcheck:
+      test: "pg_isready && psql -c \"\\dx\" | grep postgis_tiger_geocoder"
+      interval: 10s
+      timeout: 2s
+      retries: 5
+      start_period: 10s

   redmine:
     :
     depends_on:
-      - postgis
+      postgis:
+        condition: service_healthy

So, if possible, without reconnecting database during init process is preferable.

The following diff is my suggestion, but I am not sure whether the sessions are separated and original #290 issue will be solved by this.

--- a/initdb-postgis.sh
+++ b/initdb-postgis.sh
        "${psql[@]}" --dbname="$DB" <<-'EOSQL'
                CREATE EXTENSION IF NOT EXISTS postgis;
                CREATE EXTENSION IF NOT EXISTS postgis_topology;
-               -- Reconnect to update pg_setting.resetval
-               -- See https://github.com/postgis/docker-postgis/issues/288
-               \c
+EOSQL
+
+       "${psql[@]}" --dbname="$DB" <<-'EOSQL'
                CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
                CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder;
 EOSQL
sanak commented 2 years ago

Using external IP (address) in docker-compose.yml healthcheck (from https://github.com/postgis/docker-postgis/pull/297#issuecomment-1147603192) is enough handy, so I close this issue.

--- a/docker-compose.yml
+++ b/docker-compose.yml
     volumes:
       - postgis:/var/lib/postgresql/data
+    healthcheck:
+      test: "pg_isready -h postgis -U ${DB_USER} -d ${DB_NAME}"
+      interval: 10s
+      timeout: 2s
+      retries: 5
+      start_period: 10s

   redmine:
    :
     depends_on:
-      - postgis
+      postgis:
+        condition: service_healthy