Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

dockerfile - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...