# Architecture

A deep dive into phases, versioned and layered images for containers and micro-VMs.

Beams are Kata containers that run in Cloud Hypervisor micro-VMs.

Phases are versioned and layered images for beam runtimes. A phase holds filesystem state and its build instructions.

Phases deliver a Git-like UX for container filesystems, combining continuous updates with persistent snapshots that you can fork and rebase from. 

Unlike docker using layers to cache often stale build command results, phases use layers to represent reusable build stages, 
which beams rebuild automatically or users create interactively, like VM snapshots.

Phases let you:

- Define build stages in YAML, with automated periodic rebuilds.
- Write to `/home` interactively, and take snapshots.
- Copy local files into the container and persist them.
- Receive automatic updates to base layers via rebasing.

You can apply these steps in any order, creating a pipeline that composes mutable, immutable, and regenerated state, 
with rules that prevent unsafe mutations and data corruption. 

Each step is called a **phase**. When a parent image refreshes, child phases compose a new image on top of it. Only rebuild phases rerun commands.

<!-- illustration
id: phase-image-refresh
style: docs-isometric
type: animated
title: Phase cycle
description: On refresh, regenerate rebuild layers and retain snapshot layers.

Show one layer set and one manifest refreshing after a scheduled or upstream update, rather than separate before/after diagrams. The manifest directly references five layers in order: base, rebuild outside /home, snapshot /home, rebuild outside /home, snapshot /home. Animate replacement of the base and rebuild solids and update their stationary HTML hash slots: 83c3f21a... becomes 4b6a09ef..., b0193e72... becomes 6ac8f931..., and d01a520c... becomes 91eb604a.... After the regenerated layers arrive, update the manifest from sha256:f001a89c... to sha256:2e74b90c.... Keep snapshot plates and their hashes c0174a0b... and e01721d8... visible and stationary throughout.
-->

## Overview

There are three types of phases:

- **Snapshot phases**, which persist your changes in `/home` in durable (but editable and forkable) layers. You can edit these until descendant snapshot phases depend on them -- then you have to fork.
- **Rebuild phases**, which periodically rerun commands to make changes outside of `/home` (like a Dockerfile on a timer). You can change the commands between rebuilds.
- **Copy phases**, which supply fixed sets of files outside of `/home` (base image overlays, like Dockerfile's `COPY --link`). You can always edit these.

<!-- illustration
id: phase-filesystem-areas
style: docs-isometric
type: static
title: Filesystem areas
description: Separate saved home files from the paths managed by rebuild and copy phases.
footer:

Port the blog's filesystem areas diagram. Show root / branching into an outside-home rebuild area labeled /usr/, /etc/, ... and a separate snapshot area /home/beams/. Nest a copy area /opt/dev/ inside the rebuild area to show that copy and rebuild phases can write overlapping paths outside /home. Label the paths and roles for rebuilds, copy and saved home. Do not imply that /opt/dev is exclusively owned by copy phases.
-->

Let's review a couple of examples. If you want to run the commands yourself, set up `tsh` in your local terminal first.

**Install the client tools and sign in**

Beams is a [Teleport](https://goteleport.com) project and uses [official Teleport client tools](https://goteleport.com/download/client-tools/).

The client tools include `tsh` to sign in and run commands, and `tctl` to administer your cluster if you have permission.



Cluster: `example.beams.run`. Edit this example to use your cluster name.

Install version **18.11.10** of the client:

**macOS**

Download and open the macOS installer, then follow its instructions.

```sh
curl -O https://cdn.teleport.dev/teleport-tools-18.11.10.pkg
open teleport-tools-18.11.10.pkg
```

**Linux**

For Linux x86-64, download and install the client tools:

```sh
curl -O https://cdn.teleport.dev/teleport-v18.11.10-linux-amd64-bin.tar.gz
tar -xzf teleport-v18.11.10-linux-amd64-bin.tar.gz
sudo ./teleport/install
```

**Windows**

For Windows x86-64, run these commands in PowerShell. Add the extraction directory (`$env:LOCALAPPDATA\Teleport`) to your user PATH to use `tsh` in future terminals; the last command adds it to this terminal's PATH.

```powershell
Invoke-WebRequest -Uri https://cdn.teleport.dev/teleport-v18.11.10-windows-amd64-bin.zip -OutFile teleport.zip
Expand-Archive -Path teleport.zip -DestinationPath "$env:LOCALAPPDATA\Teleport" -Force
$env:Path += ";$env:LOCALAPPDATA\Teleport"
```

Follow the official client [tools install guide](https://goteleport.com/download/client-tools/) for other versions and architectures.

Check that you’re running v18.11.10 of the client with `tsh version`.

```sh
tsh version
```

**Passwordless**

I sign in without a password (e.g. a passkey).

```sh
tsh login --proxy='example.beams.run' --auth=passwordless
```

**Password \& MFA**

I have a password & MFA set up for my account:

Replace `you@example.com` with your Teleport username.

```sh
tsh login --proxy='example.beams.run' --user=you@example.com
```

**SSO**

I sign in with SSO (e.g. Okta, GitHub, SAML).

```sh
tsh login --proxy='example.beams.run'
```

### MicroVMs with snapshots and forks

A snapshot saves files, not running processes or memory. A fork gives you an independent copy of those files. For example, create a workspace from your local terminal:

**Local terminal**

```sh
tsh phase add workspace --from=default:v1
```

Inside the build beam, save a file:

**Inside the workspace build beam**

```sh
printf '%s\n' 'original workspace' > ~/notes.txt
tsh phase snapshot --final
```

Back in your local terminal, open a fork of the saved workspace:

**Local terminal**

```sh
tsh phase add beak-buttons --fork-from=workspace
```

Inside the fork, `cat ~/notes.txt` should print `original workspace`. Save a different value:

**Inside the beak-buttons build beam**

```sh
printf '%s\n' 'beak buttons experiment' > ~/notes.txt
tsh phase snapshot --final
```

Now start another fork from `workspace` with `tsh phase add flock-chat --fork-from=workspace` in your local terminal. In that build beam, `cat ~/notes.txt` should still print `original workspace`; the first fork changed only its own copy. For more, see [Forks](/docs/phases/forks/).

<!-- illustration
id: phase-workspace-forks
style: docs-isometric
type: static
title: Two independent forks
description: Fork saved workspace files into independent environments with a shared base.
footer: Solid routes: environment parent. Dashed routes: fork saved workspace files. Each fork has independent compute; processes and memory are not copied.

Port the blog's agents working on two independent forks diagram. Show base as the common environment parent of workspace, beak-buttons and flock-chat with solid parent routes. Show separate dashed fork routes from the saved workspace to beak-buttons and flock-chat. Draw stationary saved-file plates at the source and independent destination plates with compute blocks. Any copy animation must arrive before destination compute becomes ready, leave the source intact, and never transfer keys, processes or memory. Label the source and both forks by name.
-->

### Compose a copy, rebuild, and snapshot pipeline

This example builds a small text page without a repository, package install, or external service. Use a new directory on your workstation. Save this file as `message.txt`:

```text
Hello from the copy layer
```

Save this as `phase.yaml` in the same directory. The **copy** layer adds the message under `/opt/dev`. The **rebuild** layer prepares a display version outside `/home` using a command that can rerun when the base changes.

**phase.yaml**

```yaml
version: v1
metadata:
  name: dev-files
  from: default:v1
files:
  - from: ./message.txt
    to: /opt/dev/message.txt
---
version: v1
metadata:
  name: environment
  from: dev-files
commands:
  - shell: tr '[:lower:]' '[:upper:]' < /opt/dev/message.txt > /opt/dev/banner.txt
```

Apply the configuration, then create the `dev` **snapshot** phase on top of it:

**Local terminal**

```sh
tsh phase apply -f phase.yaml &&
  tsh phase add dev --from=environment
```

In the build beam that opens, save the part you want to edit interactively:

**Inside the dev build beam**

```sh
printf '%s\n' 'Saved in the snapshot' > ~/caption.txt
tsh phase snapshot --final
```

Finally, add a second **rebuild** layer that reads both inputs and assembles the page. Save this as `app.yaml` on your workstation:

**app.yaml**

```yaml
version: v1
metadata:
  name: app
  from: dev
commands:
  - shell: cat /opt/dev/banner.txt /home/beams/caption.txt > /opt/dev/page.txt
```

Apply it and start a fresh beam from the resulting image:

**Local terminal**

```sh
tsh phase apply -f app.yaml &&
  tsh beams add --phase=app
```

Inside the new beam, verify the copy input, the snapshot input, and the assembled output:

**Inside the app beam**

```sh
grep -Fx 'Hello from the copy layer' /opt/dev/message.txt &&
  grep -Fx 'Saved in the snapshot' ~/caption.txt &&
  printf '%s\n' 'HELLO FROM THE COPY LAYER' 'Saved in the snapshot' | cmp - /opt/dev/page.txt
```

The check prints the two input lines and exits successfully if `page.txt` contains exactly the uppercase banner followed by the saved caption.

<!-- illustration
id: phase-editable-pipeline
style: docs-isometric
type: static
title: An editable build pipeline
description: Compose copy, rebuild, and editable snapshot stages in one pipeline.
footer: The middle snapshot remains editable. The saved caption stays in dev while the banner and final page are regenerated.

Show the ordered chain base image -> dev-files copy -> environment rebuild -> dev snapshot -> app rebuild. Label the generated banner and final page. Show tsh phase edit dev pointing to the still-editable middle snapshot and saving dev triggering an app rebuild. Do not imply that running processes are saved or that the dev snapshot is replaced when its parent updates.
-->

**`dev` is still editable**, even though `app` depends on it. From your workstation, run `tsh phase edit dev`. In the build beam that opens, change `~/caption.txt` and run `tsh phase snapshot --final` again. Saving `dev` triggers a rebuild of `app`. After the rebuild succeeds, a new beam from `app` will have the same banner and the updated caption in `/opt/dev/page.txt`. Beams that are already running keep their original image.

Phases are rebuilt continuously, with upstream changes triggering downstream changes. Only rebuild phases need to execute build commands during a refresh. Snapshot and copy phases are quickly stitched on top of new layers by updating image registry metadata.

By default, snapshots own `/home`, and rebuilds own everything else. You can also give a rebuild phase specific paths inside `/home`, like `node_modules`, using `metadata.rebuild_home_paths`. Later snapshots must leave those paths alone.

Phases provide a single model for container persistence and security patching that covers agentic development workflows, dev environments, building apps, and whatever else you do in containers.

## Compatibility

Updating dependencies underneath a saved workspace is only useful if you can control which updates you get. For Teleport Beams, we start with a stable Debian base image, so OS security fixes arrive within an ABI-stable release. Rebuild phases rerun every 24 hours to pick up updates allowed by the commands you specify in phase.yaml, while snapshots preserve your work on top.

Tests can run as part of rebuilds as well, including long integration tests that require access to other services connected to Teleport. If the build fails, the previous published image stays available. You choose which updates to accept and how to test them in `phase.yaml`, and phases handle rebuilding the environment around your saved work.

## How it works

The phases model can be implemented on any Linux-based platform that supports raw container primitives like OverlayFS, but here's how we've implemented it for Teleport Beams.

Builds run in a Kubernetes pod, isolated in a Cloud Hypervisor microVM using Kata Containers. The pod has an unprivileged (but VM-like) user container and a privileged builder container. Before user code starts, a trusted launcher passes open file descriptors for the user container's mount namespace, root, and cgroup to the builder over a Unix socket (`SCM_RIGHTS`). The builder enters that mount namespace, mounts an OverlayFS filesystem, and uses `pivot_root` to switch roots and detach the old root. The setup socket is then hidden from the user environment.

<!-- illustration
id: phase-build-internals
style: docs-isometric
type: static
title: Build setup and snapshot capture
description: Isolated setup and snapshot capture inside a microVM, with an external OCI registry.
footer: Only filesystem layers are saved, not running processes or memory. The workload cannot use the private setup socket.

Port the blog's build pod and registry diagram. Inside a Kubernetes pod / Cloud Hypervisor microVM boundary, distinguish an unprivileged user container from a privileged builder. Before user code, the trusted launcher passes mount namespace, root and cgroup file descriptors via SCM_RIGHTS to a private setup socket. The builder performs setns -> OverlayFS -> pivot_root, detaches the old root and hides the setup socket, then the user workload uses the new OverlayFS root. A later snapshot request uses a separate user socket; the builder freezes the workload cgroup, captures a layer and thaws before uploading to an OCI registry outside the pod. Label ggcr upload / rebase and layers + manifests. Show the captured layer as a separate plate and indicate the direction of each operation. No keys, memory or running processes are saved, no real operations execute, and no private setup access is implied for the running workload.
-->

For snapshots, the builder freezes the workload's cgroup to quiesce filesystem writes, captures the writable layer, then thaws it before uploading. Image assembly, rebasing, and registry operations use the `go-containerregistry` (ggcr) library to manipulate layers and manifests directly on the registry.

## Retention and access control

Teleport Beams automatically garbage collects data: unused images are deleted in less than **2 days**.
Once cloud tenant is suspended, all data in image registry is deleted in less than **3 weeks**.

Each region has a dedicated ECR repository in that region. Snapshots are region-locked and access to them is not possible outside of that region. 
Beams cannot pull, upload, or download snapshots across regions.

Users cannot directly pull, download, or upload snapshots, including from inside of a beam, regardless of region.
