1. WebAssembly:没有容器的 Docker
2022-12, Docker 宣布与WasmEdge合作支持WebAssembly。 Docker 成为加入字节码联盟中的一员!
2. 什么是WebAssembly?
WebAssembly(或 Wasm)是一种定义二进制指令格式的开放标准,它允许从不同的源语言创建可移植的二进制可执行文件。
它起源于网络,并得到所有主要浏览器的支持。
2.1 Wasm 如何在浏览器中工作?

浏览器引擎集成了一个 Wasm runtime,通常称为 Wasm 运行时,可以运行 Wasm 二进制指令。有编译器工具链(如 Emscripten)可以将源代码编译为 Wasm 目标。这允许将遗留应用程序移植到浏览器并直接与在客户端 Web 应用程序中运行的 JS 代码通信。
Wasm 优势:
- 标准:
WASI和WebAssembly应用程序的标准化 - 快速: 相比容器,直接字节加载,没有
冷启动过程 - 可移植: 一次编译,支持大多数
CPU(x86、ARM、RISC-V)和大多数操作系统. 包括 Linux、Windows、macOS、Android、ESXi,甚至是非 Posix 操作系统 - 高效: Wasm 应用程序 占用
内存和 CPU运行 - 安全: Wasm 应用程序只能
访问明确允许的内容
Wasm 容器构建示例

基于go-app中golang中的syscall/js 实现现有的 wasm 编译;
package main
import (
"net/http"
"os"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type hello struct {
app.Compo
name string
}
func (h *hello) Render() app.UI {
return app.Div().Body(
app.H1().Body(
app.Text("Hello, "),
app.If(h.name != "",
app.Text(h.name),
).Else(
app.Text("World!"),
),
),
app.P().Body(
app.Input().
Type("text").
Value(h.name).
Placeholder("What is your name?").
AutoFocus(true).
OnChange(h.ValueTo(&h.name)),
),
)
}
func main() {
app.Route("/", &hello{})
app.Route("/hello", &hello{})
app.RunWhenOnBrowser()
h := &app.Handler{
Title: "Hello Demo from Docker",
Author: "dongjiang <dongjiang1989@126.com>",
Version: os.Getenv("APP_VERSION"),
}
if err := http.ListenAndServe(":45123", h); err != nil {
panic(err)
}
}
编译:
dongjiang@MacBook Pro:wasm $ GOOS=js GOARCH=wasm go build -o web/app.wasm ./app/
dongjiang@MacBook Pro:wasm $ ls -l
total 27288
-rw-r--r-- 1 dongjiang staff 486 4 25 19:38 Dockerfile
drwxr-xr-x 3 dongjiang staff 96 4 25 19:21 app
-rwxr-xr-x 1 dongjiang staff 13955945 4 25 19:54 app.wasm
-rw-r--r--@ 1 dongjiang staff 166 4 25 19:11 go.mod
-rw-r--r-- 1 dongjiang staff 708 4 25 19:11 go.sum
drwxr-xr-x 4 dongjiang staff 128 4 25 19:33 vendor
drwxr-xr-x 3 dongjiang staff 96 4 25 19:55 web
dongjiang@MacBook Pro:wasm $ go build -o server ./app/
dongjiang@MacBook Pro:wasm $ ls -l
total 47616
-rw-r--r-- 1 dongjiang staff 486 4 25 19:38 Dockerfile
drwxr-xr-x 3 dongjiang staff 96 4 25 19:21 app
-rwxr-xr-x 1 dongjiang staff 13955945 4 25 19:54 app.wasm
-rw-r--r--@ 1 dongjiang staff 166 4 25 19:11 go.mod
-rw-r--r-- 1 dongjiang staff 708 4 25 19:11 go.sum
-rwxr-xr-x 1 dongjiang staff 10407714 4 25 19:56 server
drwxr-xr-x 4 dongjiang staff 128 4 25 19:33 vendor
drwxr-xr-x 3 dongjiang staff 96 4 25 19:55 web
dongjiang@MacBook Pro:wasm $ ./server
结果:

工程
代码工程:https://github.com/kubeservice-stack/muti-arch/tree/master/wasm
dockerhub: https://hub.docker.com/repository/docker/dongjiang1989/muti-arch-demo-server/general
「如果这篇文章对你有用,请随意打赏」
如果这篇文章对你有用,请随意打赏
使用微信扫描二维码完成支付