Ihave configured gitlab in my ubunutu machine, I have also registered runner(docker) in my local machine, So when the pipeline run. I’m getting this error
fatal: unable to access 'http://127.0.0.1/gitlab-instance-0daf0d62/car-assembly-line.git/': Failed to connect to 127.0.0.1 port 80 after 0 ms: Connection refused
ERROR: Job failed: exit code 1
I have setup gitlab in my machine and registered the runner(docker) in the same machine, so when I triggeer the pipeline build is failing
2
Answers
Firstly, I would discourage you from running the runner on the same machine as you’re running your Gitlab instance (many reasons, mainly security)
On the topic of your problem – when running gitlab runner with the docker executor you’re running the runner itself (not just the CI jobs, but the "manager" too) in a docker container.
For that docker container "localhost" means itown local (virtual) interface.
If we look at my development machine I have 3 interfaces;
lo
which is loopback, also known aslocalhost
ens18
which is my "public" interface, exposed to the network to which the machine is connecteddocker0
which is the default virtual docker interfaceIf I start up an nginx container, bind it to port
80
andcurl localhost
from the host machine I will get the default nginx response.I truncated the content as it’s irrelevant, just know that we reached the nginx container.
If we now look at the nginx logs we’ll see two reqests coming from two different addresses, the first one from our host machine and the second one coming from the container itself.
If I start up another container, this time httpd (aka. Apache2) for example, and run
curl localhost
inside it, just like I did with nginx, you see that we get the Apache’s default page, not the one from nginx.And as you can see running
curl localhost
returns the webpage contents of the default httpd page.If we wanted to get the nginx page from the httpd container we would have to first find the internal address of the host machine in the docker network. Remember the
docker0
interface of the host machine? We’ll need it now.At the very end of the third line, where
docker0
is listed you can see the address of this interface –172.17.0.1
.if we
curl 172.17.0.1
from the httpd container, instead of getting the default apache page, we get the nginx page.If we curled
10.10.10.117
– which is the "public" address of the host machine – we’d get the same result.What have we learned?
"localhost" is very relative term. It (in vast majority of situations) means the environment you’re currently in. For containers it means the container itself, not the host.
What you have to do is instead of connecting with your runner to
http://localhost
, you should connect to your machine’s internal docker address (if you read the post you know how to get it).You could also tell it to connect to your public address.
All of this, of course, only matters if you’re running the gitlab-runner on the same machine as your gitlab instance. If you were running the runner on another machine you’d have to use the public address anyway, so you wouldn’t have this problem in the first place.
Try changing "localhost" to "host.docker.internal"
When the runner is using the docker executor it starts a docker container that runs your jobs.
Inside the container the localhost refers to the container’s IP address which is not the same one as the host’s IP address. In order to reach the host you’ll need to use "host.docker.internal" (make sure that the host is accessible through localhost in the desired port).