> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tokenfactory.nebius.com/llms.txt
> Use this file to discover all available pages before exploring further.

# run - Execute a command in the sandbox

Spawn a sandbox instance from the session image and execute a command.

## Help output

<div className="contree-terminal" style={{"border":"1px solid rgba(255,255,255,0.15)","borderRadius":8,"overflow":"hidden","margin":"1rem 0"}}><div style={{"background":"#292929","height":24,"display":"flex","alignItems":"center","padding":"0 12px","position":"relative"}}><div style={{"display":"flex","alignItems":"center","flexShrink":0}}><span style={{"display":"inline-block","width":10,"height":10,"borderRadius":"50%","background":"#ff5f57","marginRight":6}} /><span style={{"display":"inline-block","width":10,"height":10,"borderRadius":"50%","background":"#febc2e","marginRight":6}} /><span style={{"display":"inline-block","width":10,"height":10,"borderRadius":"50%","background":"#28c840"}} /></div><div style={{"position":"absolute","left":0,"right":0,"textAlign":"center","fontFamily":"-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif","fontWeight":"bold","color":"#999"}}>\$ contree run --help</div></div><pre style={{"margin":0,"borderRadius":0,"padding":"8px 10px","background":"#1e1e1e","overflowX":"auto"}}><code style={{"fontFamily":"JetBrains Mono, SF Mono, SFMono-Regular, Menlo, Monaco, Cascadia Mono, Segoe UI Mono, Roboto Mono, Oxygen Mono, Ubuntu Monospace, Source Code Pro, Fira Mono, Droid Sans Mono, Consolas, Courier New, monospace","color":"#c5c8c6","whiteSpace":"pre-wrap"}}><span style={{"color":"#61afef","fontWeight":"bold"}}>usage: </span><span style={{"color":"#c678dd","fontWeight":"bold"}}>contree run</span><span style={{"color":"#c5c8c6"}}> \[</span><span style={{"color":"#98c379"}}>-h</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-t </span><span style={{"color":"#e5c07b"}}>TIMEOUT</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-C </span><span style={{"color":"#e5c07b"}}>CWD</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-e </span><span style={{"color":"#e5c07b"}}>ENV</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-H </span><span style={{"color":"#e5c07b"}}>HOSTNAME</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-D</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-I</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-s</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-F </span><span style={{"color":"#e5c07b"}}>FILE</span><span style={{"color":"#c5c8c6"}}>]</span><br /><span style={{"color":"#c5c8c6"}}>                   \[</span><span style={{"color":"#56b6c2"}}>--file-excludes </span><span style={{"color":"#e5c07b"}}>PATTERN \[PATTERN ...]</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-T </span><span style={{"color":"#e5c07b"}}>TRUNCATE</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#56b6c2"}}>--preserve-env</span><span style={{"color":"#c5c8c6"}}>] \[</span><span style={{"color":"#98c379"}}>-d</span><span style={{"color":"#c5c8c6"}}>]</span><br /><span style={{"color":"#c5c8c6"}}>                   \[</span><span style={{"color":"#56b6c2"}}>--use </span><span style={{"color":"#e5c07b"}}>IMAGE</span><span style={{"color":"#c5c8c6"}}>]</span><br /><span style={{"color":"#c5c8c6"}}>                   </span><span style={{"color":"#98c379"}}>...</span><br /><br /><span style={{"color":"#c5c8c6"}}>Spawn a sandbox instance from the current session image and execute a command.</span><br /><br /><span style={{"color":"#c5c8c6"}}>Uses the image from the active session (set via \`contree use IMAGE\`),</span><br /><span style={{"color":"#c5c8c6"}}>or an image specified inline via \`\`--use IMAGE\`\`.</span><br /><span style={{"color":"#c5c8c6"}}>Commands are passed after -- separator; without --, the first</span><br /><span style={{"color":"#c5c8c6"}}>positional arg is the command.</span><br /><br /><span style={{"color":"#c5c8c6"}}>By default the CLI polls until the operation reaches a terminal</span><br /><span style={{"color":"#c5c8c6"}}>status (SUCCESS, FAILED, CANCELLED) and prints stdout/stderr.</span><br /><span style={{"color":"#c5c8c6"}}>Use -d/--detach to exit immediately after spawning.</span><br /><br /><span style={{"color":"#c5c8c6"}}>File attachments:</span><br /><span style={{"color":"#c5c8c6"}}>  Use --file to inject host files or directories into the sandbox</span><br /><span style={{"color":"#c5c8c6"}}>  before execution. Files are uploaded to the API (with SHA256 dedup)</span><br /><span style={{"color":"#c5c8c6"}}>  and mounted at the specified instance path. Ownership and</span><br /><span style={{"color":"#c5c8c6"}}>  permissions default to host file stat unless overridden.</span><br /><br /><span style={{"color":"#c5c8c6"}}>  Note: non-disposable runs persist filesystem changes into a</span><br /><span style={{"color":"#c5c8c6"}}>  new image. Files attached once are already part of that image</span><br /><span style={{"color":"#c5c8c6"}}>  and do not need re-attachment. Use --disposable to discard</span><br /><span style={{"color":"#c5c8c6"}}>  changes after execution.</span><br /><br /><span style={{"color":"#c5c8c6"}}>  Format: host\_path\[:instance\_path]\[:uUID]\[:gGID]\[:mMODE]</span><br /><br /><span style={{"color":"#c5c8c6"}}>    host\_path                             all defaults from stat</span><br /><span style={{"color":"#c5c8c6"}}>    host\_path:/inst/path                  point a destination path</span><br /><span style={{"color":"#c5c8c6"}}>    host\_path:m0755                       override only mode</span><br /><span style={{"color":"#c5c8c6"}}>    host\_path:/inst/path:u0:g0:m0755      all explicit</span><br /><span style={{"color":"#c5c8c6"}}>    host\_path:uroot:groot                 uid/gid by name (local)</span><br /><br /><span style={{"color":"#c5c8c6"}}>  Tagged options (u/g/m) can appear in any order after host\_path.</span><br /><span style={{"color":"#c5c8c6"}}>  instance\_path is detected by its leading /.</span><br /><span style={{"color":"#c5c8c6"}}>  For directory attachments, files are walked recursively and default</span><br /><span style={{"color":"#c5c8c6"}}>  excludes are applied: .\*, .git, \*.pyc, \_\_pycache\_\_, .venv,</span><br /><span style={{"color":"#c5c8c6"}}>  .mypy\_cache, .pytest\_cache, node\_modules, dist, build.</span><br /><span style={{"color":"#c5c8c6"}}>  Add extra patterns with --file-excludes.</span><br /><br /><span style={{"color":"#c5c8c6"}}>  The CLI also keeps a local upload cache keyed by</span><br /><span style={{"color":"#c5c8c6"}}>  path+inode+mtime+size and reuses known file UUIDs to avoid</span><br /><span style={{"color":"#c5c8c6"}}>  unnecessary re-upload checks/uploads.</span><br /><br /><span style={{"color":"#c5c8c6"}}>  Note: named uid/gid (e.g. uroot) are resolved locally via</span><br /><span style={{"color":"#c5c8c6"}}>  pwd/grp — use numeric IDs if unsure about host/sandbox mismatch.</span><br /><br /><span style={{"color":"#61afef","fontWeight":"bold"}}>positional arguments:</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>command\_args</span><span style={{"color":"#c5c8c6"}}>          Command and arguments (after --)</span><br /><br /><span style={{"color":"#61afef","fontWeight":"bold"}}>options:</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-h</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--help</span><span style={{"color":"#c5c8c6"}}>            show this help message and exit</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-t</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--timeout</span><span style={{"color":"#c5c8c6"}}> </span><span style={{"color":"#e5c07b","fontWeight":"bold"}}>TIMEOUT</span><br /><span style={{"color":"#c5c8c6"}}>                        Timeout in seconds (default: 120)</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-C</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--cwd</span><span style={{"color":"#c5c8c6"}}> </span><span style={{"color":"#e5c07b","fontWeight":"bold"}}>CWD</span><span style={{"color":"#c5c8c6"}}>         Working directory inside sandbox, absolute path or empty string for use</span><br /><span style={{"color":"#c5c8c6"}}>                        sandbox WORKDIR (default: )</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-e</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--env</span><span style={{"color":"#c5c8c6"}}> </span><span style={{"color":"#e5c07b","fontWeight":"bold"}}>ENV</span><span style={{"color":"#c5c8c6"}}>         Environment variable KEY=VALUE (repeatable)</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-H</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--hostname</span><span style={{"color":"#c5c8c6"}}> </span><span style={{"color":"#e5c07b","fontWeight":"bold"}}>HOSTNAME</span><br /><span style={{"color":"#c5c8c6"}}>                        Container hostname (default: linuxkit)</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-D</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--disposable</span><span style={{"color":"#c5c8c6"}}>      Drop filesystem changes after run</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-I</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--interpreter</span><span style={{"color":"#c5c8c6"}}>     Interpreter (shebang) mode. Read the script file given as the first</span><br /><span style={{"color":"#c5c8c6"}}>                        argument, strip the #! line, and send the body as stdin to /bin/sh -s.</span><br /><span style={{"color":"#c5c8c6"}}>                        Usage: #!/usr/bin/env -S contree run -I</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-s</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--shell</span><span style={{"color":"#c5c8c6"}}>           Join command args into a single shell expression</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-F</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--file</span><span style={{"color":"#c5c8c6"}}> </span><span style={{"color":"#e5c07b","fontWeight":"bold"}}>FILE</span><span style={{"color":"#c5c8c6"}}>       Attach file or directory (repeatable, dirs recurse). Format:</span><br /><span style={{"color":"#c5c8c6"}}>                        host\[:inst\_path]\[:uUID]\[:gGID]\[:mMODE]. Tagged options (u/g/m) in any</span><br /><span style={{"color":"#c5c8c6"}}>                        order; uid/gid resolved locally from pwd/grp; defaults from host stat.</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--file-excludes</span><span style={{"color":"#c5c8c6"}}> </span><span style={{"color":"#e5c07b","fontWeight":"bold"}}>PATTERN \[PATTERN ...]</span><br /><span style={{"color":"#c5c8c6"}}>                        Additional glob exclude patterns for directory attachments (repeatable).</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-T</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--truncate</span><span style={{"color":"#c5c8c6"}}> </span><span style={{"color":"#e5c07b","fontWeight":"bold"}}>TRUNCATE</span><br /><span style={{"color":"#c5c8c6"}}>                        Truncate output to N bytes (default: 65536)</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--preserve-env</span><span style={{"color":"#c5c8c6"}}>        Preserve env vars from previous run (server-side)</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#98c379","fontWeight":"bold"}}>-d</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--detach</span><span style={{"color":"#c5c8c6"}}>, </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--no-wait</span><br /><span style={{"color":"#c5c8c6"}}>                        Exit immediately after spawning (do not wait for result)</span><br /><span style={{"color":"#c5c8c6"}}>  </span><span style={{"color":"#56b6c2","fontWeight":"bold"}}>--use</span><span style={{"color":"#c5c8c6"}}> </span><span style={{"color":"#e5c07b","fontWeight":"bold"}}>IMAGE</span><span style={{"color":"#c5c8c6"}}>           Switch session to IMAGE before running (UUID or tag:NAME). Equivalent to</span><br /><span style={{"color":"#c5c8c6"}}>                        'contree use IMAGE' followed by 'run'. Recorded in session history and can</span><br /><span style={{"color":"#c5c8c6"}}>                        be rolled back with 'session rollback'. (default: )</span><br /><br /><span style={{"color":"#c5c8c6"}}>examples:</span><br /><span style={{"color":"#c5c8c6"}}>  contree use ubuntu && contree run -- uname -a</span><br /><span style={{"color":"#c5c8c6"}}>  contree run --use tag:ubuntu:latest -- uname -a</span><br /><span style={{"color":"#c5c8c6"}}>  contree run --shell -- 'echo hello && ls /'</span><br /><span style={{"color":"#c5c8c6"}}>  contree run -e FOO=bar DEBUG=1 -- ./app</span><br /><span style={{"color":"#c5c8c6"}}>  contree run --file ./app.py:/app.py --disposable -- python /app.py</span><br /><span style={{"color":"#c5c8c6"}}>  contree run --file ./src:/app/src --file-excludes '\*.log' -- make -C /app/src</span><br /><span style={{"color":"#c5c8c6"}}>  contree run -d -- sleep 3600</span><br /><br /><span style={{"color":"#c5c8c6"}}>for coding agents:</span><br /><span style={{"color":"#c5c8c6"}}>  \`run\` executes remotely inside the instance image (not on local host)</span><br /><span style={{"color":"#c5c8c6"}}>  local files/dirs must be mapped with --file to be available remotely</span><br /><span style={{"color":"#c5c8c6"}}>  mutates session image unless --disposable is set</span><br /><span style={{"color":"#c5c8c6"}}>  supports directory attachments via --file host\_dir:/instance\_dir</span><br /><span style={{"color":"#c5c8c6"}}>  local file cache avoids re-upload when path+inode+mtime+size unchanged</span><br /><span style={{"color":"#c5c8c6"}}>  returns command exit code when available</span><br /><span style={{"color":"#c5c8c6"}}>  default formatter prints raw stdout/stderr only</span><br /><span style={{"color":"#c5c8c6"}}>  use -f json for structured operation metadata</span><br /><br /><span style={{"color":"#c5c8c6"}}>agent note:</span><br /><span style={{"color":"#c5c8c6"}}>  Before using this command in an automated workflow, read:</span><br /><span style={{"color":"#c5c8c6"}}>    contree agent</span></code></pre></div>

## Quick start with `--use`

Switch session to an image and run a command in one step:

```bash theme={null}
contree run --use tag:ubuntu:latest -- uname -a
```

This is equivalent to:

```bash theme={null}
contree use tag:ubuntu:latest
contree run -- uname -a
```

The image switch is recorded in session history and can be rolled back
with `contree session rollback`.

## Execution modes

**Direct command** (default):

```bash theme={null}
contree run uname -a
```

**Shell mode** (`-s` / `--shell`):

```bash theme={null}
contree run -s -- 'echo hello && ls /'
```

Joins all command args into a single shell expression.

**Interpreter mode** (`-I` / `--interpreter`):

```bash theme={null}
contree run -I ./script.sh
```

Reads a local script, strips the `#!` line, and sends the body as stdin
to `/bin/sh -s`. Enables shebang scripts:

```bash theme={null}
#!/usr/bin/env -S contree run -I
echo "runs inside a ConTree sandbox"
```

**Piped stdin**:

```bash theme={null}
echo 'uname -a' | contree run /bin/sh
```

When stdin is not a TTY, it is read, base64-encoded, and sent as the
`stdin` field.

## Lifecycle

1. Resolve the session image (or switch to `--use IMAGE` first)
2. Upload any `--file` attachments (with SHA256 dedup)
3. Merge pending files from `contree file edit`/`cp`
4. POST `/v1/instances`
5. Poll until terminal status (unless `-d`)
6. Print stdout/stderr; propagate the exit code

On Ctrl-C the operation is cancelled via DELETE.

See [Working with Files](../tutorial/files) for `--file` syntax details.
