【问题标题】:Service "postgis" fails to start in GitLab CI服务“postgis”无法在 GitLab CI 中启动
【发布时间】:2020-04-06 12:21:46
【问题描述】:

我正在尝试使用 Docker 映像“postgis/postgis:latest”作为 GitLab CI 中的服务,但该服务无法启动。
这是 CI 日志的开头,最后一行是最重要的:

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

无法将主机名“postgres”转换为地址:名称或服务未知

在我看来,主机“postgres”是错误的。但是 GitLab 的文档说主机名将是别名:https://docs.gitlab.com/ce/ci/docker/using_docker_images.html#accessing-the-services

我的 .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

我还尝试删除别名并根据文档使用“postgis-postgis”或“postgis__postgis”作为主机名,但每次都出现相同的错误。我也尝试使用 docker 镜像“mdillon/postgis”,因为我经常看到它,但也出现同样的错误。

【问题讨论】:

    标签: docker gitlab postgis gitlab-ci pipeline


    【解决方案1】:

    我尝试插入您的 .gitlab-ci.yml 摘录并收到错误:

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

    下次请提供一个可重现的最小示例。 ;)

    我能够重现并修复该问题。修复是删除PGHOST 设置。 (您将其值设置为 postgres。您的主容器可以使用别名 postgres 访问 postgis 容器,但 postgis 容器本身不需要主机名即可访问 PostgreSQL 服务,因为该服务正在侦听本地套接字。)

    PGHOSTpsql 在“postgis”容器中使用(由services 指令启动),在脚本https://github.com/postgis/docker-postgis/blob/master/initdb-postgis.sh 中(最终在/docker-entrypoint-initdb.d/10_postgis.sh 中使用——参见https://github.com/postgis/docker-postgis/blob/master/Dockerfile.template#L16

    以下.gitlab-ci.yml 有效:

    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
    

    这是工作日志:

    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
    

    正如您在上面的 ping 命令中看到的,从图像 node:lts-stretch 创建的容器能够使用 postgres 别名访问 postgis 容器。

    这会解除对您的阻止吗?

    【讨论】:

    • 是的,非常感谢!我删除了 PGHOST,现在服务可以正常启动。这个解决方案的坏处是我的应用程序需要 PGHOST 变量。但似乎没有办法改变我的代码,因为没有选项可以为服务和作业使用不同的变量。这是一个未解决的问题gitlab.com/gitlab-org/gitlab/issues/29815
    • 知道了,感谢 GitLab 问题的链接。我已将其更新为在此处进行交叉链接。您能否在作业级别为您的应用程序设置 PGHOST 变量,或者您是否需要在多个作业中使用它?换句话说,您不需要 postgis 容器中的 PGHOST 变量,对吗?您只需要在节点容器中使用它,对吗?那么你能在你的作业中定义变量吗(因为你的作业将在节点容器中运行)?
    • 感谢您更新问题。我试图从顶级变量块中删除 PGHOST 并将其添加到我需要它的作业中。但是服务是由工作启动的,所以它们得到相同的变量。但是我通过将export PGHOST="postgres" 添加到需要访问数据库的作业的before_script 来使其工作。
    猜你喜欢
    • 2021-10-13
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 2022-10-25
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    相关资源
    最近更新 更多