Volumes
A volume is a persistent disk, backed by object storage, that sandboxes mount at a path. Its files outlive every sandbox that mounts it, and several sandboxes can mount it at once. Daytona mounts volumes only when it creates a sandbox, so a sandbox’s mounts are fixed for its life.
Files outlive every sandbox
Section titled “Files outlive every sandbox”Declare a shared volume
Section titled “Declare a shared volume”import * as Daytona from "@alchemy.run/daytona";
export const Cache = Daytona.Volume("Cache", {});Mount it in a dev box
Section titled “Mount it in a dev box”import * as Daytona from "@alchemy.run/daytona";import { Base } from "./Images.ts";import { Cache } from "./Storage.ts";
export const DevBox = Daytona.Sandbox("DevBox", { snapshot: Base, mounts: [{ volume: Cache, path: "/cache" }],});Mount it in sandboxes you create
Section titled “Mount it in sandboxes you create”const cache = yield* Daytona.MountVolume(Cache, { path: "/cache" });const sandbox = yield* sandboxes.create({ mounts: [cache] });In a Worker, MountVolume returns a mount entry for the sandboxes you create. In a sandbox that runs a program, it mounts the volume into that sandbox.
One volume per user
Section titled “One volume per user”const volumes = yield* Daytona.CreateVolume({ prefix: "user-" });const home = yield* volumes.getOrCreate({ name: `user-${user}` });The Worker’s key can read, create and delete volumes. getOrCreate waits until a new volume is ready. Volumes have only a name, so prefix is how you find the ones the Worker created: the binding refuses names outside it. They are not deleted when the stack is destroyed; volumes.list() and delete the names with your prefix to clean them up.
Delete a volume
Section titled “Delete a volume”yield* volumes.delete(home.volumeId);Deleting a volume deletes its files. A volume can’t be deleted while a sandbox mounts it, and Daytona releases it about a minute after that sandbox is deleted, so delete fails with VolumeInUse until then: retry later rather than holding a request open.
Write new files, don’t append
Section titled “Write new files, don’t append”A volume is backed by object storage, so writing a whole file works but appending to an existing one doesn’t. Write a new file instead of >> onto an old one, like a file per visit or per run.
When you destroy the stack, a declared Daytona.Volume waits for the sandboxes that mount it to go first.