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.
Environment variables
Section titled “Environment variables”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.
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"])await using sandbox = await Sandbox.create("alpine:3.20", { env: { STAGE: "test", LOG_LEVEL: "debug" },});console.log(text((await sandbox.run('echo "$STAGE $LOG_LEVEL"')).stdout));console.log(text((await sandbox.run('echo "$STAGE"', { env: { STAGE: "override" } })).stdout));console.log(sandbox.spec.environment);let sandbox = client .create_sandbox("alpine:3.20") .env("STAGE", "test") .env("LOG_LEVEL", "debug") .await?;sandbox .scope(async |sandbox| { let both = sandbox.run(r#"echo "$STAGE $LOG_LEVEL""#).await?; print!("{}", String::from_utf8_lossy(&both.stdout)); let one = sandbox .run(r#"echo "$STAGE""#) .env("STAGE", "override") .await?; print!("{}", String::from_utf8_lossy(&one.stdout)); println!("{:?}", sandbox.spec().environment()); Ok(()) }) .await?;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.
Secrets
Section titled “Secrets”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.
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)const token = process.env.EXAMPLE_TOKEN ?? "not-a-real-token";await using sandbox = await Sandbox.create("alpine:3.20");const result = await sandbox.run('test -n "$TOKEN" && echo has a token', { env: { TOKEN: token } });console.log(text(result.stdout));let token = std::env::var("EXAMPLE_TOKEN").unwrap_or_else(|_| "not-a-real-token".to_owned());let sandbox = client.create_sandbox("alpine:3.20").await?;sandbox .scope(async |sandbox| { let result = sandbox .run(r#"test -n "$TOKEN" && echo has a token"#) .env("TOKEN", token) .await?; print!("{}", String::from_utf8_lossy(&result.stdout)); Ok(()) }) .await?;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.