I'm running a GitLab CI/CD pipeline with a single GitLab Runner. My gitlab-ci.yml includes extra hosts, which seem to be applied correctly, but I’m encountering an issue during the Docker login step.
Here’s the relevant part of my gitlab-ci.yml:
stages:
- build
default:
tags:
- shared
variables:
DOCKER_IMAGE: "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
build:
stage: build
image: docker:latest
services:
- docker:dind
script:
- cat /etc/hosts
- ping -c 4 10.10.30.2
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
- docker build -t $DOCKER_IMAGE .
- docker push $DOCKER_IMAGE
only:
- branches
And the config.toml for the runner contains this extra_hosts configuration:
extra_hosts = ["gitlab.example.com:10.10.30.2", "registry.gitlab.example.com:10.10.30.2"]
The pipeline output confirms that the host entries are set correctly:
10.10.30.2 gitlab.example.com
10.10.30.2 registry.gitlab.example.com
However, when attempting to run docker login, I get the following error:
Error response from daemon: Get "https://registry.gitlab.example.com/v2/": dial tcp: lookup registry.gitlab.example.com on 193.110.81.0:53: no such host
It seems like Docker isn’t recognizing the extra_hosts entry and is trying to resolve the registry URL with an external DNS server instead.
How can I ensure Docker uses the correct host configuration set in extra_hosts for my registry URL?
Solution
Based on an online solution (thanks Aroon), this fix resolves the issue of Docker ignoring the hosts file inside the GitLab Runner Build-Container by adding the following line to the /etc/hosts file on the Docker host.
10.10.30.2 registry.gitlab.example.com
It’s not the most ideal solution but serves as a working workaround for now.