Layered Snapshot Builds with Infinibranch
Snapshot.build(...) from the morphcloud.api module lets you treat Morph Cloud snapshots like Docker layers. You describe a sequence of effects—shell commands or Python callables—and Morph Cloud snapshots the machine after each step. The SDK automatically reuses the longest cached prefix (via Infinibranch snapshots), which means subsequent runs skip straight to the first new step.
This guide walks through two caching-heavy workflows:
- Pulling a remote version number at runtime to decide whether a cache should be reused or expanded.
- Spawning multiple branch-specific snapshots that share a long common prefix so you can roll back experiments instantly.
Prerequisites
- Install the SDK:
pip install morphcloud - Set the
MORPH_API_KEYenvironment variable. - Have a "builder" snapshot ID you want to extend—typically a base OS image or an application bootstrap snapshot.
Understand Snapshot.build
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
base = client.snapshots.get("snapshot_base_id")
recipe = [
"apt-get update && apt-get install -y curl jq", # shell command step
lambda inst: inst.exec(["bash", "-lc", "mkdir -p /opt/app"]), # python callable step
]
final_snapshot = base.build(recipe)
print(final_snapshot.id)
Each entry in recipe becomes a layer:
- Strings run as foreground shell commands (
instance.execbehind the scenes). - Callables receive the running
Instanceobject so you can use Python for richer logic (file uploads, API calls, conditional behavior).
When you rerun base.build(recipe) the SDK checks, step by step, for existing snapshots with the same digest. Matching layers are skipped and the cached snapshot at the end of the prefix becomes the new starting point.
Treat your step list like a Dockerfile. Put the most stable layers first, and reserve the last steps for volatile configuration so the expensive work stays cached.
Use case A — Cache Busting with Remote Version Numbers
Imagine a CLI bundle published at https://releases.example.com/mycli/latest.json. The JSON looks like { "version": "1.24.0", "tarball": "https://.../mycli-1.24.0.tar.gz" }. You want a snapshot that always contains the newest CLI, but only rebuilds when the version changes.
import os
import requests
from morphcloud.api import MorphCloudClient
client = MorphCloudClient(api_key=os.environ["MORPH_API_KEY"])
builder = client.snapshots.get("snapshot_builder_id")
release = requests.get(
"https://releases.example.com/mycli/latest.json", timeout=5
).json()
version = release["version"]
tarball_url = release["tarball"]
steps = [
"apt-get update && apt-get install -y curl tar", # stays cached for months
lambda inst: inst.exec("mkdir -p /opt/mycli"),
# The command embeds the resolved version: if the version changes, so does the digest.
f"curl -fsSL {tarball_url} -o /opt/mycli/mycli-{version}.tar.gz",
f"tar -xzf /opt/mycli/mycli-{version}.tar.gz -C /opt/mycli",
f"ln -sfn /opt/mycli/mycli-{version} /usr/local/bin/mycli",
]
snapshot = builder.build(steps)
print(f"Snapshot {snapshot.id} now carries mycli {version}")