Running Docker in Docker

It is sometimes useful to be able to launch a container that can launch Docker tasks itself. This is done by installing Docker in a Docker container (Docker in Docker). It also involves mounting the host’s Linux sockets in the Container. Docker CLI itself uses the Docker socket to communicate with the Docker Engine.

This socket is located at this location:

This command returns:

Docker Engine is not actually executed inside the container: the Docker CLI is executed inside the container and communicates with the host to perform Docker tasks.

Let’s create a build script :

The content of this file is:

Let’s give the execution rights to this script:

We want to deposit the Docker images created on Hub Docker. To do so, we introduce two environment variables for credentials ( and ). The name of the image, including the repository, the name of the image and the tag are specified in the script parameter ().

This script can be improved by adding the execution of tests when running the container.

Let’s create a Dockerfile to build the Docker image of builder:

  • it is based on ,
  • it includes the buid script ,
  • This script is run at each container launch with the command.

Let’s create a with the following contents:

Let’s create this image, called :

The output of the command is:

Let’s define the environment variables for Hub Docker credentials:

Let’s create a directory corresponding to the application:

Let’s create an that we want to build in the Docker container of the builder:

Now let’s run the docker-builder image which will itself run the command in the subdirectory to generate a new image based on the file.

First of all, let’s check that the image we are going to create is not present on the host:

This command does not return any result.

Now let’s run the build:

The result is:

The Docker socket is mounted with the argument .

Let’s check that the Docker image is now present on the host:

This time, we find this image locally:

By connecting to Hub Docker, you can find this image:

Under Windows, instead of using the socket, you must use a pipe named using the argument .

So we were able to generate a Docker image on the host from a Docker container dedicated to the build.

Interests in the full lifecycle: design, Agile Coaching, development, testing, DevOps, Cloud, Management 3.0, ITIL. It defines me.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store