Run a pipeline in a Docker sandbox
Move a pipeline into a Docker sandbox when its agents must not change files on the daemon’s machine, or when its tests must find the same compiler on every machine. You build an image with Go and make it the sandbox go of the example pipelines run_program and write_hello. At the end, go test runs with the Go of the image, and the step request of the agent step writer#0 names the image ID that docker image inspect prints. If a step fails, see Troubleshooting.
Before you start
Section titled “Before you start”- You did the Quickstart: you have a clone of codeband with the binary at
build/codeband. - Docker runs on your machine:
docker versionshows aServerpart. - jq for the check at the end.
- Run each command from the
codebandfolder.
How a Docker sandbox runs a step
Section titled “How a Docker sandbox runs a step”For a sandbox of the kind docker, the engine runs each attempt of an agent step and each shell step in a new container of the image, and removes it at the end. codeband installs nothing and pulls no image, so the image holds git, sh, cat, mktemp, sleep, tr, grep, your toolchain and the program of every harness that your agents use.
-
Add your toolchain to the example image
examples/docker-sandboxholds aDockerfilewith git, Claude Code, Codex CLI and the useragent, as which every program in the container runs. Copy the folder:Terminal window cp -R examples/docker-sandbox /tmp/go-agentsIn
/tmp/go-agents/Dockerfile, replace the four comment lines below# Add the toolchain of your target repositories here, as root, for example:with the step that they show, without the#. It installs Debian’s Go:/tmp/go-agents/Dockerfile # Add the toolchain of your target repositories here, as root, for example:RUN apt-get update \&& apt-get install --yes --no-install-recommends golang-go \&& rm -rf /var/lib/apt/lists/*A Debian package goes before
USER agent, where the build runs as root. A tool for one user, such as rustup, goes at the end. -
Build the image
Terminal window docker build --tag go-agents /tmp/go-agentsThe build ends with the name of the image:
Output #10 writing image sha256:eac71bfcecd43c0270eeba34c5391f5892097ddbbdda5861b1c611ff603eef85 done#10 naming to docker.io/library/go-agents done#10 DONE 1.6sCheck that the image has Go, git and Claude Code, and runs as
agent:Terminal window docker run --rm go-agents sh -c 'go version && git --version && claude --version && id -un'Output go version go1.24.4 linux/arm64git version 2.47.32.1.332 (Claude Code)agent -
Define the sandbox in
codeband.tomlAdd a
[sandbox.go]table to~/.config/codeband/codeband.toml. Create the folder withmkdir -p ~/.config/codebandif it does not exist:~/.config/codeband/codeband.toml [sandbox.go]kind = "docker"image = "go-agents"imageis the tag ofdocker build --tag. -
Name the sandbox on the pipelines
Give the decorators of
run_programandwrite_hellothe argumentsandbox="go":examples/py-factory/py_factory/program.py @cb.pipeline(sandbox="go")def run_program(argv: Argv, env: Env = (), timeout_seconds: int = 600) -> cb.RunResult | None:examples/py-factory/py_factory/hello.py @cb.pipeline(stages=[build], sandbox="go")def write_hello(instruction: str = "add a file hello.txt that says hello") -> cb.RunResult | None:We recommend one name per toolchain, such as
go, so that each installation decides where it runs. -
Start the daemon and the code server
The daemon reads
codeband.tomlonly at start, so stop a runningcodeband devwith Ctrl-C. In a second terminal, in thecodebandfolder, run:Terminal window just examples/py-factory/devThe daemon logs the file that it read, the address of the web interface, and
Listeningwhen it is ready:Output Nov 29 21:57:38.922 INF Read the config file comp=daemon path=~/.config/codeband/codeband.toml…http://127.0.0.1:8765/?token=R4OB2CZ6PNUFA6LV7YJOWURBJS…Nov 29 21:57:39.321 INF Listening comp=daemon socket=~/.local/state/codeband/daemon.sock -
Make a Go target repository
Back in the first terminal, make a git repository with a Go package and its test:
Terminal window mkdir /tmp/greet-gocd /tmp/greet-gogit init --quiet .cat > go.mod <<'EOF'module example.com/greetgo 1.24EOFcat > greet.go <<'EOF'package greet// Hello greets name.func Hello(name string) string {return "hello, " + name}EOFcat > greet_test.go <<'EOF'package greetimport "testing"func TestHelloGreetsAda(t *testing.T) {if got := Hello("Ada"); got != "hello, Ada" {t.Errorf("Hello(%q) = %q", "Ada", got)}}EOFgit add go.mod greet.go greet_test.gogit commit -m "Add greet and its test"cd -The commit prints
3 files changed, andcd -takes you back to thecodebandfolder. -
Run a shell step in the container
Docker writes the file
/.dockerenvinto every container, so this test passes only in a container:Terminal window build/codeband run --repo /tmp/greet-go py-factory/run_program --input '{"argv": ["sh", "-c", "test -f /.dockerenv"]}'The shell step is ok. Your run ID is different:
Output Started run 3qyxr1vkht▸ program#0 shell step: sh -c test -f /.dockerenv✓ program#0 exit code 0 after 1.4sno code changeRun the tests of the target repository with the Go of the image.
-vprints the output of the program:Terminal window build/codeband run -v --repo /tmp/greet-go py-factory/run_program --input '{"argv": ["go", "test", "./..."]}'Output Started run 2bkn4ys8hp▸ program#0 shell step: go test ./...ok example.com/greet 0.003s✓ program#0 exit code 0 after 5.1sno code changeMost of the 5.1 seconds go to compiling Go’s standard library in the new container. A volume for the build cache saves that time.
-
Run an agent step in the container
write_helloruns one agent step on the fake harness, which needs no login, and ends the run with a patch file:Terminal window build/codeband run --repo /tmp/greet-go py-factory/write_hello --patch /tmp/greet-go.patchThe fake agent writes
hello.txtin the container, and the engine fetches its commit before it removes the container:Output Started run 8g73bqf4nb▸ writer#0 agent step of writerwriter#0: writing hello.txt✓ writer#0 finished after 1.2s, $0.00/tmp/greet-go.patchAn agent on Claude Code runs the
claudeof the image.codeband checkdoes not test it, so an image withoutclaudefails only when an attempt starts.
Check that it worked
Section titled “Check that it worked”The step request of an agent step records its sandbox. Print it for the run of write_hello, with the run ID that step 8 printed in place of RUN_ID:
jq -c '.stepStarted.agentStep.request.sandbox // empty' \ ~/.local/state/codeband/store/runs/RUN_ID/journal.jsonlThe command prints one line:
{"name":"go","kind":"SANDBOX_KIND_DOCKER","imageId":"sha256:eac71bfcecd43c0270eeba34c5391f5892097ddbbdda5861b1c611ff603eef85"}The image ID is the one of go-agents:
docker image inspect go-agents --format '{{.Id}}'sha256:eac71bfcecd43c0270eeba34c5391f5892097ddbbdda5861b1c611ff603eef85In the web interface, the page of the run shows the same under Step request. docker ps --all --filter label=codeband.sandbox=go lists no container, because the engine removed each one.
Optional: Keep Go’s build cache in a volume
Section titled “Optional: Keep Go’s build cache in a volume”A volume in the docker table keeps files from one container to the next. Add one at /home/agent/.cache, where Go keeps its build cache, and set GOFLAGS:
[sandbox.go]kind = "docker"image = "go-agents"
[[sandbox.go.docker.Mounts]]Type = "volume"Source = "gocache"Target = "/home/agent/.cache"
[sandbox.go.env]GOFLAGS = { value = "-trimpath" }Docker creates the volume gocache at the first mount. The target must be a folder of the image that belongs to agent. Without -trimpath, Go runs the tests again, because the workspace is in a new folder in each sandbox.
Restart codeband dev and run the tests twice:
build/codeband run -v --repo /tmp/greet-go py-factory/run_program --input '{"argv": ["go", "test", "./..."]}'build/codeband run -v --repo /tmp/greet-go py-factory/run_program --input '{"argv": ["go", "test", "./..."]}'The second run takes a second, and Go prints (cached):
Started run pxkhawnsd4▸ program#0 shell step: go test ./...ok example.com/greet 0.003s✓ program#0 exit code 0 after 4.9sno code changeStarted run 600j619yjh▸ program#0 shell step: go test ./...ok example.com/greet (cached)✓ program#0 exit code 0 after 1.0sno code changeKeep only caches in a volume: the step request does not record its files, so an agent step in a run fork can see other files there.
Clean up
Section titled “Clean up”Stop codeband dev with Ctrl-C, remove the tables of go from codeband.toml, and run:
git checkout -- examples/py-factory/py_factory/program.py examples/py-factory/py_factory/hello.pyrm -rf /tmp/go-agents /tmp/greet-go /tmp/greet-go.patchThe daemon never removes the repository cache of /tmp/greet-go. docker volume ls --filter name=codeband-cache- lists the caches of all target repositories; remove this one with docker volume rm. docker volume rm gocache and docker image rm go-agents remove the build cache and the image.
Troubleshooting
Section titled “Troubleshooting”For an error that this page doesn’t cover, see Run problems.
start the sandbox: the image … is not on this machine, and codeband pulls no image: …
Section titled “start the sandbox: the image … is not on this machine, and codeband pulls no image: …”The image of [sandbox.go] names an image that your Docker does not have, here go-agents:1.24. An agent step fails with the attempt failure kind setup, and a shell step is not ok:
▸ writer#0 agent step of writer writer#0 attempt 0 failed: setup: start the sandbox: the image go-agents:1.24 is not on this machine, and codeband pulls no image: run docker pull go-agents:1.24 (no retry)✗ writer#0 failed after 0.1s: setup: start the sandbox: the image go-agents:1.24 is not on this machine, and codeband pulls no image: run docker pull go-agents:1.24Build the image with the name of the message, as in step 2, or run the docker pull command of the message for an image of a registry. The next run finds it without a restart. When the name in codeband.toml is wrong, fix image and restart codeband dev.
the pipeline names the sandbox go, which the daemon does not define: it read no config file, …
Section titled “the pipeline names the sandbox go, which the daemon does not define: it read no config file, …”The daemon read no codeband.toml, or one without [sandbox.go]. The line Read the config file or Read no config file of codeband dev names the file or the places it searched. Add the table there and restart codeband dev.
start the sandbox: reach Docker at …
Section titled “start the sandbox: reach Docker at …”Start Docker, and check that docker version in the terminal of codeband dev shows a Server part. Docker not reached has the details.
Related
Section titled “Related”- Sandbox explains how commits get in and out, and what a sandbox does not protect.
- The
codeband.tomlreference lists every key of a sandbox definition. - Run a program in the target repository explains the shell step of
run_program. - Limit the network of a Docker sandbox lets the containers reach only the domains that you name.