Skip to content

Environment and secrets

Use this page to pass environment variables to a whole sandbox or to an individual command, and to see what happens to a secret you pass in, given that every run is recorded.

Setting env on a sandbox adds variables to the image’s environment for every process inside it. Setting env on a command adds variables for that command alone, overriding any sandbox variable with the same name.

python/examples/environment.py
with Sandbox.create("alpine:3.20", env={"STAGE": "test", "LOG_LEVEL": "debug"}) as sandbox:
print(sandbox.run('echo "$STAGE $LOG_LEVEL"').stdout)
print(sandbox.run('echo "$STAGE"', env={"STAGE": "override"}).stdout)
print(sandbox.spec["environment"])

Sandbox variable names must contain only letters, digits, and _, cannot start with a digit, and must be unique; their values cannot contain a NUL byte. Command variable names cannot be empty or contain =.

The sandbox’s spec, returned when you create or fetch the sandbox, includes its sandbox-level environment variables and their values.

When a single command needs a secret, pass it to that command from your own environment instead of setting it on the whole sandbox. That keeps it out of the sandbox’s spec, though it is still stored with the commands you sent and in the recording.

python/examples/environment.py
token = os.environ.get("EXAMPLE_TOKEN", "not-a-real-token")
with Sandbox.create("alpine:3.20") as sandbox:
result = sandbox.run('test -n "$TOKEN" && echo has a token', env={"TOKEN": token})
print(result.stdout)

Deleting a sandbox in the panel deletes its record, output, and recording. See the privacy policy for what dvm.run retains and for how long.