Jay Vyas
Jay Vyas

Reputation: 2712

Cannot connect to the Docker daemon at unix:///var/run/docker.sock.( Gitlab )

I have a AWS instance with Docker installed on it. And some containers are running.I have setup one Laravel project inside docker.

I can access this web application through AWS IP address as well as DNS address(GoDaddy).

I have also designed gitlab CI/CO to publish the code to AWS instance.

When I try to push the code through Gitlab pipelines, I am getting following error in pipeline.

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

I checked the docker, it is running properly. Any clues please.

.gitlab-ci.yml

http://pastie.org/p/7ELo6wJEbFoKaz7jcmJdDp

the pipeline failing at deploy-api-staging: -> script -> scripts/ci/build

build script

http://pastie.org/p/1iQLZs5GqP2m5jthB4YCbh

deploy script

http://pastie.org/p/2ho6ElfN2iWRcIZJjQGdmy

Upvotes: 0

Views: 2019

Answers (1)

Daniel Campos Olivares
Daniel Campos Olivares

Reputation: 2594

From what I see, you have directly installed and registered the GitLab runner on your EC2 instance.

I think the problem is that you haven't already given permissions to your GitLab Runner user to use Docker.

From the official Docker documentation:

The Docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The Docker daemon always runs as the root user.

If you don’t want to preface the docker command with sudo, create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group.

Well, GitLab Runners use the user gitlab-runner by default when they're running any CI/CD Pipeline and that user won't use sudo (neither it should be in the sudoers file!) so we have to correctly configure it.

First of all, create a Docker group on the EC2 where the GitLan Runner is registered:

sudo groupadd docker

Then, we are going to add the user gitlab-runner to that group:

sudo usermod -aG docker gitlab-runner

And we are going to verify that the gitlab-runner user actually has access to Docker:

sudo -u gitlab-runner -H docker info

Now your Pipelines should be able to access without any problem to the Unix socket under unix:///var/run/docker.sock.

Additional Steps if using Docker Runners

If you're using the Docker executor in your runner, you have to now mount that Unix socket on the Docker image you're using.

[[runners]]
    url = "https://gitlab.com/"
    token = REGISTRATION_TOKEN
    executor = "docker"
    [runners.docker]
        tls_verify = false
        image = "docker:19.03.12"
        privileged = false
        disable_cache = false
        volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]

Take special care of the contents in the volume clause.

Upvotes: 1

Related Questions