Docker Sandboxes already handle credentials for built-in services such as GitHub and the model providers. The host-side proxy holds those secrets and adds them to outbound requests, so the agent never sees a real key. The harder question comes a week later, when someone wants the agent to call an internal API. The proxy has no idea where that API lives or which header it wants, and pasting a token into the sandbox environment defeats the purpose of sandboxing the agent at all.
Docker’s new Learn guide, Connect to services without exposing credentials, part of the “Build and customize Docker Sandbox environments” learning path, answers that question with a small v3 mixin kit. In this post the Collabnix Team walks through the same technique with a different scenario, explains why each field exists, and points out where local and cloud sandboxes behave differently.
The scenario: an inventory agent that reads shipment status
Imagine an inventory-app repository. Its coding agent should check the status of pending shipments before it proposes a change to the reorder logic. Shipment data lives behind an internal “stockroom” API at api.stockroom.example.com, with a read endpoint at GET /v2/shipments. The API expects a bearer token.
We want three things:
- The agent can reach the stockroom API, and nothing else new.
- The real token stays on the developer’s host. The sandbox only ever sees a placeholder.
- The token can ride on read requests only. A
POSTthat cancels a shipment must never carry it.
All of this fits in one descriptor file with no Dockerfile: stockroom-api/stockroom-api.yaml. Because nothing in it is secret, it can live in the repo and go through normal code review.
Prerequisites
- sbx v0.45 or later. Kits v3 need at least v0.45; the official guide was written against v0.47.0. See Install Docker Sandboxes.
- Docker with Buildx (optional) to validate the descriptor before using it. sbx builds a local kit directory on its own, so this step is a safety net, not a requirement.
- Docker’s published v3 shell workload,
docker/sbx-kit-shell:1.0.0, which we use as the base agent so we can test with plaincurl. - An interactive terminal for the first run, because sbx asks you to approve the kit’s credential request.
Step 1: Prove the API is unreachable by default
Start with no kit. Sandboxes deny egress by default, so this request should stop at the host proxy before it ever reaches the stockroom API:
sbx create docker/sbx-kit-shell:1.0.0 --name inv-baseline
sbx exec inv-baseline -- curl -s https://api.stockroom.example.com/v2/shipments
The sbx create reference documents --name for naming the sandbox, and sbx exec runs a command inside it (starting it first if it is stopped). This failing baseline proves every later success comes from the kit.
Step 2: Open a network path with network-policy@1
Create the kit directory next to where you run sbx, and add stockroom-api/stockroom-api.yaml:
# syntax=docker/sandbox-kit:3
schemaVersion: "3"
kind: mixin
displayName: Stockroom shipments API
description: Network access to the internal stockroom API
capabilities:
- type: com.docker.sandbox/network-policy@1
config:
runtime:
allow:
- api.stockroom.example.com
The first line selects the kit build frontend and schemaVersion: "3" picks the v3 format. kind: mixin means the kit adds to a workload instead of replacing it, and a mixin is allowed to carry no files at all. The runtime block applies while the agent runs.
A sandbox keeps the kit configuration it was created with, so each iteration gets a new sandbox name. The --kit flag adds a mixin from a local directory, ZIP, git repo or OCI reference:
sbx create docker/sbx-kit-shell:1.0.0 --name inv-egress --kit ./stockroom-api
sbx exec inv-egress -- curl -s https://api.stockroom.example.com/v2/shipments
This time the request leaves the sandbox and reaches the API, which rejects it as unauthenticated (the guide shows a 401). That is the first important lesson: allowing a host and authenticating to it are separate grants. A network rule decides where traffic may go. It never attaches a credential.
Step 3: Declare a proxy-managed credential
Now add a credential@1 capability under the network policy:
- type: com.docker.sandbox/credential@1
description: Stockroom API token
config:
service: stockroom-ledger
phase: runtime
apiKey:
name: STOCKROOM_API_TOKEN
proxyManaged: true
inject:
- domain: api.stockroom.example.com
header: Authorization
format: "Bearer %s"
Each field answers one question the proxy would otherwise have to guess:
service: stockroom-ledgernames the secret on the host. There is no registration step; the kit and the user simply agree on this string.phase: runtimemakes it available to the agent. Aninstallcredential would only exist for setup hooks and is withdrawn before the agent starts.apiKey.nameis the environment variable tools inside the sandbox read. It holds a placeholder, never the real value.proxyManaged: truekeeps the real value on the host and lets the proxy write it into outbound requests.injectsays which requests receive it and how. Here, requests to the stockroom host get anAuthorizationheader rendered fromformat, with the token in place of%s.
If your API wants a different header, change both fields, for example header: x-api-key with format: "%s". In v3 kits you must spell the header out; the guide notes that sbx does not use the older scheme shorthand. Also keep this rule in mind: every inject domain must also be allowed by the network policy for the same phase. Injection only sets a header. It cannot open a connection.
Give the agent context
The proxy now knows about the API, but the agent does not. Without instructions, a helpful agent may ask the developer to paste a token, which is the exact habit we want to break. Add an inline agent-context@1 entry:
- type: com.docker.sandbox/agent-context@1
config:
content: |
## Stockroom shipments API
Read shipment status at https://api.stockroom.example.com/v2/.
Auth is handled by the sandbox proxy: send
`Authorization: Bearer $STOCKROOM_API_TOKEN`. The variable is a
placeholder. Do not print it and never ask the user for a token.
Step 4: Store the token and approve the binding
Two host-side actions are required before the proxy will use a third-party kit’s credential, and they are intentionally separate. First, store the value under the service name the kit declares:
sbx secret set stockroom-ledger
The Manage credentials page confirms this pattern for kit-declared services: use the same identifier the kit uses. The value goes to the OS credential store (on headless Linux without a Secret Service, sbx falls back to a permission-protected file). The sbx secret set reference also documents --ref and --command for resolving the value from a password manager or a tool.
Second, create a sandbox with the updated kit from an interactive terminal. Because the kit is not one of Docker’s built-ins, sbx pauses and asks you to approve its request to use the stockroom-ledger credential on the stockroom host:
sbx create docker/sbx-kit-shell:1.0.0 --name inv-tokened --kit ./stockroom-api
Your approval is written to the credential bindings file at ~/.config/sbx/credentials.yaml (%APPDATA%\sbx\credentials.yaml on Windows). The binding stores no secret; it records which mechanism and which domains you approved. If a later kit version asks for a new domain, sbx prompts again.
For CI and --detached runs there is nobody to answer the prompt. Per the guide, sbx then starts the sandbox with the credential withheld and prints a warning instead of failing. Run the kit interactively once, or write the binding into credentials.yaml ahead of time.
Confirm the agent only holds a placeholder
sbx exec inv-tokened -- printenv STOCKROOM_API_TOKEN
sbx exec inv-tokened -- sh -c 'curl -s -H "Authorization: Bearer $STOCKROOM_API_TOKEN" https://api.stockroom.example.com/v2/shipments'
The first command shows a placeholder, not your token. The second request leaves the sandbox carrying that placeholder; the proxy matches the destination against the inject rule, checks your binding, reads the real token from the host store and rewrites the header on the way out.
Step 5: Bound the token with network-policy@2
The kit works, but it is too generous. Any request to the stockroom host, including a POST that cancels a shipment, would get the token. network-policy@2 keeps the same install and runtime blocks but lets an entry restrict a host to specific methods and paths. Replace the @1 entry (a kit declares one version, never both):
- type: com.docker.sandbox/network-policy@2
config:
runtime:
allow:
- hosts: [api.stockroom.example.com]
methods: [GET, HEAD]
paths: [/v2/**]
A bounded entry grants matching HTTP requests, not the whole host. Methods must be uppercase, and if you state paths you must also state methods. Before creating another sandbox, let the kit frontend validate the file strictly; an unknown field or a lowercase method fails the build here instead of inside a sandbox:
docker buildx build -f stockroom-api/stockroom-api.yaml -t stockroom-kit-check stockroom-api/
sbx create docker/sbx-kit-shell:1.0.0 --name inv-readonly --kit ./stockroom-api
The domain has not changed, so the existing binding still covers it. Now test both sides of the boundary:
sbx exec inv-readonly -- sh -c 'curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer $STOCKROOM_API_TOKEN" https://api.stockroom.example.com/v2/shipments'
sbx exec inv-readonly -- sh -c 'curl -s -o /dev/null -w "%{http_code}\n" -X POST -H "Authorization: Bearer $STOCKROOM_API_TOKEN" https://api.stockroom.example.com/v2/shipments/batch-42/cancel'
sbx policy log inv-readonly
In the guide’s run, the read returns 200 and the write returns 403. That 403 comes from the sandbox boundary, not from your API: the request never left the machine, so the token was never attached. sbx policy log accepts an optional sandbox name and shows which hosts were allowed or blocked and the rule that matched.
Local sandboxes vs Cloud Sandboxes
Everything above runs against a local sandbox, where the host-side proxy, your OS keychain and the bindings file all live on your machine. That is what makes the three-way split work: the kit says which hosts and requests, the user supplies the token and the approval, and the proxy combines them per request.
Docker Cloud Sandboxes follow different rules for egress. The sbx create reference states that cloud sandboxes use cloud network policies and that host network and HTTP policies do not apply, and the local vs cloud comparison notes that cloud policies are network-only, without HTTP method and path filtering. In practice that means the network-policy@2 bounds from Step 5 are a local feature. Cloud sandboxes do accept mixins via sbx --cloud create ... --kit, but if your security story depends on method and path limits, validate that design on local sandboxes and check the cloud network policy docs before moving it.
Troubleshooting tips
- Request blocked with no kit or with a typo in the host. Expected under default deny. Check the allow list and run
sbx policy logto see which rule matched. - 401 from the API after adding the network policy. The path is open but no credential is injected yet. Add
credential@1and make sure the inject domain is also in the allow list. - Credential silently missing in CI. Unattended runs cannot approve the binding, so sbx withholds the credential and warns. Approve once interactively or pre-write the binding file.
- 403 on a write you expected to work. That is the
network-policy@2boundary doing its job. Widen methods or paths deliberately, and expect a new review. - Build fails on the descriptor. The frontend is strict: check for lowercase methods, unknown fields, or
pathswithoutmethods. - Kit changes not taking effect. A sandbox keeps its creation-time kit config. Create a new sandbox name after each edit.
Key takeaways
- A content-free v3 mixin can extend the sandbox proxy to any internal API without putting a secret in the image, the repo or the sandbox.
- Egress and credentials are separate grants. You need both, and every inject domain must be allowed.
- Storing a secret and approving its use are separate steps; the bindings file is your audit record of which hosts may carry which token.
- network-policy@2 limits the token to specific methods and paths, which protects you even if the agent is talked into a destructive call.
- Method and path bounds are a local sandbox feature; cloud sandboxes use network-only cloud policies.
References
Source guide: Connect to services without exposing credentials (Docker Learn). Related: Build and customize Docker Sandbox environments learning path, Manage credentials, sbx create, sbx secret set, sbx policy log, Local vs cloud sandboxes, and the network-policy@2 spec.