How to install docker in docker container?

2019-06-16 09:54发布

问题:

This is my Dockerfile:

FROM golang
# RUN cat /etc/*release
RUN apt-get update
RUN apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

RUN apt-get update
RUN apt-get -y install docker-ce
RUN docker run hello-world

The golang Dockerfile is official, it bases on the

Debian GNU/Linux 8 (jessie)

So I wrote down this Dockerfile by checking the install steps from Docker Install Tutor(Debian)

But the output is

Step 8/8 : RUN docker run hello-world
 ---> Running in b183b8cc5d10
docker: Cannot connect to the Docker daemon at 
unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

How to solve this problem? I want to establish docker containers in the host docker container.

回答1:

Use Docker-in-Docker for this task. They have already solved many of the problems for you.



回答2:

I had a similar problem trying to install Docker inside a Bamboo Server image. To solve this:

  1. first remove the line: RUN docker run hello-world from your Dockerfile
  2. The simplest way is to just expose the Docker socket, by bind-mounting it with the -v flag or mounting a volume using Docker Compose:

docker run -v /var/run/docker.sock:/var/run/docker.sock ...



回答3:

Try with starting docker service before of executing any docker command. Add this line

RUN bash service docker start

to your Dockerfile above of this line:

RUN docker run hello-world 


回答4:

The easiest way is to use the official Docker-in-Docker images from https://hub.docker.com/_/docker/ with the :dind tag (which is the successor of the project Hendrikvh already mentioned).

You definitely need to use the --priviledged flag also:

docker run --privileged --name yourDockerContainerNameHere -d docker:dind

With that your Docker-in-Docker experiments should work - but be aware of the many stumbleblocks that could be in your way: https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/