'How to fix the error "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running"
I am trying to spin up a docker container inside another docker container and I get this error:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running
I get this error when I try to build the images using docker build -t dind .
And here's my Dockerfile
looks like:
FROM docker:18.09.0-dind
RUN docker network create gitlab-runner-net
RUN docker run -d \
--name gitlab-dind \
--privileged \
--restart always \
--network gitlab-runner-net \
-v /var/lib/docker \
docker:17.06.0-ce-dind \
--storage-driver=overlay2
RUN mkdir -p /srv/gitlab-runner
RUN touch /srv/gitlab-runner/config.toml
Can someone tells me why this error is coming and how to fix this?
Solution 1:[1]
I don't know if this gonna help you, but you can use docker-compose instead:
Exemple of docker-compose.yml
version: '3.5'
services:
docker:
image: docker:18.09.0-dind
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /var/lib/docker
entrypoint: /bin/sh
command: -c "docker network create gitlab-runner-net;
docker run -d --name gitlab-dind --privileged
--restart always --network gitlab-runner-net
-v /var/lib/docker docker:17.06.0-ce-dind"
Then docker-compose up -d
Starting dk_docker_1 ... done
Attaching to dk_docker_1
docker_1 | 66fdfc18e290814cff7b1d8cb75acf575e22cc5b4d3478e0274b82043f0d493c
docker_1 | e3a2deb36d8a8b71aef86b2a73455a2a1cd5fa99c4e961a125421ca7aefd2e14
dk_docker_1 exited with code 0
Then on the host:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3a2deb36d8a docker:17.06.0-ce-dind "dockerd-entrypoint.…" 45 seconds ago Up 44 seconds 2375/tcp gitlab-dind
then run docker inside docker:
docker exec -it 05e7f866382b docker run -d alpine sleep 300
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
801bfaa63ef2: Pull complete
Digest: sha256:3c7497bf0c7af93428242d6176e8f7905f2201d8fc5861f45be7a346b5f23436
Status: Downloaded newer image for alpine:latest
06a481b6692d6e4ef19ca4bd95ddc403c08a828817dc66092343ddd3e0690f27
docker exec -it 05e7f866382b docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06a481b6692d alpine "sleep 300" 12 seconds ago Up 12 seconds peaceful_snyder
This is just an example you will have to properly setup networks, volumes etc ...
Solution 2:[2]
I had this problem but in using docker in datagrip but finally found that the problem is from permission if you start with sudo problem will be solved or if you want to use only docker I recommand seeing this https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04 and there is ugly way to solve and that is giving permission to docker.sock using this command sudo chmod 777 /var/bin/docker.sock I hope it was helpfull
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dr Claw |
Solution 2 | Ali Sattarzadeh |