首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于带有码头的GitLab管道的PGHOST :由docker-组合创建的postgres的dind

问用于带有码头的GitLab管道的PGHOST :由docker-组合创建的postgres的dind
EN

Stack Overflow用户
提问于 2022-03-13 18:42:16
回答 1查看 333关注 0票数 0

我有一个坞-撰写文件,它初始化postgres和postgres迁移服务。我还想在gitlab管道上针对我的插件-编写烘焙postgres服务运行测试,但我无法通过本地主机连接到pg_db。在我的代码中,我使用pgx包。在我的本地机器上,对PGHOST变量使用localhost没有问题。

因此,我的主要问题是在PGHOST变量中放置什么主机,以便测试在gitlab管道中使用postgres连接。

docker-compose.yml

代码语言:javascript
复制
version: "3.3"
services:
    pg_db:
      container_name: pg_db
      image: postgres:13.2-alpine
      environment:
        - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
        - POSTGRES_USER=${POSTGRES_USER}
        - POSTGRES_DB=${POSTGRES_DB}
        - POSTGRES_SSLMODE=${POSTGRES_SSLMODE}
        - POSTGRES_HOST_AUTH_METHOD=${POSTGRES_HOST_AUTH_METHOD}
      ports:
        - ${POSTGRES_PORT}:5432
      restart: always
      deploy:
        resources:
          limits:
            cpus: '1'
            memory: 4G
      networks:
        - postgres
        - backend

    #init db
    store-init:
      image: x:latest
      container_name: store-init
      environment:
        - PGHOST=pg_db
        - PGUSER=${POSTGRES_USER}
        - PGPASSWORD=${POSTGRES_PASSWORD}
        - PGDATABASE=${POSTGRES_DB}
        - PGPORT=${POSTGRES_PORT}
      restart: on-failure
      depends_on:
        - pg_db
      networks:
        - postgres
        - backend

networks:
  backend:
  postgres:
    driver: bridge

这里是我的gitlab-ci.yml的一个重要部分

代码语言:javascript
复制
services:
    - docker:dind

stages:
  - test

test:
  stage: test
  image: golang:1.17-alpine3.15
  variables:
    PGHOST: localhost
  before_script:
    - apk update && apk add make git openssh g++
    - apk add --no-cache docker-compose
    - git config --global user.email "$GITLAB_USER_EMAIL" && git config --global user.name "$GITLAB_USER_NAME"
    - mkdir -p ~/.ssh && echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa && chmod -R 600 ~/.ssh && ssh-keyscan -t rsa ssh.x >> ~/.ssh/known_hosts
  script:
    - cp .env.example .env
    - docker-compose up -d
    - sleep 30 # a temporary line to get the logs
    - cat /etc/hosts # debug line
    - docker-compose port pg_db 5432 # debug line
    - netstat -a # debug line
    - docker-compose ps # debug line
    - go test -v -timeout 30s ./... -tags=withDB

  only:
    - merge_request
    - dev
    - master

我要的原木

代码语言:javascript
复制
variables: 
   PGHOST: localhost
代码语言:javascript
复制
$ cp .env.example .env
$ docker-compose up -d
Recreating alp-logger_pg_db_1 ... 
Recreating alp-logger_pg_db_1 ... done
store-init is up-to-date
$ sleep 30
$ cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2  runner-lxzkchpx-project-304-concurrent-0
$ docker-compose port pg_db 5432
0.0.0.0:5432
$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 runner-lxzkchpx-project-304-concurrent-0:50294 static.124.194.21.65.clients.your-server.de:ssh TIME_WAIT   
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
$ docker-compose ps
   Name                 Command              State           Ports         
---------------------------------------------------------------------------
pg_db        docker-entrypoint.sh postgres   Up      0.0.0.0:5432->5432/tcp
store-init   ./alp-store                     Up                            

和连接到postgres db的错误:

代码语言:javascript
复制
failed to connect to `host=localhost user=test database=test`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)

调试命令的日志是相同的,所以我将跳过它们。我所犯的错误

代码语言:javascript
复制
variables: 
    PGHOST: pg_db

对于任何其他命名主机,如docker。

代码语言:javascript
复制
failed to connect to `host=pg_db user=test database=test`: hostname resolving error (lookup pg_db on 1.1.1.1:53: no such host)

我所犯的错误

代码语言:javascript
复制
variables: 
    PGHOST: 127.0.0.1
代码语言:javascript
复制
failed to connect to `host=127.0.0.1 user=test database=test`: dial error (dial tcp 127.0.0.1:5432: connect: connection refused)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-14 02:29:41

在本地机器上运行容器与在GitLab中使用docker:dind运行容器之间的一个重要区别是,容器在'localhost‘上不可用--它们在docker:dind容器上可用。

如果您想要与这个容器对话,在您的场景中,postgres容器将在docker:5432上可用(docker是您的postgres容器有其端口映射的docker:dind容器的主机名)。

简单HTTP服务容器的说明

作为一个简化的示例,如果要在本地运行带有端口映射的容器strm/helloworld-http,下面的工作如下:

代码语言:javascript
复制
docker run -d --rm -p 80:80 strm/helloworld-http
# give it some time to startup
curl http://localhost  # this works

但是,GitLab中的相同设置并不是:

代码语言:javascript
复制
myjob:
  variables:  # these variables are not necessarily required
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: "tcp://docker:2375"
  services:
    - docker:dind
  script:
    - docker run -d --rm -p 80:80 strm/helloworld-http
    - sleep 10
    - curl http://localhost  # Fails!

一个解决方法是使用docker主机名,而不是:

代码语言:javascript
复制
script:
    - docker run -d --rm -p 80:80 strm/helloworld-http
    - sleep 10
    - curl http://docker  # works!
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71459760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档