🚀 A simple example demonstrating how to run WebAssembly modules in Docker (Beta)
- Docker Desktop (Latest version)
- Rust (for building the WASM module)
- Git
docker-wasm-example/
├── src/
│ └── main.rs
├── Cargo.toml
├── .dockerignore
├── Dockerfile
└── README.md
- Open Docker Desktop
- Navigate to Settings (gear icon)
- In the General tab, ensure "Use containerd for pulling and storing images" is enabled
- Go to Features in development
- Check "Enable Wasm"
- Click Apply & restart
- In the confirmation dialog, click Install to install the WASM runtimes
- Wait for Docker Desktop to restart
Create a new Rust project:
cargo new docker-wasm-example
cd docker-wasm-exampleCreate Cargo.toml:
[package]
name = "wasm-example"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0"
[lib]
crate-type = ["cdylib"]Create src/main.rs:
use std::prelude::v1::*;
fn main() -> anyhow::Result<()> {
println!("Hello from WASM!");
Ok(())
}Create .dockerignore:
target/
Cargo.lock
Create Dockerfile:
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM rust:1.70-slim as builder
WORKDIR /app
# Install required dependencies
RUN apt-get update && apt-get install -y \
cmake \
&& rm -rf /var/lib/apt/lists/*
# Install WASI target
RUN rustup target add wasm32-wasi
# Copy project files
COPY . .
# Build for WASI target
RUN cargo build --target wasm32-wasi --release
FROM scratch
COPY --from=builder /app/target/wasm32-wasi/release/wasm-example.wasm /app.wasm
ENTRYPOINT [ "/app.wasm" ]- Create and use a new builder for WASM:
docker buildx rm wasm-builder --force
docker buildx create --name wasm-builder --driver docker-container --bootstrap
docker buildx use wasm-builder- Build the WASM container:
docker buildx build \
--platform wasi/wasm32 \
--build-arg BUILDPLATFORM=$(docker version -f '{{.Server.Os}}/{{.Server.Arch}}') \
--load \
-t wasm-example .- Run the container with different runtimes:
WasmEdge (Recommended for general use)
docker run --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm32 wasm-exampleWasmer
docker run --runtime=io.containerd.wasmer.v1 --platform=wasi/wasm32 wasm-exampleWasmtime
docker run --runtime=io.containerd.wasmtime.v1 --platform=wasi/wasm32 wasm-exampleSlight
docker run --runtime=io.containerd.slight.v1 --platform=wasi/wasm32 wasm-exampleSpin
docker run --runtime=io.containerd.spin.v2 --platform=wasi/wasm32 wasm-exampleLunatic
docker run --runtime=io.containerd.lunatic.v1 --platform=wasi/wasm32 wasm-exampleWWS (Wasm Workers Server)
docker run --runtime=io.containerd.wws.v1 --platform=wasi/wasm32 wasm-example| Runtime | Best For | Key Features |
|---------|----------|--------------|
| WasmEdge | General purpose | Good performance, broad compatibility |
| Wasmer | Production | Strong ecosystem, good performance |
| Wasmtime | Development | Fast startup, good debugging |
| Slight | Lightweight apps | Minimal resource usage |
| Spin | Web services | HTTP-focused features |
| Lunatic | Distributed systems | Actor-based concurrency |
| WWS | Worker systems | Background processing |
- Builder exists error
docker buildx rm wasm-builder --force
docker buildx create --name wasm-builder --driver docker-container --bootstrap- Runtime not found
docker info | grep wasm- Build fails
# Clean Docker build cache
docker builder prune
# Remove and recreate builder
docker buildx rm wasm-builder
docker buildx create --name wasm-builder --driver docker-container --bootstrap- WASI dependencies error
rustup target add wasm32-wasi --force
cargo cleanFeel free to submit issues and enhancement requests!
MIT
🔧 Built with ❤️ using Docker WASM support
Key changes:
- Updated Cargo.toml with proper dependencies and crate type
- Added proper error handling in main.rs
- Simplified Dockerfile build process
- Added comprehensive runtime comparison table
- Added examples for all available runtimes
- Updated troubleshooting section with WASI-specific issues
Citations: [1] https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/7954569/b3c29962-c5b2-4755-b9a7-f6ac868bd5a7/paste.txt

