[英]Why can't I connect to my docker-compose service from Gitlab CI on localhost
I'm running this on Gitlab.com's CI runner(s).我在 Gitlab.com 的 CI 运行器上运行它。 A truncated version of my
.gitlab-ci.yml
is我的
.gitlab-ci.yml
的截断版本是
image: docker/compose:1.25.5
services:
- docker:dind
Test:
- script/test
And a truncated version of my script/test is我的脚本/测试的截断版本是
docker-compose run -d --name app1_test -p 8080:8080 app1 bash
docker-compose run -d --name app2_test -p 8081:8080 app2 bash
curl -s ORIGIN:8081/healthz
I find that regardless of whether my ORIGIN is localhost
, docker
, 0.0.0.0
etc. I always get can't connect to remote host (0.0.0.0): Connection refused
我发现无论我的 ORIGIN 是否是
localhost
, docker
, 0.0.0.0
等。我总是can't connect to remote host (0.0.0.0): Connection refused
I've seen so many answers on Gitlab forums and stack overflow and none of them solved the problem.我在 Gitlab 论坛和堆栈溢出上看到了很多答案,但没有一个能解决问题。 What is going on and how do I solve / diagnose the issue?
发生了什么,我该如何解决/诊断问题?
First of all, many of the answers on forums are pointing out the solution I ended up going with.首先,论坛上的许多答案都指出了我最终采用的解决方案。 In practice, the reason I wanted to check that service was so that another service in my docker-compose.yml could talk to it.
在实践中,我想检查该服务的原因是我的 docker-compose.yml 中的另一个服务可以与之对话。 So why not just use that service to do the ping?
那么为什么不直接使用该服务进行 ping 操作呢? Then you get a better test and you can use Docker's automatic DNS with the
--name
option.然后你得到一个更好的测试,你可以使用 Docker 的自动 DNS 和
--name
选项。
docker exec -t app1_test curl -s app2_test:8080/healthz
Now, if this still bugs you because you just want to know why this is happening and how to diagnose / solve it, or you really do need to talk to the container from the host.现在,如果这仍然困扰您,因为您只是想知道为什么会发生这种情况以及如何诊断/解决它,或者您确实需要从主机与容器交谈。 I recommend that you inspect some things in your script
我建议您检查脚本中的一些内容
cat /etc/hosts
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' app1_test
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' app2_test
what I got surprised me我感到惊讶
$ 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
127.0.0.1 0hshit.hopto.org
127.0.0.1 daymndaymn.myftp.org
127.0.0.1 loba.webhop.me
172.17.0.3 docker 26f99c2de716 runner-fa6cab46-project-18056856-concurrent-0-3b5b3ec1f3220cec-docker-0
172.17.0.4 runner-fa6cab46-project-18056856-concurrent-0
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' app1_test
172.20.0.3172.19.0.4
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' app2_test
172.20.0.2172.19.0.3
So the other answers about using docker
weren't working because that is pointing to 172.17.0.3 but neither of my containers are there.所以关于使用
docker
的其他答案不起作用,因为它指向 172.17.0.3 但我的容器都不在那里。 I should now be able to do a curl
to 172.20.0.2:8080
and be fine, but I didn't try it because by then I convinced myself it's better to just run curl
in a container.我现在应该能够执行
curl
到172.20.0.2:8080
并且没问题,但我没有尝试它,因为那时我说服自己最好在容器中运行curl
。
I actually don't know why there are two IP addresses there like that, but some rabbits just make a turn in the hole that you don't follow you know?我其实不知道为什么那里有两个 IP 这样的地址,但是有些兔子只是在你不跟随的洞里转弯你知道吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.