Join our Discord Server
Docker and Wasm – Better Together

Rust, Wasm and Docker Desktop

Estimated reading: 3 minutes 82 views

WebAssembly, also known as Wasm, is a low-level binary instruction format designed for web browsers that enables the execution of code at near-native speeds in a safe, sandboxed environment. Wasm was created to address the limitations of JavaScript as the only language that can be executed in web browsers. It allows web developers to use a wide range of programming languages, including C++, Rust, and Go, to write code that can run in the browser with near-native performance.

Wasm code is executed in a virtual machine within the browser and is designed to be platform-independent. This means that the same Wasm binary can be executed on any device or platform that supports WebAssembly, including desktops, smartphones, and even IoT devices. Wasm can also interact with JavaScript, allowing developers to easily integrate Wasm code into existing web applications.

Wasm is being adopted by major web browser vendors, including Google, Mozilla, Microsoft, and Apple, and is rapidly gaining popularity as a powerful tool for web developers. It is being used for a wide range of applications, from games and simulations to machine learning and blockchain.

 

Docker Desktop Support Wasm

The Docker+Wasm feature is currently in Beta. We recommend that you do not use this feature in production environments.

Switch-On Docker+Wasm integration

To use the Docker+Wasm integration, you must turn on the containerd image store feature. The Docker+Wasm integration requires the containerd image store feature. 

Verify if you have the latest version of Docker Desktop installed on your Mac system.

 docker version
Client:
 Cloud integration: v1.0.29
 Version:           20.10.22
 API version:       1.41
 Go version:        go1.18.9
 Git commit:        3a2c30b
 Built:             Thu Dec 15 22:28:41 2022
 OS/Arch:           darwin/arm64
 Context:           desktop-linux
 Experimental:      true

Server: Docker Desktop 4.16.2 (95914)
 Engine:
  Version:          22.06.0-beta.0-917-gf888bd4531.m
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.18.4
  Git commit:       f888bd4531
  Built:            Wed Jan 18 09:46:59 2023
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.14
  GitCommit:        9ba4b250366a5ddde94bb7c9d1def331423aa323
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Installing Rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"

 Adding a target for Rust can compile it into a Wasm binary

$ rustup target add wasm32-wasi

info: downloading component 'rust-std' for 'wasm32-wasi'
info: installing component 'rust-std' for 'wasm32-wasi'

Creating a Rust Project using Cargo

cargo new wasm-container 
     Created binary (application) `wasm-container` package

The project structure look something like this:

tree
.
└── wasm-container
    ├── Cargo.toml
    └── src
        └── main.rs

2 directories, 2 files

Change directory to Wasm

If the build succeeds, a wasm file will be created under the release directory.

cargo build --target wasm32-wasi --release
   Compiling wasm-container v0.1.0 (/Users/ajeetraina/feb/wasm/wasm-container)
    Finished release [optimized] target(s) in 1.85s
ajeetraina@Docker-Ajeet-Singh-Rainas-MacBook-Pro wasm-container % 

The project structure look something like this:

wasm-container % tree
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    ├── release
    │   ├── build
    │   ├── deps
    │   ├── examples
    │   └── incremental
    └── wasm32-wasi
        ├── CACHEDIR.TAG
        └── release
            ├── build
            ├── deps
            │   ├── wasm_container-f66b7ddf90b70b18.d
            │   └── wasm_container-f66b7ddf90b70b18.wasm
            ├── examples
            ├── incremental
            ├── wasm-container.d
            └── wasm-container.wasm

13 directories, 9 files

Creating a Dockerfile

Create a New Dockerfile under wasm-container directory

cat Dockerfile 
FROM scratch
COPY ./target/wasm32-wasi/release/wasm-container.wasm /wasm-container.wasm
ENTRYPOINT [ "wasm-container.wasm" ]
ajeetraina@Docker-Ajeet-Singh-Rainas-MacBook-Pro wasm-container % 

Using Docker Buildx

% docker buildx build --platform wasi/wasm32 -t ajeetraina/wasm-container:1.0 .

Verifying the Docker Image

$ docker images

ajeetraina/wasm-container                                            1.0                                                                                       796bd53ad35d   19 seconds ago   514kB

Running the Docker Container

docker run --platform wasi/wasm32 --runtime=io.containerd.wasmedge.v1  ajeetraina/hello-wasm-container:1.0
Hello, world!
Share this Doc

Rust, Wasm and Docker Desktop

Or copy link

CONTENTS
Join our Discord Server