Docker in a Docker (DinD)

Internal reference: topics/04-1.md

Introduction

Here are a few use cases to run docker inside a docker container.


How it works

DinD creates a child container inside a Docker container. For this, you need either an official docker image with dind tag. The dind image is baked with the required utilities for Docker to run inside a docker container. Or you build it with a Dockerfile with more control for all added packages and configuration (see Exercises in this course)

Fig-01: Docker in Docker container


Example with ready-to-use image

(1) Create a container named i.e. dind-test with docker:dind image from Docker-Hub.

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

(2) Log in to the container using exec.

docker exec -it dind-test /bin/sh

(3) When you list the docker images, you should see the Ubuntu image along with other docker images in your host.

docker images

(4) Now create a Dockerfile inside the test directory.

mkdir test && cd test

(5) Create a Dockerfile

nano Dockerfile

(6) Copy the following Dockerfile contents to test the image build from within the container.

FROM ubuntu:18.04
LABEL maintainer=Hans Muster <hmuster@example.com>
RUN apt-get update && \
    apt-get -qy full-upgrade && \
    apt-get install -qy curl && \
    curl -sSL https://get.docker.com/ | sh

(7) Build the Dockerfile

docker build -t test-image .

Daniel Garavaldi