Cannot connect to the Docker daemon in GitLab CI — Common Causes and Solutions
GitLab CI is a popular continuous integration and delivery platform that is often used to build, test, and deploy code changes. One common error that users may encounter when using GitLab CI is the “Cannot connect to the Docker daemon” error. This error occurs when GitLab CI is unable to connect to the Docker daemon, which is the process that manages Docker containers on the system.
--
You are trying to run Docker in your GitLab CI pipeline, and the following error occurs:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
There are several potential causes for this error, and the solution will depend on the root cause. Here are a few common causes and solutions:
The Docker daemon is not running
This is the most obvious cause of the error, but it can be confusing why this is happening.
If your pipeline looks something like this:
some-job:
image: docker
script:
- docker --version
- docker pull alpine
You may think that pulling an image or building an image should work without issues. Right? Wrong. By specifying the image docker, you have just started a Docker client but are missing the Docker daemon, which does the heavy lifting. So let’s amend the pipeline with a service:
some-job:
image: docker
services:
- docker:dind
script:
- docker --version
- docker pull alpine
Docker Daemon listens on the wrong line
By default, the Docker daemon listens on a Unix socket located at /var/run/docker.sock
. Depending on how the GitLab Runner is configured, this is not always the case.
If the Docker daemon is not listening on the default Unix socket, you will need to update the GitLab CI configuration to point to the correct socket or port.
It is possible that the Docker daemon has been configured to listen on a different Unix socket or a network port. In most cases, adding the following configuration…