Join our Discord Server
Collabnix Team The Collabnix Team is a diverse collective of Docker, Kubernetes, and IoT experts united by a passion for cloud-native technologies. With backgrounds spanning across DevOps, platform engineering, cloud architecture, and container orchestration, our contributors bring together decades of combined experience from various industries and technical domains.

Docker Sandbox Environment Files: Declarative sbxenv.yaml Setups with sbx env plan, run, exec and rm

6 min read

Dark terminal window showing the command sbx env run base.sbxenv.yaml local.sbxenv.yaml with the title Sandbox Environment Files, Declarative sbxenv.yaml with sbx env

Every team that runs AI coding agents in Docker Sandboxes ends up with the same problem: a long sbx run command with kits, ports, secrets and mounts that only one person remembers. The Docker docs now have a detailed guide on sandbox environment files, and it was expanded this week with launch command overrides, custom mount targets, clearer exec lifecycle rules and a recovery path for sandboxes that lost their environment state. This post walks through the whole workflow with commands taken from the guide and checked against the sbx env CLI reference.

An environment file is a declarative sbxenv.yaml that describes the agent, kits, workspace, resources, credentials and host lifecycle commands for one sandbox. You commit it next to your project, and every contributor gets the same sandbox from one command. Kits package the software; the environment file picks the kits and configures the sandbox around them.

Prerequisites

  • The sbx CLI installed and signed in. Follow the Docker Sandboxes install guide for your platform.
  • A project directory you want the agent to work in. The examples use a web-app folder.
  • Awareness that sbx env is marked experimental. The command interface and file format may change between releases, so pin kit versions and re-read the guide when you upgrade.
  • For cloud examples, access to Docker Cloud Sandboxes and the --cloud flag.

Step 1: Write your first sbxenv.yaml

The guide is strict about one thing: keep the environment file outside every directory you mount into the sandbox. The file can declare host commands, so an agent that can edit it could change what runs on your machine next time. A safe layout looks like this:

web-app-env/
├── sbxenv.yaml
└── web-app/

Now create web-app-env/sbxenv.yaml. This version uses the Claude agent, mounts web-app as the workspace, adds the Playwright kit, sets one environment variable and publishes port 3000:

schemaVersion: "1"
name: web-app
agent: claude
workspace: ./web-app

kits:
  - docker.io/sbx/playwright-kit:latest

env:
  NODE_ENV: test

ports:
  - sandbox: 3000
    host: 3000

From the web-app-env directory, start it:

$ sbx env run

sbx prints an environment plan and asks for approval. On the first run it creates a sandbox named web-app, installs Playwright and Chromium, publishes port 3000 on the host and attaches you to the agent. Later runs attach to the same sandbox. If you pass a directory, sbx looks for sbxenv.yaml inside it; with no path it uses the current directory.

If you must keep the file inside a direct-mounted workspace, put it in the workspace root, where sbx mounts it read-only. Only set sandboxOptions.writableEnvFiles: true when you really want the agent to edit it, because that lets the agent change future host commands.

Step 2: Review the plan before anything touches your host

The create, run and rm subcommands all show a plan first. To inspect it without applying or approving anything, use:

$ sbx env plan

The plan lists host commands, credentials, bindings, MCP registrations, directories, kits, ports, sandbox options and environment variables. Literal secret values show only as SHA-256 digests, while secret references and host commands stay visible so you can review them. Each entry carries a symbol:

  • + adds a resource, ~ changes one, - destroys one.
  • > runs a host command again.
  • ! stops tracking a resource that is no longer declared, without deleting it.

Plans that contain lifecycle commands, credential command sources or local MCP commands ask for approval on every invocation by default. You have three options: approve one run with --force, skip lifecycle and MCP commands, or let sbx remember approval until the commands change:

$ sbx env run --skip-host-commands
$ sbx settings set env.rememberHostCommands true

Note that --skip-host-commands also disables previously approved MCP commands until you run sbx env run again without the flag and approve. Credential command sources still run when their secrets are needed.

Step 3: Parameterize and layer environment files

One file can serve many setups through a top-level args block. Each argument needs exactly one of default or required: true, and can be restricted with enum or an RE2 pattern:

schemaVersion: "1"
name: web-app
agent: claude

args:
  channel:
    default: stable
    enum: [stable, beta]
  endpoint:
    required: true
  cpus:
    default: "4"
    pattern: "[1-9][0-9]*"

env:
  RELEASE_CHANNEL: ${{ env.args.channel }}
  API_ENDPOINT: ${{ env.args.endpoint }}

sandboxOptions:
  cpus: ${{ env.args.cpus }}

Supply values on the command line or from a file of NAME=VALUE lines. Later files win over earlier files, and flags win over every file:

$ sbx env run --env-arg endpoint=https://api.example.com --env-arg channel=beta
$ sbx env run --env-args-file production.args

Only ${{ env.args.NAME }}, ${{ env.projectDir }} and ${{ env.fileDir }} are expanded. Shell style ${VAR} is not read from your host, so a value like $PATH:/opt/bin passes through unchanged. An unquoted reference is parsed as YAML after substitution, which is why cpus above becomes an integer.

For team defaults plus personal tweaks, commit a base.sbxenv.yaml, git-ignore a local.sbxenv.yaml, and pass both in merge order:

$ sbx env run base.sbxenv.yaml local.sbxenv.yaml

Mappings merge by key, lists concatenate (except entrypoint and cmd), and later scalars replace earlier ones. Every source file must be valid on its own; a later file cannot fix a broken value in an earlier one. You can also keep personal defaults in ~/.sbxenv.yaml with workspace: ${{ env.projectDir }}, which is loaded only when you do not pass an explicit path and cannot set name.

Step 4: Control what sbx env run launches

New in the guide: entrypoint and cmd decide what sbx env run executes inside the sandbox. Both are lists of strings, cmd is appended to entrypoint, and each item stays one argument with no shell interpretation:

entrypoint: [/usr/bin/printf, '%s\n']
cmd: [default, 'two words']

Arguments after -- replace cmd for that run and keep the entrypoint:

$ sbx env run -- 'replacement argument'

If you need a shell expression, set entrypoint: [/bin/sh, -c] explicitly. These overrides affect only sbx env run. They do not change the published kit, sbx env exec or PID 1 in the sandbox. With --detach, run only starts the sandbox, and you cannot combine --detach with arguments after --. Changes to these two fields apply on the next run without recreating the sandbox.

Step 5: Multiple repositories, mount targets and lifecycle hooks

additionalWorkspaces mounts related directories next to the primary workspace. The new target field lets you choose an absolute POSIX path inside the sandbox instead of reusing the host path:

workspace: .
additionalWorkspaces:
  - path: ../shared-libs
    target: /workspace/libs
    readOnly: true

Additional workspaces are always mounted directly, even when the primary workspace uses clone mode, and they follow the same target restrictions as sbx create and sbx run.

The lifecycle block runs commands on your host with your user privileges in three phases: initialize (every create and run, so make it idempotent), postCreate (only after a new sandbox is created) and preRemove (after you approve removal):

lifecycle:
  initialize:
    - name: Prepare workspace
      command: test -d web-app || git clone https://github.com/example/web-app
      timeout: 5m
  postCreate:
    - command: ./scripts/seed-fixtures.sh
      workdir: web-app
  preRemove:
    - command: ./scripts/archive-state.sh

Commands receive variables such as SBX_LIFECYCLE_PHASE, SBX_SANDBOX_NAME, SBX_AGENT and SBX_WORKSPACE. The file’s env values and resolved secrets are not passed to host commands.

Step 6: Reuse in automation, recover and clean up

The same committed file works in CI. --force approves the plan for one invocation only, and for rm it also removes the sandbox when it is in use:

$ sbx env create --force
$ sbx env exec -- npm test
$ sbx env rm --force

The guide now spells out exec behavior: if the sandbox is missing and stdin is interactive, it offers to create it with the normal approval and lifecycle flow. For an existing sandbox it skips lifecycle commands. With noninteractive stdin or --no-input, run sbx env create first. If you override the name with --name, use the same name for every command:

$ sbx env create --name web-app-test
$ sbx env exec --name web-app-test -- npm test
$ sbx env rm --name web-app-test

When you remove an environment, scoped secrets and local MCP commands go away, but global credential bindings stay unless you ask for them to be pruned. Be careful: this deletes the whole global binding entry for each declared service, which can affect other sandboxes.

$ sbx env rm --prune-bindings

Local sandboxes vs Cloud Sandboxes

Everything above targets local sandboxes. Adding --cloud (as in sbx --cloud env) creates the sandbox on Docker-managed infrastructure from the same file format, but the docs list important differences:

  • Cloud sandboxes do not mount or sync local project files. You copy files in or clone a repository, then retrieve your work before removal.
  • Local credentials are not available in the cloud. For ref or command secrets, set snapshot: true so the value is resolved once on your host and stored as a literal; rotate it by recreating the environment.
  • fuse and writableEnvFiles in sandboxOptions are local only, and cloud resource limits must match a published cloud size.
  • Lifecycle commands still run on your host, and any sbx command they call, such as sbx cp, inherits cloud mode. SBX_WORKSPACE is empty in cloud environments.

For the cloud walkthrough, see Use environment files in the cloud, which we also covered in an earlier Collabnix post.

Troubleshooting tips from the docs

  • “agrees with everything this environment declares” but “nothing was done”: the sandbox exists but sbx lost the record linking it to the environment, for example after an upgrade or state reset. Run sbx env run interactively with the same file paths and name, confirm adoption only if it is the right sandbox, then approve the plan. --force does not confirm adoption, so do this before retrying automation.
  • Config change seems ignored: on an existing sandbox, only env values and MCP servers are reconciled. Workspaces, kits, ports, secrets, bindings and sandboxOptions need sbx env rm and a fresh create.
  • Failed create left resources behind: secrets, bindings and MCP registrations are applied before the sandbox is created. Run sbx env rm with the same paths, adding --prune-bindings if you want declared bindings removed too.
  • Remote kit rejected: remote kit sources must match the kit.allowedSources setting. Docker Hub is allowed by default, and setting the value replaces the full allowlist.
  • Port publishing fails: sandbox creation fails and the new sandbox is removed. Check for host port conflicts, and use protocol: tcp with no hostIP for a dual-stack binding.

Key takeaways

  • sbxenv.yaml turns a long sandbox setup into a reviewable, shareable file. Keep it outside every writable mount.
  • Use sbx env plan to see host commands, secrets and ports before you approve anything.
  • args, --env-arg and multi-file merges let one base file serve developers, CI and personal setups.
  • entrypoint and cmd change only what sbx env run launches, and -- overrides cmd per run.
  • additionalWorkspaces now supports a custom target path for extra mounts.
  • Automation should use create --force, exec and rm --force, and lost-state recovery must be done interactively.

References

Source guide: Sandbox environment files on docs.docker.com. Related pages: sbx env run CLI reference, Sandboxes settings (env.rememberHostCommands, sandbox.fuse), environment files in the cloud, using kits, credentials and installation.

Have Queries? Join https://launchpass.com/collabnix

Collabnix Team The Collabnix Team is a diverse collective of Docker, Kubernetes, and IoT experts united by a passion for cloud-native technologies. With backgrounds spanning across DevOps, platform engineering, cloud architecture, and container orchestration, our contributors bring together decades of combined experience from various industries and technical domains.

Understanding AI Embeddings and Vector Search for Beginners

Explore the world of AI embeddings and vector search, learning how they transform massive data into actionable insights, driving intelligent systems.
Collabnix Team
9 min read
Join our Discord Server