Docker in a sandbox
Run Docker, Docker Compose and Testcontainers inside a sandbox. A real dockerd in the sandbox's own microVM, with no --privileged and no host socket.
A sandbox is a Firecracker microVM with its own Linux kernel, so a normal Docker daemon runs inside it. The containers you start are processes in that VM: they share nothing with other sandboxes, and you need no --privileged flag and no Docker socket from the host.
| Kernel | Linux 6.1 (vmlinux-ps-6.1). Docker cannot start on the older 5.10 kernel. |
| Templates | Every official template runs 6.1, and so does every template you build since 2026-11-23. The examples use base. |
| Preinstalled | No. Install it in the sandbox (under a minute), or bake it into your own template. |
| Ports | A published container port gets the sandbox's public preview URL. |
| Hibernate | Running containers are saved with the sandbox and are still running after a wake. |
Quick start
Install Docker, run a container, and open its port from the internet. The install took 27 to 44 seconds in our runs. Stream it: a blocking exec is cut by the gateway after about 100 seconds.
from pandastack import Sandbox
sbx = Sandbox.create(template="base", ttl_seconds=1800)
sbx.exec_stream(
"apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker.io"
" && systemctl start docker",
on_stdout=lambda s: print(s, end=""),
on_stderr=lambda s: print(s, end=""),
timeout_seconds=300,
)
print(sbx.exec("docker run --rm hello-world", timeout_seconds=120).stdout)
sbx.exec("docker run -d --name web -p 8080:80 nginx:alpine", timeout_seconds=120)
print(sbx.preview_url(8080))
# → https://8080-<sandbox-id>.pandastack.ai (the nginx welcome page)
# ... when you are done:
sbx.kill()import { Sandbox } from "@pandastack/sdk";
const sbx = await Sandbox.create({ template: "base", ttlSeconds: 1800 });
await sbx.execStream(
"apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker.io" +
" && systemctl start docker",
{
onStdout: (s) => process.stdout.write(s),
onStderr: (s) => process.stderr.write(s),
timeoutSeconds: 300,
},
);
console.log((await sbx.exec("docker run --rm hello-world", { timeoutSeconds: 120 })).stdout);
await sbx.exec("docker run -d --name web -p 8080:80 nginx:alpine", { timeoutSeconds: 120 });
console.log(sbx.previewUrl(8080));
// → https://8080-<sandbox-id>.pandastack.ai (the nginx welcome page)
// ... when you are done:
await sbx.kill();The package does not start the daemon by itself in a sandbox, hence systemctl start docker. Measured on production (2026-11-23): hello-world ran in 1 second, and nginx answered on its preview URL 4 seconds after docker run.
Bake Docker into your template
Installing Docker in every sandbox costs those seconds every time. Build a template with Docker in it once instead. The template's snapshot is taken with the daemon running, so every sandbox made from it starts with Docker already up.
FROM ubuntu:24.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
docker.io docker-compose-v2 docker-buildx iptables ca-certificates curl \
&& mkdir -p /etc/docker \
&& echo '{"registry-mirrors": ["https://mirror.gcr.io"]}' > /etc/docker/daemon.json \
&& rm -rf /var/lib/apt/lists/*from pandastack import Client, Sandbox
client = Client()
build = client.templates.build_from_dockerfile(
"docker-box", "Dockerfile", memory_mb=4096, size_mb=4096,
)
client.templates.wait_for_build(build.id) # a few minutes
sbx = Sandbox.create(template="docker-box")
print(sbx.exec("docker run --rm hello-world").stdout) # no install stepimport { readFileSync } from "node:fs";
import { Client, Sandbox } from "@pandastack/sdk";
const client = new Client();
const build = await client.templates.buildFromDockerfile({
name: "docker-box",
dockerfile: readFileSync("Dockerfile", "utf8"),
memoryMb: 4096,
sizeMb: 4096,
});
for await (const line of client.templates.buildLogs(build.id)) console.log(line);
const sbx = await Sandbox.create({ template: "docker-box" });
console.log((await sbx.exec("docker run --rm hello-world")).stdout);pandastack template build --name docker-box -f Dockerfile --memory-mb 4096 --size-mb 4096The daemon.json line points Docker at a registry mirror (why). Our test build of a Dockerfile like this one took about five minutes. The sandbox it produced had Docker running at start and ran hello-world in 2 seconds.
A template's kernel is fixed when the template is built. One you built before 2026-11-23 boots the older 5.10 kernel, where dockerd exits at start: build it again with the same command. uname -r inside a sandbox shows which kernel it runs (6.1.x is the one you want). To build on 5.10 on purpose, send kernel=vmlinux-5.10 with the build.
Docker Compose
Install the Compose plugin (docker-compose-v2 on Ubuntu, already in the Dockerfile above), then use it as on any Linux machine. Run these in the sandbox, for example through exec_stream:
apt-get install -y -qq docker-compose-v2 # skip in a template that has it
mkdir -p /root/stack && cd /root/stack
cat > compose.yaml <<'EOF'
services:
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: test
ports: ["5432:5432"]
cache:
image: redis:7-alpine
ports: ["6379:6379"]
EOF
docker compose up -d
docker compose exec -T db pg_isready -U postgres # accepting connections
docker compose exec -T cache redis-cli ping # PONGMeasured: both services were ready 21 seconds after docker compose up -d, image pulls included. Your code in the sandbox reaches them on localhost:5432 and localhost:6379.
Testcontainers
Testcontainers finds the daemon at /var/run/docker.sock, as it does on a Linux CI runner, so a test suite that starts its own throwaway databases runs unchanged. We tested the Python library; the other languages look for the same socket.
export PATH=/opt/mise/shims:$PATH # base keeps Python under mise
python3 -m pip install "testcontainers[redis]" redisfrom testcontainers.redis import RedisContainer
with RedisContainer("redis:7-alpine") as redis:
print(redis.get_client().ping()) # TrueMeasured: the pip install took 8 seconds and the test 3 seconds.
Build images
Install the BuildKit plugin (docker-buildx, also in the Dockerfile above), then build and run as usual:
apt-get install -y -qq docker-buildx # skip in a template that has it
docker build -t app .
docker run --rm appA two-step Alpine image built in 2 seconds in our test. For a build that may run longer than 5 minutes, start it in the background (see Limits).
Avoid Docker Hub's pull limit
Docker Hub allowed 100 anonymous pulls per hour from our machines' address when we checked (2026-11-23), and every sandbox on the same machine shares that address. Pull through Google's free mirror of Docker Hub instead. It needs no account:
mkdir -p /etc/docker
echo '{"registry-mirrors": ["https://mirror.gcr.io"]}' > /etc/docker/daemon.json
systemctl restart docker
docker info --format '{{.RegistryConfig.Mirrors}}' # [https://mirror.gcr.io/]Docker asks the mirror first and falls back to Docker Hub for an image the mirror does not have. In your own template, write the same file in the Dockerfile, as above. Pulls made after docker login count against your Docker Hub account instead of the shared address.
Hibernate a running stack
Hibernating saves the whole VM, containers included. Waking it brings them back still running: nothing restarts and no image is pulled again.
sbx = Sandbox.create(template="base", persistent=True)
# ... install Docker, start your containers ...
sbx.hibernate()
# later:
sbx.wake()
print(sbx.exec("docker ps").stdout) # the same containers, still UpMeasured on a 4 GiB base sandbox with nginx running in a container: 48 seconds to hibernate, 2 seconds to wake, and nginx was still answering on its preview URL. A persistent sandbox runs until you kill it, so kill it when you are done.
Limits
- Disk. Images and containers live on the sandbox's own disk.
basehas about 7.6 GB free, so prefer slim images (-alpine,-slim). - Memory. Containers share the sandbox's memory: 4 GiB in
base, and thememory_mbyou bake into your own template. - Long commands. A streamed command is cut after 5 minutes by the load balancer in front of the API, and the command keeps running in the sandbox. Start long builds or test runs in the background and poll their log:
setsid nohup docker build -t app . > /tmp/build.log 2>&1 &. - Not preinstalled. No official template ships Docker yet.
Troubleshooting
| Symptom | Cause and fix |
|---|---|
Cannot connect to the Docker daemon at unix:///var/run/docker.sock | The daemon is not running: apt-get install does not start it in a sandbox. Run systemctl start docker, or dockerd > /var/log/dockerd.log 2>&1 & in an image without systemd. journalctl -u docker shows why it stopped. |
dockerd exits with iptables or nftables errors, and uname -r starts with 5.10 | The template was built before 2026-11-23. Build it again. |
toomanyrequests from Docker Hub | The anonymous pull limit. Use the mirror above, or docker login. |
Networking — NATID mode
Why every sandbox has the same internal IP, and how the orchestrator dials it.
Ephemeral environments
Apps and managed databases share one lifecycle — create, serve, scale-to-zero on idle, wake on demand, delete. What's guaranteed at each step, and when to reach for an ephemeral environment versus an always-on one.