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 Cloud Sandboxes with Environment Files: A Hands-On sbx env Workflow

6 min read

Dark terminal window showing the command sbx --cloud env run ./cloud.sbxenv.yaml with the title Docker Cloud Sandboxes with Environment Files

Exploring Docker Cloud Sandboxes for Development

Docker published a new walkthrough, Use environment files in the cloud, that shows how to take one declarative YAML file and use it to drive a Docker Cloud Sandbox from creation to cleanup. The file picks the agent, environment variables, kits and resource limits, and the sbx --cloud env commands turn it into a running sandbox on Docker-managed infrastructure. This matters because a cloud sandbox behaves differently from a local one: it has its own filesystem, its own credentials and its own network policy.

In this post, the Collabnix Team walks through the full workflow: preparing cloud credentials, writing the environment file, creating the sandbox, moving your project in and out, working with Claude Code, and adapting a local environment file for the cloud. Every command below was checked against the official sbx CLI reference.

Prerequisites

  • The sbx CLI, version 0.45.0 or later, which the Cloud sandboxes page lists as the minimum for cloud use.
  • An active Docker Agentic Platform subscription with cloud access activated. Cloud compute charges apply.
  • A Docker account signed in from the CLI with sbx login.
  • An Anthropic API key for Claude Code, and optionally a GitHub token with write access if you want the agent to push commits.
  • A local project to work on. The guide uses a project directory named web-app.

Note that sbx env is marked experimental in the CLI reference. The command interface and the file format may change between releases, so keep your team on the same sbx version.

Sign in and confirm that your account can reach the cloud service:

sbx login
sbx --cloud diagnose

Step 1: Store cloud credentials before you create anything

Credentials you configured for local sandboxes are not visible to cloud sandboxes. The cloud has its own secret store, so you need to add the agent key there first. For Claude Code that is the anthropic service secret:

sbx --cloud secret set anthropic

The command prompts for the key, so it never lands in your shell history.

If the agent should push to GitHub, add a GitHub token scoped to the sandbox name you will use:

sbx --cloud secret set github --sandbox cloud-web-app

Two rules from the guide are easy to miss. First, the --sandbox value must match the name in your environment file. Second, cloud environments pick up their credentials at creation time. Adding a GitHub secret after the sandbox exists will not make it available inside that sandbox. If you change your mind later, copy your work out, then recreate the environment with the secret in place.

Step 2: Write the cloud environment file

Save the following as cloud.sbxenv.yaml in the root of your project. The file location does not decide which files end up in the sandbox, so you can keep it elsewhere if you like.

schemaVersion: "1"
name: cloud-web-app
agent: claude
env:
  NODE_ENV: test
sandboxOptions:
  cpus: 2
  memory: 4g

This file selects Claude Code as the agent, sets NODE_ENV inside the sandbox, and asks for two CPUs and 4 GiB of memory. The CPU and memory values must match one of the available cloud sizes. You can also list kits in the file to install project tools.

Notice what is missing: there is no workspace entry. A cloud sandbox does not mount or sync directories from your machine, so a workspace declaration has no meaning there. You bring code in by copying it or by cloning a repository inside the sandbox.

Step 3: Plan, create and load your project

Run these from your project directory. plan prints what applying the file would set up, including inherited cloud credentials, without applying or recording anything. create provisions the sandbox without attaching to it.

sbx --cloud env plan ./cloud.sbxenv.yaml
sbx --cloud env create --ttl 2h --on-timeout delete ./cloud.sbxenv.yaml

create asks you to approve the plan before it applies it. The --ttl flag sets a two hour lifetime, and --on-timeout delete removes the sandbox when that time runs out. The reference documents three timeout actions: stop, restart (which needs a TTL of at least 1h) and delete. Without these flags, a cloud sandbox expires after one hour, and the service stops sandboxes that can be resumed and deletes the rest. With delete set, make sure you pull your results out before the two hours are up.

Now copy the project into the sandbox workspace:

sbx --cloud cp . cloud-web-app:/home/agent/workspace/web-app

A few details from the guide and the sbx cp reference shape how you should use this:

  • Use a destination that does not exist yet. If the destination is an existing directory, the source is placed inside it, which nests your project one level deeper.
  • cp . does not honor .gitignore. Hidden files such as .env, Git history, dependency folders and build output all go up. For larger projects, build a clean staging directory and copy that instead.
  • The copy is a one time transfer. Edits on either side are not synchronized afterwards.
  • Directory uploads skip symbolic links and empty directories and report what they skipped.
  • Install dependencies inside the sandbox rather than uploading a local dependency directory.

If you prefer Git, clone the repository inside the sandbox instead. That needs cloud credentials for private repositories and a cloud network policy that allows the hosts your project talks to.

Step 4: Automate the upload with a postCreate hook

Manual copying is fine once, but the point of an environment file is repeatability. Add a lifecycle.postCreate command so a fresh sandbox gets the project automatically:

lifecycle:
  postCreate:
    - name: Upload project
      command: sbx --cloud cp . "$SBX_SANDBOX_NAME:/home/agent/workspace/web-app"

This command runs on your machine, with your privileges, after the sandbox is created and before the CLI attaches to the agent. The . resolves to the directory that holds the environment file, and SBX_SANDBOX_NAME carries the sandbox name, including any --name override. If you keep the file somewhere else or upload from a staging directory, set the command workdir accordingly.

Reconnecting to an existing sandbox skips postCreate, so the agent work is never overwritten by a second upload. If the upload fails, the sandbox still exists. You can finish the transfer by hand or remove it and start over.

Step 5: Work with the agent, then retrieve and clean up

Attach to Claude Code using the same file. run creates the sandbox if it does not exist and reattaches if it does:

sbx --cloud env run ./cloud.sbxenv.yaml

Give the agent a scoped task, for example: work in /home/agent/workspace/web-app, find the test and lint commands in package.json, install dependencies, run the checks and fix failures. Press Ctrl+\ to detach while the agent keeps running. Only pass --ttl and --on-timeout on creation, because run refuses them when attaching to an existing sandbox.

To run a one off command without an agent session, use env exec with -- separating the file path from the command:

sbx --cloud env exec ./cloud.sbxenv.yaml -- \
  git -C /home/agent/workspace/web-app diff --stat

When the agent is done, copy the project back to a new local directory so you can review changes before touching your original files:

sbx --cloud cp cloud-web-app:/home/agent/workspace/web-app ../web-app-result

If you set the GitHub secret before creation, you can ask the agent to commit to a branch such as cloud-fixes and push it over an HTTPS remote. Then review it locally:

git fetch origin
git switch --track origin/cloud-fixes

Finally, remove the environment:

sbx --cloud env rm ./cloud.sbxenv.yaml

Removal deletes the sandbox and the secrets this environment provisioned. Inherited credentials, such as the Anthropic key from Step 1, stay. Global bindings also stay unless you add --prune-bindings. Anything stored only inside the sandbox is lost. For scripts and CI, --force approves the plan for create, run or rm, and sbx --cloud env run --detach ./cloud.sbxenv.yaml starts the sandbox when no terminal is available.

Local Sandboxes vs Cloud Sandboxes

The same sbxenv.yaml format drives both local and cloud sandboxes, but the cloud supports only a subset of fields. According to the guide, cloud environments support agents, kits, environment variables, CPU and memory limits, credentials for supported providers, and host lifecycle commands. Before reusing a local file in the cloud, remove these declarations:

  • workspace, additionalWorkspaces and clone options
  • ports, registries and mcp.servers
  • sandboxOptions.template, profile, pullPolicy, skills, display, gpu, usb and writableEnvFiles

Unsupported settings are rejected before any host command or provisioning runs, which is a safe failure mode. Pass the cloud file path explicitly, because with no path the CLI loads shared defaults from ~/.sbxenv.yaml, and those may contain local only settings. For ports and MCP servers in the cloud, use the dedicated cloud commands instead.

Credential precedence is also worth knowing. Sandbox scoped credentials override account defaults, and credentials declared in the file secrets section override both. Setting snapshot: true resolves a host command or vault reference once and uploads the value, and that value does not refresh. Dynamic secret sources and custom credential providers are not supported in the cloud.

Lifecycle hooks differ too. initialize prepares files before creation, and preRemove can pull results down when you run sbx --cloud env rm. However, preRemove does not run when the cloud service deletes an expired sandbox, and a failing preRemove does not block removal. Do not treat it as your only backup.

Troubleshooting tips from the docs

  • Agent cannot authenticate: local credentials do not carry over. Store the key with sbx --cloud secret set.
  • GitHub token missing inside the sandbox: secrets are bound at creation. Copy your work out and recreate the environment with the secret set and the --sandbox name matching the file.
  • Project nested one level too deep: the first cp targeted an existing directory. Use a fresh destination path.
  • run rejects --ttl: timeout flags only apply when creating. Drop them when reattaching.
  • Changes to secrets, bindings, kits or resource limits do not apply: these require recreating the sandbox. Updated env values apply to new sessions, but rejoining a running agent keeps its old environment.
  • Creation was interrupted: retry the same command with the unchanged declaration within 23 hours, and follow the recovery message.

Key takeaways

  • One cloud.sbxenv.yaml gives a whole team the same agent, variables, kits and resource limits on Docker-managed infrastructure.
  • Cloud sandboxes have their own filesystem, credentials and network policy. Nothing from your local sandbox setup is inherited.
  • Use plan to preview, create or run to apply, exec for one off commands and rm to clean up.
  • Set cloud secrets before creation. They cannot be added to an existing environment.
  • sbx --cloud cp is a one time copy that ignores .gitignore, so stage your files carefully.
  • Always retrieve work before the TTL ends, since preRemove does not run on service side expiry.

References

Source guide: Use environment files in the cloud. Related pages: Cloud sandboxes, Local and cloud differences, Authenticate cloud agents, Manage cloud network policy, Environment files, and the CLI reference for sbx env, sbx cp and sbx secret set.

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