2

The issue is I get the curl: (7) Failed to connect to localhost port 8090: Connection refused GItLab CI error but this does not happen on my laptop where I get the source html of the webpage. The .gitlab-ci.yml below is a simple reproduction of the issue. I have spent numerous hours trying to figure this out - i'm sure someone else has also.

GitLab Docker Runner Verbose

Aside: This isn't a similar question - since they don't offer a solution.

GitLab Repo: https://gitlab.com/mudassir-ahmed/wordpress-testing-with-gitlab-ci/tree/another-approach but the only file it contains is the .gitlab-ci.yml shown below...

image: docker:stable

variables:
  # When using dind service we need to instruct docker, to talk with the
  # daemon started inside of the service. The daemon is available with
  # a network connection instead of the default /var/run/docker.sock socket.
  #
  # The 'docker' hostname is the alias of the service container as described at
  # https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services
  #
  # Note that if you're using the Kubernetes executor, the variable should be set to
  # tcp://localhost:2375/ because of how the Kubernetes executor connects services
  # to the job container
  # DOCKER_HOST: tcp://localhost:2375/
  #
  # For non-Kubernetes executors, we use tcp://docker:2375/
  DOCKER_HOST: tcp://docker:2375/
  # When using dind, it's wise to use the overlayfs driver for
  # improved performance.
  DOCKER_DRIVER: overlay2

services:
  - docker:dind

before_script:
  - docker info

build:
  stage: build
  script:
    - apk update
    - apk add curl
    #- hostname -i
    - docker container ls
    - docker run -d -p 8090:80 --name nginx-server kitematic/hello-world-nginx
    - curl localhost:8090 # This works on my laptop but not on a GitLab runner.

Mudassir
  • 1,136
  • 1
  • 11
  • 29

2 Answers2

2

Referring to the answer from here : gitlab-ci.yml & docker-in-docker (dind) & curl returns connection refused on shared runner

There are two ways to fix this :

Option 1: Replace localhost in curl localhost:8090 with docker like this curl docker:8090

Option 2:

services:
  - name: docker:dind
    alias: localhost
Blackout
  • 348
  • 3
  • 11
  • Option 2 doesn't work for me, option 1 is also recommended [here](https://stackoverflow.com/questions/41559660/gitlab-ci-runner-not-able-to-expose-ports-of-nested-docker-containers). – Ambroise Rabier May 04 '23 at 15:20
  • Option 2 doesn't work for me either. I use `$DOCKER_HOST` as suggested [here](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1909) – Gary Jul 27 '23 at 14:50
0
 docker run -d -p 8090:80 --name nginx-server kitematic/hello-world-nginx
 curl localhost:8090 # This works on my laptop but not on a GitLab runner.

Assuming that is your code i think that you should somehow add some timeout between docker run and curl.

I have similar issues some time ago after starting docker container on gitlab runner machine i wasnt able to accces my url to. When i added command which check if container is running for " about one minute " it resolved my problem.

"docker inspect -f {{.State.Running}} " + containerName" but in order to do that check, you should add some additional script

marslfc
  • 1
  • 2
  • hi, so you are proposing that I should add something like ```sleep 60``` before the curl command? – Mudassir Jul 16 '19 at 15:02
  • you can try and if that help just create simple bash script which will check if docker container running for approx 1minute – marslfc Jul 16 '19 at 15:04
  • hope that this is the same issue, because if you are wait after run container manually, localhost is available ? – marslfc Jul 16 '19 at 15:06
  • thats not the issue.. tried the pause and used docker inspect and the container state is running – Mudassir Jul 16 '19 at 15:11
  • so when you login to gitlab runner machine and try to use curl -v localhost:8090 in command line/terminal what result do you get ? – marslfc Jul 16 '19 at 15:13
  • ill post a picture... but its the same thing – Mudassir Jul 16 '19 at 15:15
  • try to find the IP that your docker instance is using by: $ docker-machine ip default Now that you know the IP address, try curl again. You can even have it evaluated within the command like so: $ curl http://$(docker-machine ip default):4000 – marslfc Jul 16 '19 at 15:19
  • I have done that in the past and still the same. Any particular reason u used port 4000 in your comment? – Mudassir Jul 16 '19 at 15:31