Skip to main content
Cloud Run runs your container on instances that come and go as traffic changes, and anything an instance writes to its own filesystem is gone when it shuts down. Mount an Archil disk in the container and those files stay on the disk, where every other instance, the next revision, and the Archil SDK can read them. Cloud Run’s second-generation execution environment supports FUSE, so the disk mounts as a regular directory with no extra permissions. Mount it when the container starts and unmount it when Cloud Run sends SIGTERM.

Prerequisites

  • The gcloud CLI, signed in to a project with billing enabled.
  • An Archil disk, ideally in the Archil region closest to your Cloud Run region, such as gcp-us-central1 for us-central1. See Region availability.
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, along with your Google Cloud project and region:

Set up the project

Enable the APIs the steps below use. Cloud Run builds and runs the service as the Compute Engine default service account unless you choose another, so give that account permission to build the image, and store the mount token in Secret Manager where only that account can read it, rather than in the service’s environment variables:

Build the service

The image needs the archil client and your app:
Dockerfile
server.py mounts the disk before it starts serving, appends a line to a file on the disk for every request, and unmounts the disk when Cloud Run sends SIGTERM. It mounts with --conditional because more than one instance can have the disk mounted at once: Cloud Run scales out under load, and a new revision starts before the old one stops.
server.py
Deploy from the directory that holds both files. Cloud Build builds the image, and --execution-environment gen2 selects the environment that supports FUSE:

Use the disk

Request the service with your own identity token:
Deploy a new revision, for example after a code change, and request the service again. The new revision mounts the same disk and finds the line the first one wrote:

How the disk behaves on Cloud Run

Mount with --conditional. Every instance of every revision can then read and write anywhere on the disk. For faster writes to a directory only one instance uses, check it out with archil checkout; see Sharing disks. Unmount on SIGTERM. Unmounting sends the client’s buffered writes to the disk. Cloud Run sends SIGTERM before it shuts an instance down, and waits 10 seconds before stopping it.

Clean up

Delete the service, the secret, the service’s image, and the disk once you no longer need them:

Next steps