How to run docker container inside docker (DIND)?

4.9k views Asked by At

I am trying to run a container inside another container using Docker inside docker https://hub.docker.com/_/docker.

When I run the following:

docker run --privileged docker:stable-dind docker run hello-world

I get the following error message:

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

I must be missing something, how can I run docker inside docker?

1

There are 1 answers

1
sauerburger On

I don't think you can do this in a one-liner (others might correct me). However, as explained at hub.docker.com/_/docker/, you can start the a docker-in-docker container as a background daemon (-d) and then start other containers inside.

To start the parent container, run

docker run -d --name some-docker --privileged docker:stable-dind

The name some-docker is arbitrary. It will be used to identify this container later on. To start a container inside, run

docker run  --link some-docker:docker docker run hello-world

The --link option exposes the network ports of the parent container and sets environment variables, such that the inner container uses docker from the dind image.