Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

container2wasm

> DevOps
Open source

Container to WASM converter

2.7K stars0 likes2 views
WebsiteGitHub

About

Container to WASM converter

[⬇️Download] [ Command reference] [ Additional Documents] [▶️Demo]

container2wasm: Container to WASM converter

container2wasm is a container-to-wasm image converter that enables to run the container on WASM.

  • Converts a container to WASM with emulation by Bochs (for x86_64 containers), TinyEMU (for riscv64 containers) and QEMU.
  • Runs on WASI runtimes (e.g. wasmtime, wamr, wasmer, wasmedge, wazero)
  • Runs on browser
  • x86_64, riscv64 or AArch64 containers are recommended.

This is an experimental software.

Demo page of containers on browser (debian,python,node,vim): https://ktock.github.io/container2wasm-demo/

Examples

Container Image to WASM (WASI)

bash
$ c2w ubuntu:22.04 out.wasm

The above command converts ubuntu:22.04 container image to WASI image (out.wasm).

NOTE1: For selecting the container image's architecture other than amd64, use --target-arch flag of c2w (e.g. c2w --target-arch=riscv64 riscv64/ubuntu:22.04 out.wasm).

NOTE2: x86_64 or riscv64 container is recommended. Other platform's containers should work but slow because of additional emulation.

The generated image runs on WASI runtimes:

bash
$ wasmtime out.wasm uname -a
Linux localhost 6.1.0 #1 PREEMPT_DYNAMIC Mon Jun  5 11:57:09 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ wasmtime out.wasm ls /
bin   dev  home  lib32	libx32	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 lib64	media	opt  root  sbin  sys  usr

Directory mapped from the host is accessible on the container.

$ mkdir -p /tmp/share/ && echo hi > /tmp/share/from-host
$ wasmtime --mapdir /mnt/share::/tmp/share out.wasm cat /mnt/share/from-host
hi

Please refer to ./examples/networking/wasi/ for enabling networking

Container on Browser

You can run the container on browser as well. There are two methods for running the container on browser.

WASI on browser

This example converts the container to WASI and runs it on browser.

Please also refer to ./examples/wasi-browser for details.

The following command generates a WASI image.

$ c2w ubuntu:22.04 /tmp/out-js2/htdocs/out.wasm

The following is an example of running the image on browser relying on xterm-pty and browser_wasi_shim. This example serves the image on localhost:8080 using apache http server.

bash
$ cp -R ./examples/wasi-browser/* /tmp/out-js2/ && chmod 755 /tmp/out-js2/htdocs
$ docker run --rm -p 8080:80 \
         -v "/tmp/out-js2/htdocs:/usr/local/apache2/htdocs/:ro" \
         -v "/tmp/out-js2/xterm-pty.conf:/usr/local/apache2/conf/extra/xterm-pty.conf:ro" \
         --entrypoint=/bin/sh httpd -c 'echo "Include conf/extra/xterm-pty.conf" >> /usr/local/apache2/conf/httpd.conf && httpd-foreground'

You can run the container on browser via localhost:8080.

WASI on browser with networking

Container can also perform networking. This section is the demo of using curl command in the container.

Tested only on Chrome. The example might not work on other browsers.

$ cat <<EOF | docker build -t debian-curl -
FROM debian:sid-slim
RUN apt-get update && apt-get install -y curl
EOF
$ c2w debian-curl /tmp/out-js2/htdocs/out.wasm

This example serves the image on localhost:8080 using apache http server. The following also puts the network stack runnable on browser to the document root.

bash
$ cp -R ./examples/wasi-browser/* /tmp/out-js2/ && chmod 755 /tmp/out-js2/htdocs
$ wget -O /tmp/out-js2/htdocs/c2w-net-proxy.wasm https://github.com/container2wasm/container2wasm/releases/download/v0.5.0/c2w-net-proxy.wasm
$ docker run --rm -p 8080:80 \
         -v "/tmp/out-js2/htdocs:/usr/local/apache2/htdocs/:ro" \
         -v "/tmp/out-js2/xterm-pty.conf:/usr/local/apache2/conf/extra/xterm-pty.conf:ro" \
         --entrypoint=/bin/sh httpd -c 'echo "Include conf/extra/xterm-pty.conf" >> /usr/local/apache2/conf/httpd.conf && httpd-foreground'

You can run the container on browser with several types of configurations:

  • localhost:8080/?net=browser: Container with networking. Network stack c2w-net-proxy implemented based on gvisor-tap-vsock runs on browser and forwards HTTP/HTTPS packets using the browser's Fetch API. The set of accesible sites is restricted by the browser configuration (e.g. CORS restriction). See also ./examples/networking/fetch for detalis.
  • localhost:8080/?net=delegate=ws://localhost:8888: Container with networking. You need to run user-space network stack c2w-net implemented based on gvisor-tap-vsock on the host (outside of browser). It forwards all packets received from the browser over WebSocket. See also ./examples/networking/websocket for detalis and configuration. (tested only on Linux)
  • localhost:8080: Container without networking.

Please also refer to ./examples/networking/ for details about networking.

emscripten on browser

This example uses emscripten for converting the container to WASM. This leverages QEMU Wasm which is QEMU ported to browser with JIT compilation (TCG) enabled. Also refer to ./examples/emscripten/ for detalis.

The following command generates a WASM image and a JS file runnable on browser.

bash
$ c2w --to-js alpine:3.20 /tmp/out-js/htdocs/

The following is an example of running the image on browser relying on xterm-pty. This example serves the image on localhost:8080 using apache http server.

Or, you can also try a smaller example ./emscripten-simple without webpack dependencies.

…

You can run the container on browser with several types of configurations:

  • localhost:8080/?net=browser: Container with networking. Network stack c2w-net-proxy implemented based on gvisor-tap-vsock runs on browser and forwards HTTP/HTTPS packets using the browser's Fetch API. The set of accesible sites is restricted by the browser configuration (e.g. CORS restriction). See also ./examples/emscripten/ for detalis.
  • localhost:8080/?net=delegate=ws://localhost:8888: Container with networking. You need to run user-space network stack c2w-net implemented based on gvisor-tap-vsock on the host (outside of browser). It forwards all packets received from the browser over WebSocket. See also ./examples/emscripten/ for detalis and configuration. (tested only on Linux)
  • localhost:8080: Container without networking.

Please also refer to ./examples/networking/ for details about networking.

Getting Started

  • requirements
    • Docker 18.09+ (w/ DOCKER_BUILDKIT=1)
    • Docker Buildx v0.8+ (recommended) or docker build (w/ DOCKER_BUILDKIT=1)

You can install the converter command c2w using one of the following methods.

NOTE: The output binary also contains c2w-net which a command usable for controlling networking feature (please see also ./examples/networking for details).

Release binaries

Binaries are available from https://github.com/container2wasm/container2wasm/releases Extract the tarball and put the binary somewhere under $PATH.

Building binaries using make

Go 1.19+ is needed.

bash
make
sudo make install

Command reference

c2w

Converts a container image into a WASM image and writes it to the specified path (default: out.wasm at the current directory).

Usage: c2w [options] image-name [output file]

  • image-name: container image name (will be pulled from the registry if it doesn't exist in Docker)
  • [output file]: path to the result WASM file.

Sub commands

  • help, h: Shows a list of commands or help for one command

Options

  • --assets value: Custom location of build assets.
  • --dockerfile value: Custom location of Dockerfile (default: embedded to this command)
  • --builder value: Bulider command to use (default: "docker")
  • --target-arch value: target architecture of the source image to use (default: "amd64")
  • --build-arg value: Additional build arguments (please see Dockerfile for available build args)
  • --to-js: convert the container to WASM using emscripten
  • --debug-image: Enable debug print in the output image
  • --show-dockerfile: Show default Dockerfile
  • --legacy: Use "docker build" instead of buildx (no support for assets flag) (default:false)
  • --external-bundle: Do not embed container image to the Wasm image but mount it during runtime
  • --help, -h: show help
  • --version, -v: print the version

c2w-net

Runs the user-space network stack used for networking support in converted WASM images. It can connect to a WASI runtime's network socket, listen for browser networking over WebSocket, or invoke a WASI image with wasmtime.

Usage:

  • c2w-net [options] socket-address
  • c2w-net --listen-ws [options] listen-address
  • c2w-net --invoke [options] wasm-file [wasm options] [COMMAND] [ARG...]

Arguments:

  • socket-address: TCP address of the WASI runtime network socket.
  • listen-address: address for the WebSocket listener, for example localhost:8888.
  • wasm-file [wasm options] [COMMAND] [ARG...]: WASM image and arguments passed to wasmtime when using --invoke.

Options:

  • --debug: Enable debug print.
  • --enable-tls: Enable TLS for the WebSocket connection.
  • --invoke: Invoke the container with networking support using wasmtime.
  • --listen-ws: Listen on a WebSocket address specified by listen-address.
  • --mac value: MAC address assigned to the container (default: "02:00:00:00:00:01").
  • -p value: Map a port between host and guest (host:guest or ip:host:guest). The --mac flag must be set correctly.
  • --wasi-addr value: IP address used to communicate between WASI and the network stack when using --invoke (default: "127.0.0.1:1234").
  • --wasmtime-cli-13: Use the old wasmtime CLI syntax for version 13 or earlier.
  • --ws-cert value: TLS certificate for the WebSocket connection.
  • --ws-key value: TLS key for the WebSocket connection.

Examples:

c2w-net --listen-ws localhost:8888
c2w-net --invoke -p localhost:8000:80 /tmp/out/httpd.wasm --net=socket

Run-time flags for WASM image

You can specify run-time flags to the generated wasm image for configuring the execution (e.g. for changing command to run in the container).

Usage: out.wasm [options] [COMMAND] [ARG...]

  • [COMMAND] [ARG...]: command to run in the container. (default: commands specified in the image config)

Options

  • -entrypoint <command> : entrypoint command. (default: entrypoint specified in the image config)
  • -no-stdin : disable stdin. (default: false)

Example:

The following changes the container's entrypoint to echo and pass hello to the arguments.

wasmtime -- /app/out.wasm --entrypoint=echo hello

Directory mapping

Directory mapped from the host is accessible on the container.

$ mkdir -p /tmp/share/ && echo hi > /tmp/share/hi
$ wasmtime --mapdir /test/dir/share::/tmp/share /app/out.wasm ls /test/dir/share/
hi

Motivation

Though more and more programming languages start to support WASM, it's not easy to run the existing program

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Gocontainersdockerwasmwebassembly

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理