英文:
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 run
和curl
)不存在此问题。 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
worksdocker 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论