Set up a GitLab docker runner on your Mac

11 Apr, 2022GitLab, Docker

You can set up a normal docker runner on your Mac by following the official guide. However if you need to run docker command such as building your own docker image with docker build within the GitLab CI job (Docker in Docker), you might run into errors like:

Error response from daemon: Get "https://registry.******/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

or

error during connect: Post "http://docker:2375/v1.24/auth": dial tcp: lookup docker on 192.168.*.*:53: no such host

So let's look at the way to set up and register a docker runner with the host's docker socket mounted to it.

Step 1

Go to https://<gitlab-host>/<namespace>/<project>/-/settings/ci_cd#js-runners-settings and copy the URL and registration token:

Step 2

Start the gitlab/gitlab-runner container:

docker run -d --name gitlab-runner --restart always \
  -v /Users/Shared/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

And make sure the gitlab-runner container is up and running:

docker ps

Step 3

Register the runner:

docker exec -it gitlab-runner \
  gitlab-runner register \
  --non-interactive \
  --url $replace_this_with_the_URL_copied_in_step_1 \
  --registration-token $replace_this_with_the_TOKEN_copied_in_step_1 \
  --name $HOST \
  --executor docker \
  --docker-image "docker:latest" \
  --docker-privileged \
  --docker-volumes /var/run/docker.sock:/var/run/docker.sock

You should see output like these:

Registering runner... succeeded                     runner=******
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

And on your Gitlab settings/ci_cd#js-runners-settings page you should see the newly registered runner:

Step 4

Now we can define a new job in the .gitlab-ci.yml to test out our new docker runner:

test:
  image: docker
  script:
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY

And your job should be executed successfully:

Step 5

Now you're good to go with your new runner, but if you need to tweak the configuration, you can edit the /Users/Shared/gitlab-runner/config/config.toml file:

Save the file and restart the gitlab-runner container to apply the changes.


Powered by Gatsby. Theme inspired by end2end.

© 2014-2022. Made withby mdluo.