dockergitlab-cidocker-in-docker

Connecting to detached container in gitlab CI pipeline


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:

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


Solution

  • 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.