Skip to content

Instantly share code, notes, and snippets.

@orimanabu
Last active August 25, 2022 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orimanabu/1da77aa0a98fcc651979f19012835e6f to your computer and use it in GitHub Desktop.
Save orimanabu/1da77aa0a98fcc651979f19012835e6f to your computer and use it in GitHub Desktop.
Run WasmEdge container with Podman and crun on Fedora 36

How to run Wasm container with Podman and crun

Login to Fedora 36 Server

ssh fedoravm

Install prereq packages

container related packages (+ git, etc)

sudo dnf install git rpm-build crun podman buildah

Packages for WasmEdge build

sudo dnf install cmake clang lld llvm-devel lld-devel

Build and install WasmEdge

mkdir work
cd work
git clone https://github.com/WasmEdge/WasmEdge.git
cd WasmEdge/
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=${HOME}/.wasmedge -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_TESTS=ON .. && make -j
make install
ln -s ~/.wasmedge/lib64/libwasmedge.so  ~/.wasmedge/lib64/libwasmedge_c.so

Build and install crun with WasmEdge support

Rebuild crun from source rpm.

cd ~
dnf download --source crun
rpm -ivh crun-1.5-1.fc36.src.rpm
cd ~/rpmbuild/
sudo dnf builddep SPECS/crun.spec
rpmbuild -bp SPECS/crun.spec
cd BUILD/crun-1.5/
./autogen.sh
CPPFLAGS=-I${HOME}/.wasmedge/include LDFLAGS="-Wl,-R${HOME}/.wasmedge/lib64 -L${HOME}/.wasmedge/lib64" ./configure --prefix=${HOME}/.crun --with-wasmedge
make -j
make install

Make sure the crun has WasmEdge support

Check if crun --version includes +WASM:wasmedge.

$ ~/.crun/bin/crun --version
crun version 1.5.0.0.0.41-ce75
commit: b6c3139229b6e62292fea63e3334dafc053bd4bc
spec: 1.0.0
+SYSTEMD +SELINUX +APPARMOR +CAP +SECCOMP +EBPF +CRIU +LIBKRUN +WASM:wasmedge +YAJL

Build Wasm binary

Prepare for Rust development environment

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
rustup target add wasm32-wasi

Build sample Wasm code

cd ~/work
git clone https://github.com/second-state/wasm-learning.git
cd wasm-learning/cli/hello
cargo build --target wasm32-wasi

Run the Wasm binary as standalone

$ ~/.wasmedge/bin/wasmedge target/wasm32-wasi/debug/hello.wasm 
hello

Build container image from the Wasm binary

Create Dockerfile

cat > Dockerfile <<END
FROM scratch
COPY target/wasm32-wasi/debug/hello.wasm /
CMD ["/hello.wasm"]
END

Build Wasm container image

buildah build --annotation "module.wasm.image/variant=compat" -t mywasm-hello .

Run with Podman and crun

$ podman --runtime ~/.crun/bin/crun run localhost/mywasm-hello
hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment