Snapshot and restore Container filesystem
Containers now support snapshot APIs in public beta for saving and restoring point-in-time filesystem state. Create a snapshot first, then pass it back to start() to restore files after container sleep, restart, or handoff to another Durable Object.
Use snapshotContainer() through the Durable Object Container API to capture the full container filesystem. Create a snapshot from a running Container, store its handle, and pass that handle to start() when you restore it later:
import { DurableObject } from "cloudflare:workers";
export class MyDurableObject extends DurableObject {
async saveSnapshot() {
// Create a snapshot from the running Container.
const containerSnapshot = await this.ctx.container.snapshotContainer({});
await this.ctx.storage.put("containerSnapshot", containerSnapshot);
}
async restoreSnapshot() {
// Restore the saved snapshot later.
const containerSnapshot = await this.ctx.storage.get("containerSnapshot");
if (!containerSnapshot) {
return;
}
this.ctx.container.start({ containerSnapshot, enableInternet: false });
}
}import { DurableObject } from "cloudflare:workers";
export class MyDurableObject extends DurableObject {
async saveSnapshot() {
// Create a snapshot from the running Container.
const containerSnapshot = await this.ctx.container.snapshotContainer({});
await this.ctx.storage.put("containerSnapshot", containerSnapshot);
}
async restoreSnapshot() {
// Restore the saved snapshot later.
const containerSnapshot =
await this.ctx.storage.get<ContainerSnapshot>("containerSnapshot");
if (!containerSnapshot) {
return;
}
this.ctx.container.start({ containerSnapshot, enableInternet: false });
}
}Snapshots are only supported for Container applications that use the durable_object scheduling policy. Snapshots are immutable, so create a new snapshot to persist filesystem changes made after a restore.
For more information, refer to Snapshots and the Durable Object Container API.