docker - 服务 "postgis"无法在 GitLab CI 中启动

标签 docker gitlab postgis gitlab-ci pipeline

我尝试使用 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

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

在我看来,主机“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”,因为我经常看到它,但也出现同样的错误。

最佳答案

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

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

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

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

PGHOST 由“postgis”容器中的 psql 使用(由 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 命令中看到的,从镜像节点创建的容器:lts-stretch 能够使用 postgres 别名访问 postgis 容器。

这会解锁你吗?

关于docker - 服务 "postgis"无法在 GitLab CI 中启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61059759/

相关文章:

postgresql - 具有异构数据类型的 3 个字段的多列索引

docker - Docker镜像的新文件在哪里保存到GCP中?

image - 构建时可以是 docker 层 "bypassed"吗?

docker - Gitlab CI/Docker : Use custom image for job

Docker 构建失败 : tag invalid reference format (Gitlab CI)

centos - 适用于 CentOS 6 的 OSM2PGSQL

sql - 返回表的大小为 MB 而不是 KB

docker - 如何为所有新的 docker 网络设置基本 IP 地址

docker-compose 构建 nginx : network error (check Internet connection and firewall)

continuous-integration - Gitlab - 提交时自动格式化代码