Skip to main content
Learn how to compile a simple C program to WebAssembly (WASM) using a WASI-compatible compiler and then package it into a Docker image with a minimal runtime.
Docker’s WASM integration is currently a beta feature available in a special technical-preview build of Docker Desktop. Refer to Docker’s experimental WASM support when testing locally.

Prerequisites

ToolVersionPurpose
Clang (WASI SDK)v16+Compile C to WASM via WASI
Docker DesktopLatest (technical preview)Build and run WASM containers
WasmEdgev0.14+Execute the WASM binary inside Docker

1. Write a simple C program

Create a file named helloworld.c:
#include <stdio.h>

int main() {
    printf("hello, world\n");
    return 0;
}

2. Compile to a WASM binary

Use the WASI SDK’s Clang to target wasm32-wasi:
clang --target=wasm32-wasi -O3 helloworld.c -o helloworld.wasm
This produces a standalone helloworld.wasm in your working directory.

3. Create the Dockerfile

We’ll build FROM scratch so the final image only contains the WASM binary:
FROM scratch
COPY helloworld.wasm /helloworld.wasm
ENTRYPOINT [ "/helloworld.wasm" ]
Save this as Dockerfile alongside helloworld.wasm.

4. Build the Docker image

Run:
cd path/to/your/project
docker build --platform=wasm/wasm32 -t helloworld-wasm .
The --platform=wasm/wasm32 flag instructs Docker to treat this as a WASM container.

5. Run the WASM container

Execute with the WasmEdge runtime support in containerd:
docker run --rm \
  --name wasm-docker-c-program \
  --runtime=io.containerd.wasmedge.v1 \
  helloworld-wasm
You should see:
hello, world
Congratulations! You’ve compiled a C program to WebAssembly and run it inside a Docker container.