Docker in a sandbox

Run Docker inside a Linux sandbox, and keep its build cache across snapshots and forks

A Linux sandbox is a whole virtual machine, so Docker runs inside it as it would on a server of your own: build images, run containers and use Compose, as root, with nothing to ask Pod for. Docker is in Pod's Linux images.

TypeScript
const sandbox = await pod.sandboxes.create({ system: "linux", size: "medium" });
const built = await sandbox.run("cd /work/app && docker build -t app .");
if (built.exitCode !== 0) throw new Error(built.stderr);

The containers share the sandbox's kernel, memory and disk. They are not kept apart from each other the way two sandboxes are: see Isolation and secrets.

Keep the build cache

Docker keeps its layers and its build cache on the sandbox's disk. The disk outlives sleep and stops, and every snapshot and fork carries it, so a build made once is cached for every copy made after it.

  • Sleep instead of delete. A sandbox that wakes has its images and cache as they were.
  • Snapshot after a warm build. Build once, take a snapshot, and start each new sandbox from it: its first build reuses every unchanged layer.
  • Fork for parallel work. A fork copies the disk as it is at that moment, so up to 10 copies start with the same warm cache, and each builds on from there without touching the others.

With BuildKit, keep the cache in a folder on the disk so that a snapshot or fork carries it whatever builder you use:

docker buildx build \
  --cache-from type=local,src=/var/cache/buildx \
  --cache-to type=local,dest=/var/cache/buildx,mode=max \
  -t app .

Size and disk

Images and their cache count against the sandbox's disk. Pick a size with room for them (Sizes), and prune what you no longer need with docker system prune before you take a snapshot you will start many sandboxes from.