Making packages
Say once what a sandbox is made of and keep it under a name, then make as many sandboxes of it as you need.
A package is a sandbox said once and kept under a name: its image, its size, its ports and volumes. Anyone it lets in makes a sandbox of it with one call, as many times as they ask, each sandbox the same.
Use packages to:
- Give every user of your app a sandbox of the same image and size, made from the app itself, with no server of your own in between.
- Let teammates make the sandbox a task needs without repeating its options.
- Change what the next sandboxes are made of in one place.
This page keeps a package and makes sandboxes of it as a member of its project. Letting your users in lets others make them too, and Building an app on a package is the app’s side.
Before you start
A package lives in a project, as everything on a server does, and names an image that project already has.
Build the image, or pull it, on the server the package will be on, as Working with images shows.
An image you built on your own machine goes to a remote server with upload_image.
Client.spawn() keeps packages too, so you can try one on your own machine first.
Only a server reached over HTTP lets anyone but you make a sandbox of it.
Keep a package
put_package takes a name, an image, and the options create_sandbox takes, as Setting up sandboxes describes them:
package = await client.put_package( "workspace", "app:latest", vcpus=2, memory_mib=2048, ports=[8000],)print(package.id)const pkg = await client.putPackage('workspace', 'app:latest', { vcpus: 2, memoryMib: 2048, ports: [8000],})console.log(pkg.id)use virtx::{PackagePutArgs, SandboxCreateArgs};
let sandbox = SandboxCreateArgs::new("app:latest").vcpus(2).memory_mib(2048).port(8000);let package = client.put_package("workspace", &PackagePutArgs::new(sandbox)).await?;println!("{}", package.id());Every sandbox made of workspace is then made of app:latest, with 2 vCPUs, 2048 MiB and port 8000 open in it.
Two options are left to each sandbox, and a package that sets them is refused with 400:
name: each sandbox is named as it is made, if at all.- A port on local: it reaches the machine of whoever makes the sandbox, so they ask for it as they make one.
A package name is letters, digits, ., _ and -, as a sandbox’s.
The server also gives the package an id.
An app names the package by its name, and its project by the project’s id, since its users are in no project.
The image
A tag is read as each sandbox is made, not as the package is kept.
A package of app:latest makes the next sandbox of whatever image app:latest points at then, so pushing a new image under the tag updates every sandbox made from then on.
To make every sandbox of one image, name it by its digest.
While a package names a tag, deleting the tag, or the image it points at, is refused with 409.
Moving the tag to another image is not.
Volumes
Every sandbox made of a package mounts the volumes it names, the same volumes.
Mount a volume for data every sandbox reads, read-only; what one sandbox should keep for itself goes in its own disk, which goes with it.
A package does not hold its volumes: removing one is not refused, and a sandbox of the package is then refused, 404, until the volume is made again or the package names another.
Size
max is how large one the package’s access lets in may resize a sandbox they made of it:
package = await client.put_package( "workspace", "app:latest", vcpus=2, memory_mib=2048, ports=[8000], max={"vcpus": 4, "memory_mib": 8192},)const pkg = await client.putPackage('workspace', 'app:latest', { vcpus: 2, memoryMib: 2048, ports: [8000], max: { vcpus: 4, memoryMib: 8192 },})use virtx::{PackagePutArgs, SandboxCreateArgs, SandboxResizeArgs};
let sandbox = SandboxCreateArgs::new("app:latest").vcpus(2).memory_mib(2048).port(8000);let args = PackagePutArgs::new(sandbox).max(SandboxResizeArgs::default().vcpus(4).memory_mib(8192));let package = client.put_package("workspace", &args).await?;Each value is no less than what the package makes a sandbox with, or the package is refused with 400.
A value left out, or no max at all, holds the sandbox to the size it was made with.
A member of the project resizes it as any sandbox, with no max.
Make a sandbox of it
create_sandbox on the package makes one, and answers it:
sandbox = await package.create_sandbox(name="task-42")await sandbox.exec(["uname", "-a"]).complete(print=True)const sandbox = await pkg.createSandbox({ name: 'task-42' })await sandbox.exec(['uname', '-a']).complete({ print: true })use virtx::{PackageSandboxCreateArgs, SandboxExecArgs};
let sandbox = package.create_sandbox(&PackageSandboxCreateArgs::default().name("task-42")).await?;sandbox.exec(&SandboxExecArgs::new(["uname", "-a"])).complete(true).await?;The sandbox is a sandbox like any other: run commands in it, read its files, forward its ports, and remove it when the work is done. It knows nothing of the package: replacing or removing the package changes no sandbox made of it.
namefinds it again withfind_sandbox. Only a member of the project may name one, since names are the project’s.portsare its ports on local, eachon="local", beside the ports in the sandbox the package gives it. A port in the sandbox is the package’s to give, and is refused with400. See A port on local.
client.create_package_sandbox does the same with the package’s id or name, for a package you have not looked up.
Find, replace and remove a package
list_packagelists the project’s packages, andget_packagefinds one by itsidor its name.put_packagewith a name the project already has replaces that package in whole. Its name andidstay the same, so an app that names it keeps working, and the sandboxes made of it stay as they were.removeremoves the package. The sandboxes made of it stay, each reached by who made it, until they are removed.
for package in await client.list_package(): print(package.id, package.name)
package = await client.get_package("workspace")await package.remove()for (const pkg of await client.listPackage()) { console.log(pkg.id, pkg.name)}
const pkg = await client.getPackage('workspace')await pkg.remove()for package in client.list_package().await? { println!("{} {}", package.id(), package.name());}
let package = client.get_package("workspace").await?;package.remove().await?;For every call and what each returns, see Packages in the SDK reference.
Troubleshooting
-
400,invalid, fromput_packageCause: A package name with another character, a port on local, or a
maxsmaller than the size the package makes a sandbox with.Fix: Give ports on local as each sandbox is made, and a
maxno smaller than the package’s own size. -
404, fromput_packageCause: The project has no image by that tag, or no volume by that name.
Fix: Build, pull or upload the image to that server and project first, and create the volume.
-
404,not_found,no package here is …Cause: No package has that
id, or that name in the client’s project.Fix: Check the client’s project, or name the package by its
id. -
409, deleting an image or a tagCause: A package names it.
Fix: Point the package at another one with
put_package, or remove the package first. -
400,invalid, fromcreate_sandboxon a packageCause: A port in the sandbox given as it is made, or a
namegiven by someone who is not a member of the project.Fix: Give ports in the sandbox in the package, and leave out
name.
Next
- Letting your users in: let people who are not members of the project make a sandbox of a package.
- Building an app on a package: what your app runs to make a sandbox of a package and use it.