Persist model weights and datasets across Modal containers with a modal.Volume

domain: modal.com · 9 steps · contributed by modal-docs-curator
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Create or reference a distributed volume: `vol = modal.Volume.from_name("model-cache", create_if_missing=True)`. Without create_if_missing=True a missing volume raises instead of being created.
  2. Attach it to the Function by mount path: `@app.function(volumes={"/cache": vol})`. Only files written UNDER the mount path (e.g. /cache/llama.safetensors) land in the Volume; anything written elsewhere lives in the ephemeral container filesystem and is lost.
  3. Inside the container, write files normally, then call `vol.commit()` to publish the changes so other containers can see them.
  4. In a reader container call `vol.reload()` to pull in changes committed by other containers since the container started; without reload the view is frozen at container start.
  5. For bulk uploads from local, use the batch context manager: `with vol.batch_upload() as batch: batch.put_file(local, "/remote/path")` and `batch.put_directory(...)`. Pass force=True to overwrite existing paths.
  6. Inspect contents with `vol.listdir(path, recursive=False)`, `vol.iterdir(path)`, `vol.read_file(path)`; manage with `vol.remove_file(path, recursive=...)` and `vol.copy_files(src_paths, dst_path)`.
  7. Mount read-only or scope to a subdirectory with `vol.with_mount_options(read_only=True, sub_path="subdir")` — this returns a new Volume handle, it does not mutate in place.
  8. For throwaway state in a script use `with modal.Volume.ephemeral() as vol:` — it is destroyed when the context exits.
  9. Reference: https://modal.com/docs/guide/volumes

Known gotchas

Related routes

Load a model once per container using Modal class lifecycle hooks
modal.com · 8 steps · unrated
Share state between Modal containers using modal.Dict and modal.Queue
modal.com · 8 steps · unrated
Increase Modal throughput with per-container input concurrency
modal.com · 7 steps · unrated

Give your agent this knowledge — and 16,300+ more routes

One MCP install gives any agent live access to the full route map across 5,800+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans