20

I started a docker container gitlab-ci-runner, and then register a runner using docker as executor, using node:latest as docker images. But when i push commit to gitlab,I got this error:

Running with gitlab-runner 11.3.1 (0aa5179e)
  on docker-ci 0f9fe2c4
Using Docker executor with image node:latest ...
ERROR: Preparation failed: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? (executor_docker.go:1150:0s)

Here is my gitlab config.toml:

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "docker-ci"
  url = "http://gitlab.xxxxxx.com/"
  token = "0......fc5"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "node:latest"
    privileged = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

I start the container using:

sudo docker run -d --name gitlab-runner --restart always \
 -v ~/srv/gitlab-runner/config:/etc/gitlab-runner \
 -v ~/var/run/docker.sock:/var/run/docker.sock \
 gitlab/gitlab-runner:latest

and register using:

sudo docker run --rm -t -i -v ~/srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register

I'm new to docker, can't find the error reason.Is there someone who can help me?

westfall
  • 343
  • 1
  • 3
  • 8

3 Answers3

10

As your CLI container or gitlab-ci-runner container need to mount the host machine's Docker socket in the container. This will allow your container to use the host machine's Docker daemon to run containers and build images.

You just need to modified run command of gitlab-ci-runner.

docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest
Adiii
  • 54,482
  • 7
  • 145
  • 148
7

I my case there was no docker on my machine. Here is doc for intalation https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-docker-ce

sudo apt-get install docker-ce docker-ce-cli containerd.io
Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88
  • 1
    Same here, though as of 2019 Linux prompted me to execute this command **sudo snap install docker** After that was done I could get Gitlab runner to run on a commit/push. – CAMD_3441 Aug 17 '19 at 12:23
  • Hi @RyabchenkoAlexander please help me on the solution please check the link :- https://stackoverflow.com/q/64587625/3946958 – Ravindra Kushwaha Oct 30 '20 at 06:30
2

As mentioned by @Adiii. the difference lies in:

  1. the way docker-container is launched volume mount needs to be changed from ~/var/run/docker.sock:/var/run/docker.sock or /srv/run/docker.sock:/var/run/docker.sock to /var/run/docker.sock:/var/run/docker.sock
  2. And config.toml
[[runners]]
  [runners.docker]
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]

Where "/var/run/docker.sock:/var/run/docker.sock" is important change.

Best described in issue discussion here: https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1986

  • 1
    Is this the same on windows? I suppose /var/run/docker.sock is some installation-specific location of some Docker feature on the host machine. – Adam Bajger Oct 22 '21 at 15:01
  • 1
    Yes that’s correct it is installation specific and gitlab runner container uses this package to further spawn new containers for pipeline stages. Not sure about windows how it works there. – JackOfAshes - Mohit Gawande Oct 22 '21 at 15:04