diff options
author | Andrew Clayton <a.clayton@nginx.com> | 2023-08-02 17:03:48 +0100 |
---|---|---|
committer | Andrew Clayton <a.clayton@nginx.com> | 2023-08-21 23:24:12 +0100 |
commit | d6ed6a219b31a58526721f96195c80061d41ce54 (patch) | |
tree | 17a1fd6ecf72a327916ff0f8bc7aaf85b981ceff /examples/docker | |
download | unit-wasm-0.1.0.tar.gz unit-wasm-0.1.0.tar.bz2 |
Initial commitv0.1.0
libunit-wasm and example C and Rust WebAssembly modules for NGINX Unit.
Co-developed-by: Timo Stark <t.stark@nginx.com>
Co-developed-by: Liam Crilly <liam@nginx.com>
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'examples/docker')
-rw-r--r-- | examples/docker/README.md | 31 | ||||
-rw-r--r-- | examples/docker/demo-wasm.Dockerfile | 20 | ||||
-rw-r--r-- | examples/docker/unit-wasm.Dockerfile | 40 | ||||
-rw-r--r-- | examples/docker/wasm-conf.json | 76 |
4 files changed, 167 insertions, 0 deletions
diff --git a/examples/docker/README.md b/examples/docker/README.md new file mode 100644 index 0000000..61d2740 --- /dev/null +++ b/examples/docker/README.md @@ -0,0 +1,31 @@ +Unit-Wasm demo +============== + +## Build the docker images + +From the repository root, run + +```shell +$ make docker +``` + +This builds two docker images. + +### 1. unit:wasm + +This image is based on the Docker Official Images for Unit 1.30 with a fresh +build of unitd and the experimental Wasm module. Wasmtime is included as a +shared object. + +### 2. unit:demo-wasm + +This image is based on the new `unit:wasm` image created above. It includes +a demo application written in C and compiled to wasm. + +## Run the demo + +```shell +$ docker run -d -p 9000:80 unit:demo-wasm +$ curl localhost:9000 +$ curl localhost:9000/echo +``` diff --git a/examples/docker/demo-wasm.Dockerfile b/examples/docker/demo-wasm.Dockerfile new file mode 100644 index 0000000..2385211 --- /dev/null +++ b/examples/docker/demo-wasm.Dockerfile @@ -0,0 +1,20 @@ +FROM unit:wasm AS build +WORKDIR /demo + +# Get all the build tools we need +# +RUN apt update && apt install -y wget build-essential clang llvm lld +RUN cd /usr/lib/llvm-11/lib/clang/11.0.1 && wget -O- https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/libclang_rt.builtins-wasm32-wasi-20.0.tar.gz | tar zxvf - +RUN wget -O- https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sysroot-20.0.tar.gz | tar zxfv - + +# Copy-in the demo application source code and build into a .wasm module +# +ADD ${PWD} /demo/ +RUN make WASI_SYSROOT=/demo/wasi-sysroot examples + +# Copy the .wasm modules and Unit configuration to the final Docker image +# that will run the demo application. +# +FROM unit:wasm +COPY --from=build /demo/examples/c/*.wasm /demo/ +ADD examples/docker/wasm-conf.json /docker-entrypoint.d diff --git a/examples/docker/unit-wasm.Dockerfile b/examples/docker/unit-wasm.Dockerfile new file mode 100644 index 0000000..b7b47a5 --- /dev/null +++ b/examples/docker/unit-wasm.Dockerfile @@ -0,0 +1,40 @@ +# Start with the minimal Docker Official Image so we can use the same defaults +# +FROM unit:minimal AS build +WORKDIR /src + +# Get all the build tools we need, including Wasmtime +# +#RUN apt update && apt install -y wget git build-essential clang lld libpcre2-dev libssl-dev +RUN apt update && apt install -y wget git build-essential libpcre2-dev libssl-dev +RUN wget -O- https://github.com/bytecodealliance/wasmtime/releases/download/v11.0.0/wasmtime-v11.0.0-$(arch)-linux-c-api.tar.xz \ + | tar Jxfv - && \ + mkdir /usr/lib/wasmtime && \ + cp /src/wasmtime-v11.0.0-$(arch)-linux-c-api/lib/* /usr/lib/wasmtime + +# Build NGINX JavaScript (njs) so that we have a feature-complete Unit +# +RUN git clone https://github.com/nginx/njs.git && \ + cd njs && \ + ./configure --no-libxml2 --no-zlib && \ + make + +# Build Unit with the Wasm module, copying the configure arguments from the +# official image. +# +RUN git clone https://github.com/nginx/unit.git && \ + cd unit && \ + wget -O- https://github.com/nginx/unit/pull/902.patch | patch -p1 && \ + ./configure $(unitd --version 2>&1 | tr ' ' '\n' | grep ^-- | grep -v opt=) \ + --cc-opt="-I/src/njs/src -I/src/njs/build" --ld-opt=-L/src/njs/build && \ + ./configure wasm --include-path=/src/wasmtime-v11.0.0-$(arch)-linux-c-api/include \ + --lib-path=/usr/lib/wasmtime --rpath && \ + make + +# Create a clean final image by copying over only Wasmtime, the new unitd +# binary, and the Wasm module. +# +FROM unit:minimal +COPY --from=build /src/unit/build/sbin/unitd /usr/sbin +COPY --from=build /src/unit/build/lib/unit/modules/wasm.unit.so /usr/lib/unit/modules +COPY --from=build /usr/lib/wasmtime/*.so /usr/lib/wasmtime/ diff --git a/examples/docker/wasm-conf.json b/examples/docker/wasm-conf.json new file mode 100644 index 0000000..5ed173e --- /dev/null +++ b/examples/docker/wasm-conf.json @@ -0,0 +1,76 @@ +{ + "access_log": "/dev/stdout", + "settings": { + "http": { + "log_route": true, + "max_body_size": 1073741824 + } + }, + + "listeners": { + "*:80": { + "pass": "routes" + } + }, + + "routes": [ + { + "match": { + "uri": "/echo*" + }, + "action": { + "pass": "applications/luw-echo-request" + } + }, + { + "match": { + "uri": "/upload*" + }, + "action": { + "pass": "applications/luw-upload-reflector" + } + }, + { + "match": { + "headers": { + "accept": "*text/html*" + } + }, + "action": { + "share": "/usr/share/unit/welcome/welcome.html" + } + }, + { + "action": { + "share": "/usr/share/unit/welcome/welcome.md" + } + } + ], + + "applications": { + "luw-echo-request": { + "type": "wasm", + "module": "/demo/luw-echo-request.wasm", + "request_handler": "luw_request_handler", + "malloc_handler": "luw_malloc_handler", + "free_handler": "luw_free_handler", + "module_init_handler": "luw_module_init_handler", + "module_end_handler": "luw_module_end_handler", + "access": { + "filesystem": [ + "/tmp", + "/var/tmp" + ] + } + }, + "luw-upload-reflector": { + "type": "wasm", + "module": "/demo/luw-upload-reflector.wasm", + "request_handler": "luw_request_handler", + "malloc_handler": "luw_malloc_handler", + "free_handler": "luw_free_handler", + "request_end_handler": "luw_request_end_handler", + "response_end_handler": "luw_response_end_handler" + } + } +} |