2.7 KiB
Webassembly
There are 3 things you need to do to run a WebAssembly module with youki.
- Build youki with wasm-wasmer feature flag enabled
- Build a container image with the WebAssembly module
- Run the container with youki
Build youki with wasm-wasmedge
or wasm-wasmer
feature flag enabled
-
Run
build.sh
with-f wasm-wasmedge
option../scripts/build.sh -o . -r -f wasm-wasmedge
The
wasm-wasmedge
feature will install WasmEdge Runtime library in the$HOME/.wasmedge
directory. To make the library avaible in your system, run the following command:export LD_LIBRARY_PATH=$HOME/.wasmedge/lib
or
source $HOME/.wasmedge/env
-
Run
build.sh
with-f wasm-wasmer
option../scripts/build.sh -o . -r -f wasm-wasmer
Build a container image with the WebAssembly module
If you want to run a webassembly module with youki, your config.json has to include either runc.oci.handler or module.wasm.image/variant=compat".
It also needs to specifiy a valid .wasm (webassembly binary) or .wat (webassembly test) module as entrypoint for the container. If a wat module is specified it will be compiled to a wasm module by youki before it is executed. The module also needs to be available in the root filesystem of the container obviously.
"ociVersion": "1.0.2-dev",
"annotations": {
"run.oci.handler": "wasm"
},
"process": {
"args": [
"hello.wasm",
"hello",
"world"
],
...
Compile a sample wasm module
A simple wasm module can be created by running
rustup target add wasm32-wasi
cargo new wasm-module --bin
cd ./wasm-module
vi src/main.rs
fn main() {
println!("Printing args");
for arg in std::env::args().skip(1) {
println!("{}", arg);
}
println!("Printing envs");
for envs in std::env::vars() {
println!("{:?}", envs);
}
}
Then compile the program to WASI.
cargo build --target wasm32-wasi
Build a container image with the module
Create a Dockerfile.
vi Dockerfile
FROM scratch
COPY target/wasm32-wasi/debug/wasm-module.wasm /
ENTRYPOINT ["wasm-module.wasm"]
Then build a container image with module.wasm.image/variant=compat
annotation. 1
sudo buildah build --annotation "module.wasm.image/variant=compat" -t wasm-module .
Run the wasm module with youki and podman
Run podman with youki as runtime. 1
sudo podman --runtime /PATH/WHARE/YOU/BUILT/WITH/WASM-WASMER/youki run localhost/wasm-module 1 2 3