westfall
westfall

Reputation: 383

Preparation failed: Cannot connect to the Docker daemon at unix:///var/run/docker.sock

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?

Upvotes: 23

Views: 38301

Answers (5)

Sergey Kopanev
Sergey Kopanev

Reputation: 1504

On macOS what helped me — was enabling back all items related to Docker in "Login items" in System Preferences. 🫢

After that I restarted Mac and everything is working fine.

And yes, on macOS, you have to have the Desktop Docker app (or install many brew tools).

Upvotes: 0

Thomas Greulich
Thomas Greulich

Reputation: 1

We had the problem that we didn't register the gitlab-runner with "sudo". So sometime it runs an other times not.

Upvotes: 0

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

Upvotes: 3

Ryabchenko Alexander
Ryabchenko Alexander

Reputation: 12380

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

Upvotes: 7

Adiii
Adiii

Reputation: 59946

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

Upvotes: 11

Related Questions