Unable to access docker-compose containers created inside docker

强颜欢笑 提交于 2020-05-25 06:11:56

问题


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"

Super simple stuff. If I run docker-compose up and run on my local machine:

echo "Hello World" | nc 127.0.0.1 8800

Then I see the echo. However: If I run this same compose scenario inside a docker container, through the GitLab runner, it fails.

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

My .gitlab-ci.yml file looks like:

---

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

And the output of gitlab-ci-multi-runner exec docker --docker-privileged docker_test is:

$ 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

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 

Edit: 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.


回答1:


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

If I'm not wrong, its host name is docker. So access as this:

echo "Hello world" | nc docker 8800

To figure out what is the host of the docker daemon, use this:

script:
  - echo $DOCKER_HOST


来源:https://stackoverflow.com/questions/45389116/unable-to-access-docker-compose-containers-created-inside-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!