Skip to main content
A Cloudflare Container runs your image in a VM that belongs to a Durable Object, which starts it, runs commands in it, and stops it after a period of inactivity. When the container stops, its disk is thrown away. Mount an Archil disk in the container and anything written there stays on the disk, where the next container can mount it and your app can read it with the Archil SDK. Cloudflare Containers support FUSE, so the disk mounts as a regular directory. The Durable Object mounts the disk when it starts the container, and the container unmounts it when Cloudflare stops it. The same approach works with the Sandbox SDK, which runs on the same Container API.

Prerequisites

  • A Cloudflare account on the Workers Paid plan, which Containers require.
  • Docker running locally. Wrangler uses it to build the image.
  • An Archil disk in the Archil region of your choice.
Set your Archil API key and region:
Create the disk with the disk CLI. The output includes the disk ID and a one-time mount token, which grants access to this disk only:
Export both for the steps below:

Create the project

Create a directory for the project and install Wrangler:
The project needs four files: the image, a script the container runs, the Worker, and Wrangler’s configuration. The image installs the archil client. Cloudflare runs linux/amd64 images, and the installer picks the matching build:
Dockerfile
The container’s main process waits for Cloudflare’s SIGTERM and unmounts the disk before it exits, which sends every buffered write to the disk and releases it:
start.sh
The Worker routes each request to a Durable Object by name. The Durable Object starts its container, mounts the disk with the token from a Worker secret, and then runs a command in the mounted directory. The command appends a line to a file on the disk and prints the file:
src/index.ts
wrangler.jsonc binds the Durable Object to the image and sets the disk ID and region:
wrangler.jsonc
Replace the two values under vars with your disk ID and region.

Deploy and try it

Log in, deploy, and store the mount token as a Worker secret. The first deploy builds the image and pushes it to Cloudflare’s registry:
The deploy prints the Worker’s URL. Request a workspace by name:
The first request to a workspace starts its container and mounts the disk, so it takes a few seconds. Stop the container. The Durable Object sends it SIGTERM, start.sh unmounts the disk, and the request returns once the container has exited:
Request a different workspace. Its container mounts the same disk and finds the line the first one wrote:

How the disk behaves in a container

One container holds the disk at a time. A plain mount is exclusive. To mount the same disk in several containers at once, add --conditional to the archil mount command. Every container can then read and write anywhere on the disk; see Sharing disks. To give each user their own storage instead, create a disk per user and pass its ID and token to that user’s Durable Object. Unmount when the container stops. Cloudflare sends a container SIGTERM when its inactivity timeout expires, and stop() sends it with signal(). Unmounting in the SIGTERM handler sends the client’s buffered writes to the disk and releases it for the next container. Stop containers with signal() rather than destroy(), which doesn’t give the container time to unmount. If a container ever goes away with the disk still mounted, see Release a stale lock.

Clean up

Delete the Worker, its container application and image, and the disk once you no longer need them. wrangler delete leaves the container application and its image behind, so delete those by ID and tag:

Next steps