Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Docker inside Docker container: Cannot connect to the Docker daemon

I created a Dockerfile to run Docker inside Docker:

    FROM ubuntu:16.04
RUN apt-get update && \
    apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common && \
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - &&\
    apt-key fingerprint 0EBFCD88

RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && \
   apt-get update && \
   apt-get install -y docker-ce && \
   systemctl enable docker

After i launched my container and run docker ps i got: "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"

i executed the command dockerd inside my container resulted:

Error starting daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables v1.6.0: can't initialize iptables table `nat': Permission denied (you must be root) Perhaps iptables or your kernel needs to be upgraded. (exit status 3)

Please advise

like image 816
Amir Avatar asked Dec 14 '22 13:12

Amir


2 Answers

The recommendation I received for this was to use the -v parameter in docker run to map the docker socket between containers like this:

-v /var/run/docker.sock:/var/run/docker.sock
like image 75
Kedar Avatar answered Jan 18 '23 22:01

Kedar


If you really want to run a Docker container inside an other Docker container, you should use already existing images provided by Docker (https://hub.docker.com/_/docker) instead of creating your own base image : choose images tagged as dind (docker in docker) or <docker_version>-dind (like 18.09.0-dind). If you want to run your own image (not recommended though), don't forget to run it with --privileged option (that's why you get the error).

Example with docker official images :

# run Docker container running Docker daemon
docker run --privileged --name some-docker -d docker:18.09.0-dind

# run hello-world Docker image inside the Docker container previously started
docker exec -i -t some-docker docker run hello-world

Nevertheless, I agree with @DavidMaze comment and the reference blog post he referred to (Do not use Docker-in-Docker for CI) : Docker-in-Docker should be avoided as much as possible.

like image 38
norbjd Avatar answered Jan 18 '23 23:01

norbjd