Skip to content
Docker-in-Docker

Docker-in-Docker

Give each container its own Docker daemon, so builds, tests and Compose stacks in one workspace don’t affect the others or the host’s Docker.

Set up

Use the files in examples/guides/docker:

~/.config/isodev/config.yaml
# yaml-language-server: $schema=https://isodev.dyszkiewicz.me/config.schema.json
# A private Docker daemon in each container.
image: isodev-docker:latest
build:
  context: image
# Required by the inner Docker daemon.
privileged: true
volumes:
  # A private volume for each container's /var/lib/docker, on Docker's own disk,
  # so the inner daemon can use overlay2. It's kept by isodev up --recreate and
  # removed by isodev down.
  - type: volume
    target: /var/lib/docker
lifecycle:
  on_start:
    scripts:
      - file: hooks/docker.sh
~/.config/isodev/image/Dockerfile
FROM ubuntu:24.04
ARG DEBIAN_FRONTEND=noninteractive
# mise installs tools to ~/.local/share/mise and puts them on PATH through its
# shims. /usr/local/bin comes first, so files mounted there win.
ENV PATH=/root/.local/bin:/usr/local/sbin:/usr/local/bin:/root/.local/share/mise/shims:/usr/sbin:/usr/bin:/sbin:/bin
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates curl git \
    && rm -rf /var/lib/apt/lists/*

# mise, which installs the tools.
RUN curl -fsSL https://mise.run | MISE_INSTALL_PATH=/usr/local/bin/mise sh

# Docker Engine with Buildx and Compose. The daemon is a system package, so it
# comes from apt.
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        docker.io docker-buildx docker-compose-v2 iptables \
    && rm -rf /var/lib/apt/lists/*
~/.config/isodev/hooks/docker.sh
#!/bin/sh
# Runs whenever Isodev creates or starts the container.
set -eu
# Start the inner Docker daemon if it isn't running.
if ! docker info >/dev/null 2>&1; then
  mkdir -p /var/log/isodev
  # /var/run survives a container restart. Stale PID files would make dockerd
  # wait for a containerd that is no longer running.
  rm -f /var/run/docker.pid /var/run/docker/containerd/containerd.pid
  # A volume mounted at /var/lib/docker supports overlay2. Fall back to the
  # slower vfs driver without it.
  driver=vfs
  if mountpoint -q /var/lib/docker; then
    driver=overlay2
  fi
  dockerd --storage-driver="$driver" </dev/null >/var/log/isodev/dockerd.log 2>&1 &
  attempt=0
  until docker info >/dev/null 2>&1; do
    attempt=$((attempt + 1))
    if [ "$attempt" -ge 60 ]; then
      echo "dockerd did not start; recent log:" >&2
      tail -n 30 /var/log/isodev/dockerd.log >&2
      exit 1
    fi
    sleep 1
  done
fi

Each container’s images and containers are stored in a private Docker volume, on Docker’s own disk, where the inner daemon can use the overlay2 storage driver. isodev up --recreate keeps them, isodev up --recreate --renew-volumes empties them, and isodev down deletes them. List the volumes with docker volume ls --filter label=isodev.managed=true.

Use it

isodev up --recreate
isodev exec docker run --rm hello-world

Each inner daemon starts with an empty image store, so every container downloads the images it uses. To keep one copy of each Docker Hub image on the host, add a Docker Hub cache.

Trade-offs

  • Containers are privileged. The inner daemon needs privileged: true, which gives the container broad access to the host. See Security.
Last updated on