Skip to main content

Docker BuildKit on Morph Cloud

tl;dr Launch optimized Docker environments with BuildKit enabled on Morph Cloud. Create, branch, and restore Docker build environments within seconds using Morph Cloud's Infinibranch technology.

Prerequisites

Install the Morph Cloud SDK

To get started, install the Morph Python SDK:

pip install morphcloud

Set your API key

export MORPH_API_KEY='your-key-here'

You can generate an API key in the Morph Cloud console.

Using the Morph Cloud CLI

The SDK includes a CLI that lets you manage resources from the command line:

# List instances
morphcloud instance list

# Create a snapshot
morphcloud snapshot create --instance-id instance_123

# For more commands and options
morphcloud --help

Quick Start

# Install dependencies
curl -LsSf https://astral.sh/uv/install.sh | sh
source ~/.local/bin/env
uv venv && source .venv/bin/activate
uv pip install morphcloud

# Set your API key
export MORPH_API_KEY='your-key-here'

# Run the setup script
uv run python docker-buildkit_setup.py

The setup script will:

  1. Create or reuse a base MorphVM snapshot
  2. Launch a new instance
  3. Install and configure Docker with BuildKit enabled
  4. Create a demo application with health check and web server
  5. Build a multi-stage Dockerfile using BuildKit's parallel execution
  6. Expose the services via HTTPS

Once the initial setup is complete, launching new Docker BuildKit instances takes just seconds, enabling:

  • Instant environment duplication for parallel build testing
  • Perfect state preservation via Morph Cloud snapshots
  • Isolated Docker environments for each build task

What You Get

Your Docker BuildKit environment provides:

  • Fully configured Docker with BuildKit optimization
  • Multi-stage build demonstration
  • Parallel execution of build stages
  • Health check API on port 8080
  • Web server on port 8081
  • Optimized container image

Beyond Traditional Docker

Compare the experience:

Traditional DockerMorph Cloud Docker BuildKit
Manual BuildKit setupOne-command deployment
Single build environmentMultiple parallel environments
Minute-long VM startupSubsequent startups in seconds
Resource-heavy copiesNear-zero overhead branching

Real-World Applications

A/B Testing Deployment Configurations

# After creating your base snapshot, test multiple configurations simultaneously
client = MorphCloudClient()
snapshot_id = "your-buildkit-snapshot-id"

# Launch three different Docker build environments with different settings
config_a = client.instances.start(snapshot_id)
config_a.exec("sudo echo '{\"features\":{\"buildkit\":true,\"containerd-snapshotter\":true}}' > /etc/docker/daemon.json")
config_a.exec("sudo systemctl restart docker")

config_b = client.instances.start(snapshot_id)
config_b.exec("sudo echo '{\"features\":{\"buildkit\":true},\"max-concurrent-downloads\":10}' > /etc/docker/daemon.json")
config_b.exec("sudo systemctl restart docker")

config_c = client.instances.start(snapshot_id)
config_c.exec("sudo echo '{\"features\":{\"buildkit\":true},\"max-concurrent-uploads\":10}' > /etc/docker/daemon.json")
config_c.exec("sudo systemctl restart docker")

# Now run identical builds across all three and compare performance
for instance, name in [(config_a, "snapshotter"), (config_b, "downloads"), (config_c, "uploads")]:
print(f"Testing {name} configuration at {instance.get_service_url('health-check')}")
instance.exec("time sudo docker build -t test:latest .")

Parallel Matrix Testing

# Test the same Docker build across multiple base images simultaneously
client = MorphCloudClient()
snapshot_id = "your-buildkit-snapshot-id"

# Create a matrix of build tests
base_images = ["alpine:3.18", "alpine:3.19", "ubuntu:22.04", "debian:bookworm-slim", "python:3.12-slim"]
test_instances = []

for image in base_images:
instance = client.instances.start(snapshot_id)
test_instances.append((image, instance))
# Modify the base image in the Dockerfile
instance.exec(f"sed -i 's|FROM ubuntu:22.04 AS base|FROM {image} AS base|' Dockerfile")
# Trigger the build in the background
instance.exec("nohup sudo docker build -t test:latest . > build.log 2>&1 &")
print(f"Building with {image} at {instance.get_service_url('health-check')}")

# Wait and collect results
import time
time.sleep(30)
for image, instance in test_instances:
print(f"Results for {image}:")
instance.exec("cat build.log | grep 'real\|successfully built'")

Zero-Downtime CI/CD Pipeline

# Create a production-like environment, then spawn testing branches with zero overhead
client = MorphCloudClient()
snapshot_id = "your-buildkit-snapshot-id"

# Start production-like environment
prod = client.instances.start(snapshot_id)
prod.exec("sudo docker build -t app:prod .")
prod.exec("sudo docker run -d -p 8080:8080 app:prod")
print(f"Production URL: {prod.get_service_url('health-check')}")

# For each new commit, branch the environment and test changes without affecting production
for commit_id in ["abc123", "def456", "ghi789"]:
# Branch the environment in milliseconds
test_env = client.instances.start(snapshot_id)
# Apply the new commit changes
test_env.exec(f"git checkout {commit_id}")
test_env.exec("sudo docker build -t app:test .")
test_env.exec("sudo docker run -d -p 8080:8080 app:test")
print(f"Testing commit {commit_id} at {test_env.get_service_url('health-check')}")

# Run integration tests against this isolated environment
# If tests pass, update the production image

Technical Details

The Docker BuildKit environment includes:

  • Docker with BuildKit enabled
  • Parallel multi-stage builds
  • Layer caching optimization
  • Health check API service
  • Demo web server
  • Systemd service management

Getting Started

  1. Run the setup script to create your template:
uv run python docker-buildkit_setup.py
  1. Note the snapshot ID from the script output

  2. Launch new instances instantly:

from morphcloud.api import MorphCloudClient

client = MorphCloudClient()
instance = client.instances.start("your-buildkit-snapshot-id")
print(f"Health Check URL: {instance.get_service_url('health-check')}")
print(f"Web Server URL: {instance.get_service_url('web-server')}")

Each new instance launches in seconds, giving you a fresh Docker BuildKit environment configured exactly like your template.

Next Steps

  1. Sign up for early access
  2. Run the setup script to create your first environment
  3. Start launching instant Docker build environments

Ready to transform your Docker build workflow? Get early access today.

Resources