Search code examples
dockergitlabpostgisgitlab-cipipeline

Service "postgis" fails to start in GitLab CI


I am trying to use the Docker image "postgis/postgis:latest" as a service in GitLab CI but the service fails to start.
This is the start of the CI log, the last line is most important:

Running with gitlab-runner 12.9.0 (4c96e5ad)
  on xxxxxxx xxxxxxxx
Preparing the "docker" executor
Using Docker executor with image node:lts-stretch ...
Starting service redis:latest ...
Pulling docker image redis:latest ...
Using docker image sha256:4cdbec704e477aab9d249262e60b9a8a25cbef48f0ff23ac5eae879a98a7ebd0 for redis:latest ...
Starting service postgis/postgis:latest ...
Pulling docker image postgis/postgis:latest ...
Using docker image sha256:a412dcb70af7acfbe875faea4467a1594e7cba3dfca19e5e1c6bcf35286380df for postgis/postgis:latest ...
Waiting for services to be up and running...
*** WARNING: Service runner-xxxxxxxx-project-1-concurrent-0-postgis__postgis-1 probably didn't start properly.
Health check error:
service "runner-xxxxxxxx-project-1-concurrent-0-postgis__postgis-1-wait-for-service" timeout
Health check container logs:
Service container logs:
2020-04-06T11:58:09.487216183Z The files belonging to this database system will be owned by user "postgres".
2020-04-06T11:58:09.487254326Z This user must also own the server process.
2020-04-06T11:58:09.487260023Z 
2020-04-06T11:58:09.488674041Z The database cluster will be initialized with locale "en_US.utf8".
2020-04-06T11:58:09.488696993Z The default database encoding has accordingly been set to "UTF8".
2020-04-06T11:58:09.488704024Z The default text search configuration will be set to "english".
2020-04-06T11:58:09.488710330Z 
2020-04-06T11:58:09.488716134Z Data page checksums are disabled.
2020-04-06T11:58:09.488721778Z 
2020-04-06T11:58:09.490435786Z fixing permissions on existing directory /var/lib/postgresql/data ... ok
2020-04-06T11:58:09.490649106Z creating subdirectories ... ok
2020-04-06T11:58:09.490656485Z selecting dynamic shared memory implementation ... posix
2020-04-06T11:58:09.525841255Z selecting default max_connections ... 100
2020-04-06T11:58:09.562735034Z selecting default shared_buffers ... 128MB
2020-04-06T11:58:09.614695491Z selecting default time zone ... Etc/UTC
2020-04-06T11:58:09.616784837Z creating configuration files ... ok
2020-04-06T11:58:09.917724902Z running bootstrap script ... ok
2020-04-06T11:58:10.767115421Z performing post-bootstrap initialization ... ok
2020-04-06T11:58:10.924542026Z syncing data to disk ... ok
2020-04-06T11:58:10.924613120Z 
2020-04-06T11:58:10.924659485Z initdb: warning: enabling "trust" authentication for local connections
2020-04-06T11:58:10.924720453Z You can change this by editing pg_hba.conf or using the option -A, or
2020-04-06T11:58:10.924753751Z --auth-local and --auth-host, the next time you run initdb.
2020-04-06T11:58:10.925150488Z 
2020-04-06T11:58:10.925175359Z Success. You can now start the database server using:
2020-04-06T11:58:10.925182577Z 
2020-04-06T11:58:10.925188661Z     pg_ctl -D /var/lib/postgresql/data -l logfile start
2020-04-06T11:58:10.925195041Z 
2020-04-06T11:58:10.974712774Z waiting for server to start....2020-04-06 11:58:10.974 UTC [47] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-04-06T11:58:10.976267115Z 2020-04-06 11:58:10.976 UTC [47] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-04-06T11:58:11.003287980Z 2020-04-06 11:58:11.002 UTC [48] LOG:  database system was shut down at 2020-04-06 11:58:10 UTC
2020-04-06T11:58:11.011056242Z 2020-04-06 11:58:11.010 UTC [47] LOG:  database system is ready to accept connections
2020-04-06T11:58:11.051536096Z  done
2020-04-06T11:58:11.051578164Z server started
2020-04-06T11:58:11.051855017Z 
2020-04-06T11:58:11.052088262Z /usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/10_postgis.sh
2020-04-06T11:58:11.218053189Z psql: error: could not connect to server: could not translate host name "postgres" to address: Name or service not known

could not translate host name "postgres" to address: Name or service not known

It seems to me that the host "postgres" is wrong. But the documenation of GitLab says that the hostname will be the alias: https://docs.gitlab.com/ce/ci/docker/using_docker_images.html#accessing-the-services

Excerpt of my .gitlab-ci-yml:

image: node:lts-stretch

services:
  - name: redis:latest
  - name: postgis/postgis:latest
    alias: postgres

variables:
  NODE_ENV: production
  REDIS_HOST: redis
  POSTGRES_DB: postgres
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres
  PGHOST: postgres
  PGUSER: postgres
  PGPASSWORD: postgres

I have also tried to remove the alias and use "postgis-postgis" or "postgis__postgis" as the hostname as per documenation, but the same error every time. I also tried to use the docker image "mdillon/postgis" because i saw it often, but also the same error.


Solution

  • I tried plugging in your .gitlab-ci.yml excerpt and got an error:

    This GitLab CI configuration is invalid: jobs config should contain at least one visible job
    

    Please provide a minimal reproducible example next time. ;)

    I was able to reproduce and fix the issue. The fix was to remove the PGHOST setting. (You had its value set to postgres. Your main container can get to the postgis container using the alias postgres but the postgis container itself doesn't need a hostname to get to the PostgreSQL service because that service is listening on a local socket.)

    PGHOST is used by psql in the "postgis" container (launched by the services directive), in the script https://github.com/postgis/docker-postgis/blob/master/initdb-postgis.sh (which ends up in /docker-entrypoint-initdb.d/10_postgis.sh -- see https://github.com/postgis/docker-postgis/blob/master/Dockerfile.template#L16)

    The following .gitlab-ci.yml works:

    image: node:lts-stretch
    
    variables:
      POSTGRES_DB: postgres
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      PGUSER: postgres
      PGPASSWORD: postgres
    
    services:
    - name: postgis/postgis:latest
      alias: postgres
    
    job1:
      script: ping -c 3 postgres
    

    Here is the job log:

    Running with gitlab-runner 12.9.0 (4c96e5ad)
       on docker-auto-scale 0277ea0f
    Preparing the "docker+machine" executor
     Using Docker executor with image node:lts-stretch ...
     Starting service postgis/postgis:latest ...
     Pulling docker image postgis/postgis:latest ...
     Using docker image sha256:a412dcb70af7acfbe875faea4467a1594e7cba3dfca19e5e1c6bcf35286380df for postgis/postgis:latest ...
     Waiting for services to be up and running...
     Pulling docker image node:lts-stretch ...
     Using docker image sha256:88c089733a3b980b3517e8e2e8afa46b338f69d7562550cb3c2e9fd852a2fbac for node:lts-stretch ...
    Preparing environment
    00:05
     Running on runner-0277ea0f-project-17971942-concurrent-0 via runner-0277ea0f-srm-1586221223-45d7ab06...
    Getting source from Git repository
    00:01
     $ eval "$CI_PRE_CLONE_SCRIPT"
     Fetching changes with git depth set to 50...
     Initialized empty Git repository in /builds/atsaloli/service-postgis/.git/
     Created fresh repository.
     From https://gitlab.com/atsaloli/service-postgis
      * [new ref]         refs/pipelines/133464596 -> refs/pipelines/133464596
      * [new branch]      master                   -> origin/master
     Checking out d20469e6 as master...
     Skipping Git submodules setup
    Restoring cache
    00:02
    Downloading artifacts
    00:01
    Running before_script and script
    00:04
     $ ping -c 3 postgres
     PING postgres (172.17.0.3) 56(84) bytes of data.
     64 bytes from postgis-postgis (172.17.0.3): icmp_seq=1 ttl=64 time=0.077 ms
     64 bytes from postgis-postgis (172.17.0.3): icmp_seq=2 ttl=64 time=0.064 ms
     64 bytes from postgis-postgis (172.17.0.3): icmp_seq=3 ttl=64 time=0.060 ms
     --- postgres ping statistics ---
     3 packets transmitted, 3 received, 0% packet loss, time 2062ms
     rtt min/avg/max/mdev = 0.060/0.067/0.077/0.007 ms
    Running after_script
    00:01
    Saving cache
    00:02
    Uploading artifacts for successful job
    00:01
     Job succeeded
    

    As you can see in the ping command above, the container created from the image node:lts-stretch is able to access the postgis container using the postgres alias.

    Does that unblock you?