无法访问在docker内部创建的docker-compose容器 [英] Unable to access docker-compose containers created inside docker
问题描述
我有一个 docker-compose.yml
文件,该文件在端口 8800
上启动了简单的HTTP回显服务。
I have a docker-compose.yml
file that starts up a simple HTTP echo service on port 8800
.
version: '2'
services:
echo-server:
image: luisbebop/echo-server
container_name: echo-server
ports:
- "8800:8800"
超级简单的东西。如果我运行 docker-compose up
并在本地计算机上运行:
Super simple stuff. If I run docker-compose up
and run on my local machine:
echo "Hello World" | nc 127.0.0.1 8800
然后我看到了回声。 但是::如果我通过GitLab运行程序在docker容器中运行相同的撰写方案,则会失败。
Then I see the echo. However: If I run this same compose scenario inside a docker container, through the GitLab runner, it fails.
我试图获得请在此处的GitLab上关注此问题,但结果有限: https:// gitlab。 com / gitlab-org / gitlab-ce / issues / 26566
I've tried to garner attention for this issue at GitLab here, but with limited results: https://gitlab.com/gitlab-org/gitlab-ce/issues/26566
我的 .gitlab-ci.yml
文件如下所示:
---
stages:
- docker_test
services:
- docker:dind
docker_test:
stage: docker_test
image: docker:latest
script:
- docker version
- apk update
- apk add py-pip
- pip install docker-compose
- docker-compose up -d
- sleep 10
- netstat -tulpn
- docker-compose port echo-server 8800
- echo "Hello world" | nc 127.0.0.1 8800
和 gitlab-ci-multi-的输出Runner exec docker --docker-privileged docker_test
是:
$ netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
$ docker-compose port echo-server 8800
0.0.0.0:8800
$ echo "Hello world" | nc 127.0.0.1 8800
ERROR: Build failed: exit code 1
这些端口在docker容器内部不可用,该容器负责docker-compose服务。我做错了吗?
So it seems like these ports aren't available inside the docker container, which is in charge of the docker-compose services. Am I doing something wrong?
我不希望端口在主机级别公开,而我只希望打开端口
I do not want the ports exposed at the host level, rather I just want the ports opened by the docker container running the build, available to that container.
Host (My Mac) -> GitLab CI Container -> Docker Compose Container exposing 8800
^ Not here ^ I want port 8800 accessible here
编辑:此外,如果我将外壳连接到正在运行的CI容器,则会看到:
Further, if I attach a shell to the running CI container, I see:
/ # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab192edf5872 luisbebop/echo-server "/opt/echo-server/..." 2 minutes ago Up About a minute 0.0.0.0:8800->8800/tcp echo-server
/ # netstat -nltup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
因此它看到了容器,并且端口映射看起来还可以,但是端口不是
So it "sees" the container, and the port mappings look okay, but the port isn't actually listening.
推荐答案
容器在 docker主机 上运行,即情况,t其他支持Gitlab构建的容器:
The container is running on the "docker host" which is, in your case, the other container that is supporting the Gitlab build:
services:
- docker:dind
如果我没记错的话,它的主机名是 docker
。因此访问如下:
If I'm not wrong, its host name is docker
. So access as this:
echo "Hello world" | nc docker 8800
要弄清楚docker守护进程的主机是什么,请使用以下命令:
To figure out what is the host of the docker daemon, use this:
script:
- echo $DOCKER_HOST
这篇关于无法访问在docker内部创建的docker-compose容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!