# One-program sandboxes

> Run a single program in a sandbox, set size and seed, wait for exit, read output, and manage its lifecycle.

This is the Markdown of https://docs.dvm.run/guides/programs/. Every page of the dvm.run docs is served as Markdown at its address plus `.md`; the list of pages is https://docs.dvm.run/llms.txt.

Pass `command` or `entrypoint` to run one program in a sandbox, like `docker run`. Unlike a session, it accepts no commands from your code and stdin is closed. Wait for exit, then read output.

* Python

  `python/examples/programs.py`

  ```python
  with Sandbox.create(
      "alpine:3.20",
      command=["sh", "-c", "echo out; echo err >&2; exit 3"],
      name="exits-three",
      cpus=2,
      memory_mib=2048,
      seed=7,
  ) as sandbox:
      print(sandbox.wait())
      output = sandbox.output()
      print(output.stdout)
      print(output.stderr)
      print(sandbox.state().kind)
  ```

* TypeScript

  `js/examples/programs.ts`

  ```ts
  await using sandbox = await Sandbox.create("alpine:3.20", {
    command: ["sh", "-c", "echo out; echo err >&2; exit 3"],
    name: "exits-three",
    cpus: 2,
    memoryMib: 2048,
    seed: 7,
  });
  console.log(await sandbox.wait());
  const output = await sandbox.output();
  console.log(text(output.stdout));
  console.log(text(output.stderr));
  console.log((await sandbox.state()).kind);
  ```

* Rust

  `rust/dvm-run/examples/programs.rs`

  ```rust
  let sandbox = client
      .create_sandbox("alpine:3.20")
      .command(["sh", "-c", "echo out; echo err >&2; exit 3"])
      .name("exits-three")
      .cpus(2)
      .memory_mib(2048)
      .seed(7)
      .await?;
  println!("{}", sandbox.wait().await?);
  let output = sandbox.output().await?;
  println!("{:?}", String::from_utf8_lossy(output.stdout.bytes()));
  println!("{:?}", String::from_utf8_lossy(output.stderr.bytes()));
  println!("{}", sandbox.state().await?.kind());
  ```

## What runs

* `command` is argv, replacing the image’s `CMD`.
* `entrypoint` replaces the image’s `ENTRYPOINT`, with `command` as its arguments.

- Python

  `python/examples/programs.py`

  ```python
  with Sandbox.create("alpine:3.20", entrypoint="sh", command=["-c", "uname -a"]) as sandbox:
      sandbox.wait()
      print(sandbox.output().stdout.decode(), end="")
  ```

- TypeScript

  `js/examples/programs.ts`

  ```ts
  await using sandbox = await Sandbox.create("alpine:3.20", {
    entrypoint: "sh",
    command: ["-c", "uname -a"],
  });
  await sandbox.wait();
  console.log(text((await sandbox.output()).stdout));
  ```

- Rust

  `rust/dvm-run/examples/programs.rs`

  ```rust
  let sandbox = client
      .create_sandbox("alpine:3.20")
      .entrypoint("sh")
      .command(["-c", "uname -a"])
      .await?;
  sandbox.wait().await?;
  print!(
      "{}",
      String::from_utf8_lossy(sandbox.output().await?.stdout.bytes())
  );
  ```

The program inherits the image’s environment, user, and working directory, plus any `env` you pass ([Environment and secrets](/guides/environment/)). Images must be public; pulls are anonymous.

## Size, seed, and name

|                                    | Default        | Range                                                                       |
| ---------------------------------- | -------------- | --------------------------------------------------------------------------- |
| `cpus`                             | 1              | 1 to 8 vCPUs                                                                |
| memory (`memory_mib`, `memoryMib`) | 1024 MiB       | 128 to 16384 MiB                                                            |
| `seed`                             | 0              | 0 to 253 − 1                                                                |
| `name`                             | from the image | up to 63 lowercase letters, digits and `-`, not starting or ending with `-` |

Your plan may cap resources below these ranges; larger requests fail with your plan’s limit ([Plans, limits and credit](/concepts/plans/)). The same image, command, and seed run the same way, except where the program reads something from outside that differs, such as the network ([What a recording holds](/concepts/recording/)). Name is a label for you.

These options also apply to a [session](/guides/sessions/) (a sandbox accepting commands).

## Wait and read output

* `wait` returns the exit code once the program exits, or raises the matching error if stopped or failed ([Errors](/reference/errors/)).
* `output` returns stdout and stderr written so far, as bytes.

For a [session](/guides/sessions/), `output` returns the session server’s response frames rather than command output ([The sandbox protocol](/reference/wire/)). Use `run` there.

## Pause, resume, stop, and reconnect

* Python

  `python/examples/programs.py`

  ```python
  with Sandbox.create("alpine:3.20", command=["sh", "-c", "while :; do :; done"]) as sandbox:
      sandbox.pause()
      print(sandbox.state().kind)
      sandbox.resume()

      elsewhere = Sandbox.connect(sandbox.id)
      elsewhere.stop()
      elsewhere.close()
      print(sandbox.state().kind)
  ```

* TypeScript

  `js/examples/programs.ts`

  ```ts
  await using sandbox = await Sandbox.create("alpine:3.20", {
    command: ["sh", "-c", "while :; do :; done"],
  });
  await sandbox.pause();
  console.log((await sandbox.state()).kind);
  await sandbox.resume();

  const elsewhere = await Sandbox.connect(sandbox.id);
  await elsewhere.stop();
  console.log((await sandbox.state()).kind);
  ```

* Rust

  `rust/dvm-run/examples/programs.rs`

  ```rust
  let sandbox = client
      .create_sandbox("alpine:3.20")
      .command(["sh", "-c", "while :; do :; done"])
      .await?;
  sandbox.pause().await?;
  println!("{}", sandbox.state().await?.kind());
  sandbox.resume().await?;

  let elsewhere = client.connect_sandbox(sandbox.id()).await?;
  elsewhere.stop().await?;
  println!("{}", sandbox.state().await?.kind());
  ```

- `pause` freezes the sandbox; `resume` continues it.
- `stop` terminates it (no-op if already ended). Stopped sandboxes cannot be inspected; exited ones can.
- `state` returns `queued`, `running`, or `paused`, then `exited` (with exit code), `stopped`, or `failed` (with reason).
- `connect` attaches another handle by sandbox ID from any process using an API key on the same account.

A sandbox that never exits runs until its time limit, then fails with that reason ([Plans, limits and credit](/concepts/plans/)). Exiting a `with` block, `await using`, or `scope` stops a running one-program sandbox.
