Docker Hub cache
Pull Docker Hub images through a cache on the host, so containers don’t each download the same images. This guide builds on Docker-in-Docker.
Run the cache
The cache is a registry in pull-through mode, running on the host. Use the files in examples/services/registry-mirror, and start it with docker compose up -d in that directory:
compose.yaml
# Docker Hub pull-through cache for the Docker Hub cache guide.
# Run: docker compose up -d
services:
registry-mirror:
image: registry:3
# Port 5000 on all host interfaces, so Isodev containers can reach it
# through their network's gateway.
network_mode: host
environment:
OTEL_TRACES_EXPORTER: none # registry 3 exports traces to localhost:4318 by default
volumes:
- ./config.yml:/etc/distribution/config.yml:ro
- registry-mirror:/var/lib/registry
restart: unless-stopped
volumes:
registry-mirror:config.yml
version: 0.1
log:
level: info
storage:
# Required in proxy mode: expired cache entries are removed after proxy.ttl.
delete:
enabled: true
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/registry
http:
addr: 0.0.0.0:5000
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
proxy:
# Docker Hub only, pulled anonymously, so it serves public images only.
remoteurl: https://registry-1.docker.io
ttl: 168hSet up
Use the files in examples/guides/docker-hub-cache:
~/.config/isodev/config.yaml
# yaml-language-server: $schema=https://isodev.dyszkiewicz.me/config.schema.json
# A private Docker daemon in each container that pulls Docker Hub images through
# a cache on the host, on top of the Docker-in-Docker guide.
image: isodev-docker-hub-cache: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
environment:
# Port of the Docker Hub pull-through cache on the host.
DOCKER_MIRROR_PORT: "5000"
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, and iproute2 to find the host's
# address for the cache. 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 iproute2 \
&& 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
# Pull Docker Hub images through the cache on the host, which is this
# container's default gateway. The daemon falls back to Docker Hub when the
# cache is unreachable.
gateway="$(ip -4 route show default | awk '{print $3; exit}')"
dockerd --storage-driver="$driver" --registry-mirror="http://$gateway:$DOCKER_MIRROR_PORT" \
</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
fiThe inner daemon reaches the cache through the container’s network gateway, which is the host. When the cache is unreachable, it pulls from Docker Hub directly.
Use it
isodev up --recreate
isodev exec docker info --format '{{.RegistryConfig.Mirrors}}'
isodev exec docker run --rm hello-world
curl -s http://localhost:5000/v2/_catalogThe catalog lists the images the cache holds, such as library/hello-world. Cached images expire after proxy.ttl in config.yml.
Trade-offs
- Only Docker Hub is cached. Images from other registries, such as
ghcr.io, are pulled directly. - Only public images. The cache pulls anonymously.
- The cache is reachable on your network. It listens on port 5000 of every host interface over plain HTTP, and anyone who can reach that port can pull through it.
Last updated on