DEV Community

Michael Levan
Michael Levan

Posted on

WASM and Docker: Quickstart

There’s been a lot of buzz around the idea that Wasm is going to take over Kubernetes, Serverless, and a few other myths. None of this is true. Wasm is a runtime, which means it needs a place to run. That could be anywhere from a local computer to Kubernetes to a Docker Engine and everywhere in-between.

In this quickstart, you’ll learn how to take a Wasm-based Go application (Go app code included in this post) and run it in Docker.

App

First, you’ll need an app to containerize. The majority of examples out there right now are Rust and JavaScript. To switch things up a bit, let’s see how to do it in Go.

The code below is a quick “hello world” style app. Save it in a file called main.go.

package main

import "fmt"

func main() {
    wasmTest()
}

func wasmTest() {
    fmt.Println("hello from the WASM beyond!")
}
Enter fullscreen mode Exit fullscreen mode

Within the same directory as the main.go file, run the following command which will export a Wasm binary.

GOOS=wasip1 GOARCH=wasm go build -o main.wasm main.go
Enter fullscreen mode Exit fullscreen mode

Run the Wasm binary to confirm it works.

wasmtime main.wasm
Enter fullscreen mode Exit fullscreen mode

If you take a look in the directory, you should see the binary like in the screenshot below.

Image description

Dockerfile

Now that the Wasm binary exists, you can create a container image out of it. The below Dockerfile will create a container image that uses the Wasm binary you just built.

FROM scratch

COPY main.wasm .

CMD [ "/main.wasm" ]
Enter fullscreen mode Exit fullscreen mode

Build The Container Image

With the Dockerfile in the previous section, build the container image.

The difference you’ll see between this container image and other container images is that you’re specifying Wasm as the build platform.

docker buildx build --platform wasi/wasm -t gowasm .

Enter fullscreen mode Exit fullscreen mode

Run The Container Image

Run the container image with the appropriate Wasm runtime (built into Docker) and the WASI/WASM interface.

docker run --rm --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm gowasm:latest
Enter fullscreen mode Exit fullscreen mode

You should see an output similar to the screenshot below.

Image description

Top comments (0)