连接到GitLab CI流水线中的分离容器

huangapple go评论66阅读模式
英文:

Connecting to detached container in gitlab CI pipeline

问题

I will provide the translation for the text you provided:

无法从Gitlab的CI连接到分离的容器

我启动了一个作为HTTP服务器的分离Docker容器,并尝试从流水线脚本连接到它,但是遇到了连接错误:

curl: (28) 连接到172.17.0.3端口9000失败,经过129268毫秒:无法连接到服务器

以下是重现连接问题的完整.gitlab-ci.yml文件:

stages:
  - Tests

with_server:
  image: docker:23.0.2
  services:
    - docker:23.0.2-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  stage: Tests
  before_script:
    - apk add --update --no-cache curl python3 && ln -sf $(which python3) /usr/bin/python
    - docker info
  script:
    - docker run --rm --detach --name that_server python:3.11-slim-bullseye python3 -m http.server 9000
    # 用于提取that_server的IP的inspect命令
    - curl $(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' that_server):9000
  after_script:
    - docker rm -f that_server

docker-dind被用于根据Gitlab的指南在CI流水线中调用docker命令。这应该允许构建和运行容器,然后连接到它。

附加信息:

  • 当我在本地运行时(docker runcurl)不存在此问题。
  • docker build可行
  • docker run hello-world可行
  • 创建一个网络并将容器连接到其中没有帮助
  • 创建另一个容器并从中连接可行,例如以下命令:
docker run --rm --name that_client python:3.11-slim-bullseye python -c "from urllib import request; req=request.Request(\"http://$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' that_server):9000\");print(request.urlopen(req).read())"

问题出现在我尝试从"主机"连接时,这个"主机"是gitlab-runner执行程序。我只是不知道为什么以及如何获得与分离的容器"连接"的可能性。

这种安排的目的是在容器中启动我的应用程序,然后执行针对它的测试。您知道我在这里漏掉了什么吗?谢谢。

英文:

Cannot connect to detached container from Gitlab's CI

I start detached docker container being an HTTP server and try to connect to it from the pipeline script, but get connection error:

curl: (28) Failed to connect to 172.17.0.3 port 9000 after 129268 ms: Couldn't connect to server

Here is the complete .gitlab-ci.yml that reproduces the connection problem:

stages:
  - Tests

with_server:
  image: docker:23.0.2
  services:
    - docker:23.0.2-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  stage: Tests
  before_script:
    - apk add --update --no-cache curl python3 && ln -sf $(which python3) /usr/bin/python
    - docker info
  script:
    - docker run --rm --detach --name that_server python:3.11-slim-bullseye python3 -m http.server 9000
    # that inspect is for extracting IP of that_server
    - curl $(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' that_server):9000
  after_script:
    - docker rm -f that_server

docker-dind is being used in order to call docker commands in the CI pipeline according to the gitlab's guide. That is supposed to allow to build and run container and then connect to it.

Side-facts:

  • The problem does not exist when I run it locally (docker run && curl).
  • docker build works
  • docker run hello-world works
  • creating a network and connecting the container there does not help
  • creating another container and connecting from it WORKS - e.g. with the following command:
docker run --rm --name that_client python:3.11-slim-bullseye python -c "from urllib import request; req=request.Request(\"http://$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' that_server):9000\");print(request.urlopen(req).read())"

The problem is when I try to connect from "host" which is gitlab-runner executor. I just don't know why and how to get a possibility to "connect" to the detached container.

The purpose of such arrangement is to start my application in a container and then execute tests against it. Do you know what do I miss here? Thanks

答案1

得分: 1

你正在使用 docker:dind 服务,它会启动容器。你的应用程序应该在 docker:9000 下可访问,而不是容器IP。

更新:

我注意到你没有映射端口,你还需要在你的 docker run 命令中加入 -p 9000:9000。还要在curl之前添加一点超时,例如 sleep 30,因为dind中容器启动需要一些时间。

英文:

You are using the docker:dind service, which starts the container. Your application should be accessible under docker:9000 instead of the container IP.

Update:

I noticed you have not mapped the port, you also need to add -p 9000:9000 to your docker run command. Also add a slight timeout before the curl, sleep 30 for example, as it takes some time until the container starts in dind.

huangapple
  • 本文由 发表于 2023年4月17日 19:29:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定