# Working with images

https://virtx.sh/docs/guides/images/

Build images from recipes, pull and push them, and manage the server's registry.

Every sandbox in virtx boots from an image, the read-only base its root filesystem starts from.
This page covers how to get images onto a server and manage them there: building them, tagging them, pulling and pushing them, copying them between servers, and pruning the ones left behind.

## Building images

An image can be built from a recipe in code, or from a Dockerfile.

### From a recipe

A recipe is a base image plus the steps to run on it: `.run(command)`, `.env(key, value)`, and `.workdir(directory)`, chained in the order they run.

**Python**

```python
recipe = (
    Recipe("alpine:latest")
    .run("apk add --no-cache jq")
    .env("LANG", "C.UTF-8")
    .workdir("/work")
)
digest = await client.build_image(recipe, "jq:latest").complete(print=True)
```

**Node.js**

```js
const recipe = new Recipe('alpine:latest')
  .run('apk add --no-cache jq')
  .env('LANG', 'C.UTF-8')
  .workdir('/work')
const digest = await client.buildImage(recipe, 'jq:latest').complete({ print: true })
```

**Rust**

```rust
let recipe = Recipe::new("alpine:latest")
    .step(Step::run("apk add --no-cache jq"))
    .step(Step::env("LANG", "C.UTF-8"))
    .step(Step::workdir("/work"));
let digest = client.build_image(recipe, "jq:latest").complete(true).await?;
```

### From a Dockerfile

A Dockerfile is read as a recipe: one stage, with only `FROM`, `RUN`, `ENV`, and `WORKDIR`. Any other instruction is refused at its line.

```dockerfile
FROM alpine:latest
RUN apk add --no-cache jq
ENV LANG=C.UTF-8
WORKDIR /work
```

The `virtx` command, which the Python and Node.js packages install, builds the Dockerfile in a directory, or the one `-f` names; see [CLI](https://virtx.sh/docs/reference/cli/):

```sh
virtx build -t jq:latest .
```

In Rust, `Recipe::from_dockerfile` reads a Dockerfile's text into a recipe for `build_image`:

```rust
let recipe = Recipe::from_dockerfile(std::fs::read_to_string("Dockerfile")?)?;
let digest = client.build_image(recipe, "jq:latest").complete(true).await?;
```

### After a build

A build stores its image under the supplied tag. `create_sandbox` boots it by that tag, or by its digest to select exactly that build.

A sandbox takes its image's environment and working directory for every command it runs, and does not run the image's entrypoint or command: start a server the image has with `exec`.

A build, like a pull or a push, is a job.
`complete` follows it to its end, printing its log with `print`, and answers the digest of what it made; a build that fails raises a `VirtxError` whose `source` is `job`, so complete it before booting from the tag.
Iterated instead, it yields its log, and raises the same error in place of its `end` entry.
Awaiting the started value gives the job, whose log can be read again with `job.log()`, as a command's is; see [Running commands](https://virtx.sh/docs/guides/running-commands/#follow-a-job).

## Tagging

A tag is a `name:tag` that points at an image. `tag_image` points a new tag at an image, named by a tag or by `name@algorithm:hex`, moving the tag if it already points elsewhere. `untag_image` removes a tag and leaves the image. `list_image` lists every tag and the digest it points at. `remove_image` removes the image a reference names and every tag on it in that name. While a sandbox or a build uses an image, removing it, or its last tag, is refused with `409` and leaves every tag as it was.

**Python**

```python
await client.tag_image("jq:latest", "jq:1.7")
for item in await client.list_image():
    print(item.reference, item.digest)
await client.untag_image("jq:1.7")
```

**Node.js**

```js
await client.tagImage('jq:latest', 'jq:1.7')
for (const item of await client.listImage()) console.log(item.reference, item.digest)
await client.untagImage('jq:1.7')
```

**Rust**

```rust
client.tag_image("jq:latest", "jq:1.7").await?;
for item in client.list_image().await? {
    println!("{} {}", item.reference, item.digest);
}
client.untag_image("jq:1.7").await?;
```

An untagged image stays on the server until it is [pruned](#prune-the-registry).

## Pull and push

You don't need to build an image to start. `pull_image` copies any image from a registry such as Docker Hub or `ghcr.io` to the server, ready to boot as it is. `push_image` copies one back to the registry its name points at, so tag it with the registry's name first.

When the registry wants credentials, pass them as `auth`: a username with a password or an access token, or a bearer token. Without `auth`, both are anonymous. These credentials are for the registry, not for the virtx server.

**Python**

```python
import os

from virtx import BasicAuth

await client.pull_image("python:3.12-slim").complete(print=True)
sandbox = await client.create_sandbox("python:3.12-slim")

await client.tag_image("jq:latest", "ghcr.io/me/jq:1")
auth = BasicAuth("me", os.environ["GHCR_TOKEN"])
await client.push_image("ghcr.io/me/jq:1", auth).complete(print=True)
```

**Node.js**

```js
await client.pullImage('python:3.12-slim').complete({ print: true })
const sandbox = await client.createSandbox('python:3.12-slim')

await client.tagImage('jq:latest', 'ghcr.io/me/jq:1')
const auth = { type: 'basic', username: 'me', password: process.env.GHCR_TOKEN }
await client.pushImage('ghcr.io/me/jq:1', auth).complete({ print: true })
```

**Rust**

```rust
client.pull_image("python:3.12-slim", None).complete(true).await?;
let sandbox = client
    .create_sandbox(&SandboxCreateArgs::new("python:3.12-slim"))
    .await?;

client.tag_image("jq:latest", "ghcr.io/me/jq:1").await?;
let auth = RegistryAuth::Basic {
    username: "me".into(),
    password: std::env::var("GHCR_TOKEN")?,
};
client.push_image("ghcr.io/me/jq:1", Some(auth)).complete(true).await?;
```

A server run over TCP (`virtx-server-tcp`) connects only to registries on public addresses, for a pull, a push and a build's base alike. A job that names a registry on the server's own machine or its private network fails with `is not reachable from this server`. To use such a registry, list it in the server's `server.json` as the reference names it, `host` or `host:port`: `"private_registries": ["registry.internal:5000"]`. A listed registry that sends the job on to another host on a private address, for its token or for a layer, needs that host listed as well. These connections do not go through an `HTTPS_PROXY` set in the server's environment, so a server whose only way to the internet is such a proxy reaches no registry. The local server reaches whatever your machine does.

Docker Hub limits how often one address may pull without credentials, which a server pulling for many users reaches quickly. Such a server can be given a mirror of Docker Hub in its `server.json`, `"registry_mirror": "mirror.gcr.io"`: a pull of a Docker Hub image without credentials, a build's base included, tries the mirror first and goes to Docker Hub for what the mirror lacks, and the image is tagged with the name the job asked for either way. A tag on the mirror can lag Docker Hub's for a while; a digest names the same image on both. A pull with credentials goes to Docker Hub. A mirror on a private address is listed in `private_registries` too. A server can also share such pulls between its projects, `"share_public_images": true`; see [Share public images](https://virtx.sh/docs/guides/self-hosting/#share-public-images) for what that tells one project of another.

## Copy a local image to a remote server

An image built on your machine can go to a server without a registry in between. `download_image` writes it from the local server to an OCI archive, and `upload_image` sends the archive to the remote server and tags it there, uploading only what that server lacks.

**Python**

```python
from virtx import BearerAuth, Client

async with Client.spawn() as local:
    async for _ in local.download_image("jq:latest", "jq.tar"):
        pass

async with Client.connect(
    "https://virtx.example.com", BearerAuth("<token>")
) as remote:
    async for _ in remote.upload_image("jq.tar", "jq:latest"):
        pass
    sandbox = await remote.create_sandbox("jq:latest")
```

**Node.js**

```js
const local = Client.spawn()
for await (const _ of local.downloadImage('jq:latest', 'jq.tar')) {}
await local.close()

const remote = Client.connect(
  'https://virtx.example.com',
  { type: 'bearer', token: '<token>' }
)
for await (const _ of remote.uploadImage('jq.tar', 'jq:latest')) {}
const sandbox = await remote.createSandbox('jq:latest')
```

**Rust**

```rust
let local = Client::spawn_virtx([] as [&str; 0]).await?;
let mut events = local.download_image("jq:latest", "jq.tar");
while let Some(event) = events.next().await {
    event?;
}

let remote = Client::connect(
    "https://virtx.example.com",
    Auth::Bearer { token: "<token>".into() },
    None,
    [] as [(&str, &str); 0],
)?;
let mut events = remote.upload_image("jq.tar", "jq:latest");
while let Some(event) = events.next().await {
    event?;
}
let sandbox = remote
    .create_sandbox(&SandboxCreateArgs::new("jq:latest"))
    .await?;
```

The archive is a standard OCI image layout, so it can also come from or go to other tools that read one.

## Prune the registry

Rebuilding a tag, like untagging it, leaves the old image on the server. `prune_image` deletes every image no tag points at and no sandbox uses, and the layers only those images held, and answers what it deleted and how many bytes it freed. An image younger than an hour is kept, so one a build or an upload has just written is not deleted before it is tagged.

**Python**

```python
pruned = await client.prune_image()
print(len(pruned.images), "images,", pruned.reclaimed, "bytes")
```

**Node.js**

```js
const pruned = await client.pruneImage()
console.log(pruned.images.length, 'images,', pruned.reclaimed, 'bytes')
```

**Rust**

```rust
let pruned = client.prune_image().await?;
println!("{} images, {} bytes", pruned.images.len(), pruned.reclaimed);
```

From a shell, `virtx image prune` does the same. `virtx-server-tcp` also prunes on its own once an hour; the stdio server prunes only when asked.
