DEV Community

Michael Levan
Michael Levan

Posted on

3

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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay