Skip to content

Working with images

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.

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)

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.

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:

virtx build -t jq:latest .

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

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.

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.

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")

An untagged image stays on the server until it is pruned.

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.

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)

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 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.

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")

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.

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

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.