### Generate Wasm Bundle and Setup emscripten Approach Source: https://github.com/ktock/container2wasm/blob/main/extras/imagemounter/README.md This command generates a Wasm bundle for the emscripten approach and prepares the necessary files. It copies example files, compresses the Wasm bundles, and sets up a local HTTP server. Ensure the output directory for the bundle is correctly specified. ```bash mkdir -p /tmp/outx/img c2w --to-js --external-bundle /tmp/outx/img/ mkdir -p /tmp/out-js3/ cp -R ./examples/no-conversion-emscripten/* /tmp/out-js3/ mv /tmp/outx/img /tmp/out-js3/htdocs/img make imagemounter.wasm cat ./out/imagemounter.wasm | gzip > /tmp/out-js3/htdocs/imagemounter.wasm.gzip ( cd ./extras/runcontainerjs && npx webpack && cp -R ./dist /tmp/out-js3/htdocs/ ) docker run --rm -p 127.0.0.1:8083:80 \ -v "/tmp/out-js3/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js3/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' ``` -------------------------------- ### Run Wazero with Networking Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/wasi/README.md Execute the wazero-test binary with a WASI image that has networking enabled. This example demonstrates basic network connectivity and package installation within the container. ```bash ./out/wazero-test -net /tmp/out/out.wasm --net=socket sh ``` -------------------------------- ### Generate Wasm Bundle and Setup WASI-on-browser Source: https://github.com/ktock/container2wasm/blob/main/extras/imagemounter/README.md This command generates a Wasm bundle for a Linux VM and prepares the necessary files for the WASI-on-browser approach. It also copies example files, compresses the Wasm bundles, and sets up a local HTTP server. ```bash mkdir /tmp/outx/ c2w --external-bundle /tmp/outx/out.wasm mkdir -p /tmp/out-js3/ cp -R ./examples/no-conversion-wasi-browser/* /tmp/out-js3/ cat /tmp/outx/out.wasm | gzip > /tmp/out-js3/htdocs/out.wasm.gzip make imagemounter.wasm cat ./out/imagemounter.wasm | gzip > /tmp/out-js3/htdocs/imagemounter.wasm.gzip ( cd ./extras/runcontainerjs && npx webpack && cp -R ./dist /tmp/out-js3/htdocs/ ) docker run --rm -p 127.0.0.1:8083:80 \ -v "/tmp/out-js3/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js3/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' ``` -------------------------------- ### Start Local Server for Browser Container Access Source: https://github.com/ktock/container2wasm/blob/main/examples/emscripten-simple/README.md Sets up a local HTTP server to serve the converted Wasm container. This command copies necessary configuration files and starts an Apache httpd container, mounting the Wasm files and configuration. Ensure the paths to configuration files and the output directory are correct. ```bash cp ./examples/emscripten/xterm-pty.conf /tmp/out-js4/ cp -R ./examples/emscripten-simple/htdocs/* /tmp/out-js4/htdocs/ docker run --rm -p 127.0.0.1:8082:80 \ -v "/tmp/out-js4/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js4/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' ``` -------------------------------- ### CORS-Enabled Registry Setup Source: https://context7.com/ktock/container2wasm/llms.txt Sets up a Docker registry with CORS headers enabled to serve container images directly to browsers. Requires creating a configuration file and starting the registry. ```bash # Create registry configuration with CORS mkdir /tmp/regconfig cat < /tmp/regconfig/config.yml version: 0.1 http: addr: :5000 headers: Access-Control-Allow-Origin: ["*"] Access-Control-Allow-Headers: ["*"] Access-Control-Expose-Headers: [Content-Range] X-Content-Type-Options: [nosniff] storage: filesystem: rootdirectory: /var/lib/registry EOF ``` ```bash # Start registry docker run --rm -d -p 127.0.0.1:5000:5000 \ -v /tmp/regconfig/config.yml:/etc/docker/registry/config.yml \ --name registry registry:2 ``` ```bash # Push image to local registry docker pull ubuntu:22.04 docker tag ubuntu:22.04 localhost:5000/ubuntu:22.04 docker push localhost:5000/ubuntu:22.04 ``` ```bash # Access in browser: localhost:8083?image=localhost:5000/ubuntu:22.04 ``` -------------------------------- ### Build Wazero Test Binary Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/wasi/README.md Build the wazero-test binary from the provided source code. This is a prerequisite for running the subsequent examples. ```bash ( cd ./tests/wazero && go build -o ../../out/wazero-test . ) ``` -------------------------------- ### Start c2w-net network stack Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/websocket/README.md Starts the `c2w-net` network stack, which listens for WebSocket connections on the specified address and port. This command should be run before starting the container. ```bash $ c2w-net --listen-ws localhost:8888 ``` -------------------------------- ### Serve WASM with Network Stack Source: https://github.com/ktock/container2wasm/blob/main/README.md Copies example files, downloads a network proxy WASM module, and runs an Apache HTTP server. The server is configured to use a custom xterm-pty.conf and serves the WASM application. This setup allows the container to perform networking when accessed via specific URLs. ```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/ktock/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' ``` -------------------------------- ### Set Up and Serve Browser Runtime for Imagemounter Source: https://context7.com/ktock/container2wasm/llms.txt Prepares the necessary files and starts a web server for the browser runtime when using imagemounter. It copies the external bundle WASM, the imagemounter WASM, and the runcontainerjs frontend, then serves them via Docker. ```bash mkdir -p /tmp/out-js3/ cp -R ./examples/no-conversion-wasi-browser/* /tmp/out-js3/ cat /tmp/outx/out.wasm | gzip > /tmp/out-js3/htdocs/out.wasm.gzip cat ./out/imagemounter.wasm | gzip > /tmp/out-js3/htdocs/imagemounter.wasm.gzip ( cd ./extras/runcontainerjs && npx webpack && cp -R ./dist /tmp/out-js3/htdocs/ ) docker run --rm -p 127.0.0.1:8083:80 \ -v "/tmp/out-js3/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js3/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' ``` -------------------------------- ### Build Imagemounter CLI Tool Source: https://github.com/ktock/container2wasm/blob/main/extras/imagemounter/README.md Builds the imagemounter-test CLI tool from its source code. This is a prerequisite for using the CLI examples. ```console ( cd ./tests/imagemounter-test/ ; go build -o ../../out/imagemounter-test . ) ``` -------------------------------- ### Serve WASI image via HTTP server Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/websocket/README.md Copies the necessary files for the WASI browser environment and starts an Apache HTTP server. The server is configured to serve the WASI application and includes a custom configuration for pseudo-terminals. This setup is required to access the container from a web browser. ```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' ``` -------------------------------- ### Prepare and Run WASI PHP Image in Browser Source: https://github.com/ktock/container2wasm/blob/main/examples/php-x86_64/README.md Converts a PHP image to WASI and sets up an Apache web server to serve it in a browser. Requires copying example files and configuring the server. ```bash $ c2w php:8.1-cli-alpine3.16 /tmp/out-js2/htdocs/out.wasm $ 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' ``` -------------------------------- ### Prepare and Serve Wasm Container Source: https://github.com/ktock/container2wasm/blob/main/examples/emscripten/README.md This command sequence prepares the Wasm files, including webpack compilation and copying assets, then starts an Apache HTTP server to serve the container. It mounts the Wasm files and configuration into the container. ```bash ( cd ./examples/emscripten/htdocs/ && npx webpack && cp -R index.html dist vendor/xterm.css /tmp/out-js4/htdocs/ ) wget -O /tmp/c2w-net-proxy.wasm https://github.com/ktock/container2wasm/releases/download/v0.5.0/c2w-net-proxy.wasm cat /tmp/c2w-net-proxy.wasm | gzip > /tmp/out-js4/htdocs/c2w-net-proxy.wasm.gzip cp ./examples/emscripten/xterm-pty.conf /tmp/out-js4/ docker run --rm -d -p 8080:80 \ -v "/tmp/out-js4/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js4/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' ``` -------------------------------- ### Run Wazero with Port Mapping Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/wasi/README.md Execute the wazero-test binary with the httpd WASI image and configure port mapping. This example maps host port 8000 to container port 80, allowing access to the httpd server. ```bash ./out/wazero-test -net -p localhost:8000:80 /tmp/out/httpd.wasm --net=socket ``` -------------------------------- ### Map host directory to WASM container Source: https://github.com/ktock/container2wasm/blob/main/README.md This example demonstrates how to map a directory from the host machine into the WASM container. The mapped directory can then be accessed and manipulated within the container's filesystem. ```bash mkdir -p /tmp/share/ && echo hi > /tmp/share/hi wasmtime --mapdir /test/dir/share::/tmp/share /app/out.wasm ls /test/dir/share/ ``` -------------------------------- ### Start WebSocket Network Delegate Source: https://github.com/ktock/container2wasm/blob/main/examples/emscripten/README.md Starts the `c2w-net` proxy to listen for WebSocket connections on a specified address and port. This is used for WebSocket-based networking when accessing the containerized application. ```bash c2w-net --listen-ws localhost:8888 ``` -------------------------------- ### Start Emscripten Module Execution Source: https://github.com/ktock/container2wasm/blob/main/tests/scripts/testpage/index.html Initializes and starts the Emscripten module, setting up file system directories and writing initial info. It also configures the TTY stream polling mechanism for handling terminal input. ```javascript function start(info) { Module['preRun'].push((mod) => { try { FS.mkdir('/pack'); } catch (e) {} mod.FS.writeFile('/pack/info', info); }); var readableCallbacks = []; Module.pty.onReadable(() => { readableCallbacks.forEach(cb => cb()); readableCallbacks = []; }); Module['preRun'].push((Module) => { Module['TTY'].stream_ops.poll = (stream, timeout, notifyCallback) => { if (Module.pty.readable) { return 1; } if (notifyCallback != null) { notifyCallback.registerCleanupFunc(() => { const i = readableCallbacks.indexOf(notifyCallback); if (i != -1) readableCallbacks.splice(i, 1); }); readableCallbacks.push(notifyCallback); } return 0; }; }); (async () => { const instance = await initEmscriptenModule(Module); })(); } ``` -------------------------------- ### Worker Initialization and Network Stack Setup Source: https://github.com/ktock/container2wasm/blob/main/examples/wasi-browser/htdocs/index.html Initializes a Web Worker and sets up the network stack based on query parameters. Supports 'delegate' and 'browser' network modes, or a default initialization if no network parameters are provided. ```javascript const worker = new Worker("./worker.js"+location.search); var nwStack; var netParam = getNetParam(); var workerImage = location.origin + "/out.wasm"; if (netParam) { if (netParam.mode == 'delegate') { nwStack = delegate(worker, workerImage, netParam.param); } else if (netParam.mode == 'browser') { nwStack = newStack(worker, workerImage, new Worker("./stack-worker.js"+location.search), location.origin + "/c2w-net-proxy.wasm"); } } if (!nwStack) { worker.postMessage({type: "init", imagename: workerImage}); } new TtyServer(slave).start(worker, nwStack); ``` -------------------------------- ### Start WASI Container in Worker Source: https://github.com/ktock/container2wasm/blob/main/extras/runcontainerjs/README.md Starts a WASI container in a worker thread using pre-initialized container information and arguments. Requires a TtyClient for communication. ```javascript var ttyClient = new TtyClient(msg.data); RunContainer.startContainer(info, args, ttyClient); ``` -------------------------------- ### Initialize Emscripten Module Source: https://github.com/ktock/container2wasm/blob/main/examples/emscripten-simple/htdocs/index.html Asynchronously initializes the Emscripten module, which is necessary to start the WebAssembly application. This should be called after all configurations are set. ```javascript (async () => { const instance = await initEmscriptenModule(Module); })(); ``` -------------------------------- ### Write Mini Uname C Program Source: https://github.com/ktock/container2wasm/blob/main/examples/llm-agent/README.md This prompt guides the LLM to create a C program that calls the uname() syscall and prints the kernel name. Ensure the workspace name is correctly substituted when compiling. ```markdown Write a small C program mini_uname.c that calls the uname() syscall of , gets the "struct utsname" data, and prints the kernel name contained in the sysname field of the struct. Test it in the following steps. - Compile it using gcc -o /tmp/a.out /workspace//mini_uname.c - Run it and check if the kernel name printed by the program matches to the real uname command output. ``` -------------------------------- ### Build Frontend and Prepare Network Proxy for Emscripten Source: https://context7.com/ktock/container2wasm/llms.txt Builds the frontend assets using webpack and copies necessary files, including the network proxy WASM, to the output directory for Emscripten deployment. Ensure you have webpack installed. ```bash ( cd ./examples/emscripten/htdocs/ && npx webpack && cp -R index.html dist vendor/xterm.css /tmp/out-js/htdocs/ ) wget -O /tmp/c2w-net-proxy.wasm https://github.com/ktock/container2wasm/releases/download/v0.5.0/c2w-net-proxy.wasm cat /tmp/c2w-net-proxy.wasm | gzip > /tmp/out-js/htdocs/c2w-net-proxy.wasm.gzip cp ./examples/emscripten/xterm-pty.conf /tmp/out-js/ ``` -------------------------------- ### Run WASM Python on Runtime Source: https://github.com/ktock/container2wasm/blob/main/examples/python-x86_64/README.md Executes a WASM-converted Python image using the wasmtime runtime. This example demonstrates passing arguments to the Python interpreter. ```bash $ wasmtime -- /tmp/out/out.wasm python3 -c "print ('hello world')" ``` -------------------------------- ### Convert and Deploy Container with Fetch API Networking Source: https://context7.com/ktock/container2wasm/llms.txt Converts a container image for browser use and deploys it with the c2w-net-proxy. This setup enables in-browser networking using the Fetch API, suitable for CORS-allowed sites. ```bash c2w debian-curl /tmp/out-js2/htdocs/out.wasm wget -O /tmp/out-js2/htdocs/c2w-net-proxy.wasm https://github.com/ktock/container2wasm/releases/download/v0.5.0/c2w-net-proxy.wasm 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' ``` -------------------------------- ### Build Debian Container with Curl Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/fetch/README.md Builds a Docker image named `debian-curl` that includes the `curl` utility. This image is a prerequisite for running the containerized `curl` example in the browser. ```dockerfile FROM debian:sid-slim RUN apt-get update && apt-get install -y curl ``` -------------------------------- ### Build riscv64 Python Docker Image Source: https://github.com/ktock/container2wasm/blob/main/examples/python-riscv64/README.md Builds a riscv64 Ubuntu image with Python 3 installed using Docker Buildx for multi-platform support. Ensure Docker Buildx v0.8+ is configured. ```console cat < { try { FS.mkdir('/pack'); } catch (e) {} mod.FS.writeFile('/pack/info', info); }); ``` -------------------------------- ### Run WASI image on wasmtime with networking Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/wasi/README.md Launches a WASI image (out.wasm) on wasmtime with host networking enabled via the --net=socket flag. This command also starts a shell within the container. ```bash $ c2w-net --invoke /tmp/out/out.wasm --net=socket sh ``` -------------------------------- ### Compile Network Proxy to WASM Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/fetch/README.md Compiles the `c2w-net-proxy` Go project into a WebAssembly binary. This WASM module acts as an HTTP/HTTPS proxy within the browser, forwarding requests from the container using the Fetch API. Ensure Go >= 1.21 is installed. ```makefile PREFIX=/tmp/out-js2/htdocs/ make c2w-net-proxy.wasm ``` -------------------------------- ### Serve and Run Container Image in Browser Source: https://github.com/ktock/container2wasm/blob/main/examples/no-conversion-emscripten/README.md Sets up a local web server to serve the prepared container image and launches it in the browser. This involves copying files, building imagemounter.wasm, and running an Apache httpd container with specific configurations. ```bash mkdir -p /tmp/out-js3/ cp -R ./examples/no-conversion-emscripten/* /tmp/out-js3/ cp -R /tmp/imageout /tmp/out-js3/htdocs/ubuntu-22.04 make imagemounter.wasm && cat ./out/imagemounter.wasm | gzip > /tmp/out-js3/htdocs/imagemounter.wasm.gzip mv /tmp/outimg /tmp/out-js3/htdocs/img ( cd extras/runcontainerjs ; npx webpack && cp -R ./dist /tmp/out-js3/htdocs/ ) docker run --rm -p 127.0.0.1:8083:80 \ -v "/tmp/out-js3/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js3/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' ``` -------------------------------- ### Set up Local Server for Browser Execution Source: https://github.com/ktock/container2wasm/blob/main/extras/imagemounter/README.md Prepares a local directory with necessary files, including the created eStargz and legacy images, and then runs an Apache HTTP server. This server is configured to serve the container images and the `imagemounter.wasm` application for browser-based lazy pulling. ```console $ mkdir -p /tmp/out-js4/ $ cp -R ./examples/no-conversion-wasi-browser/* /tmp/out-js4/ $ cp -R /tmp/gcc-13.2-org /tmp/out-js4/htdocs/ $ cp -R /tmp/gcc-13.2-esgz /tmp/out-js4/htdocs/ $ make imagemounter.wasm && cat ./out/imagemounter.wasm | gzip > /tmp/out-js4/htdocs/imagemounter.wasm.gzip $ cat out.wasm | gzip > /tmp/out-js4/htdocs/out.wasm.gzip $ ( cd extras/runcontainerjs ; npx webpack && cp -R ./dist /tmp/out-js4/htdocs/ ) $ docker run --rm -p 127.0.0.1:8084:80 \ -v "/tmp/out-js4/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js4/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' ``` -------------------------------- ### Serve Container Image and Run in Browser Source: https://github.com/ktock/container2wasm/blob/main/examples/no-conversion-wasi-browser/README.md Sets up a local web server to host the container image and related Wasm files, enabling the container to be run in the browser. This involves copying necessary files, compressing Wasm binaries, and running an Apache HTTP server with specific configurations. ```console $ mkdir -p /tmp/out-js3/ $ cp -R ./examples/no-conversion-wasi-browser/* /tmp/out-js3/ $ cp -R /tmp/imageout /tmp/out-js3/htdocs/ubuntu-22.04 $ make imagemounter.wasm && cat ./out/imagemounter.wasm | gzip > /tmp/out-js3/htdocs/imagemounter.wasm.gzip $ cat out.wasm | gzip > /tmp/out-js3/htdocs/out.wasm.gzip $ ( cd extras/runcontainerjs ; npx webpack && cp -R ./dist /tmp/out-js3/htdocs/ ) $ docker run --rm -p 127.0.0.1:8083:80 \ -v "/tmp/out-js3/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js3/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' ``` -------------------------------- ### Load Container Image and Run Source: https://github.com/ktock/container2wasm/blob/main/examples/no-conversion-emscripten/htdocs/index.html Defines the addresses for necessary JavaScript and WASM files, then initiates the creation of a QEMU WASM container. This function is crucial for launching the containerized environment. ```javascript const vmImage = location.origin + "/img"; const outJsAddr = vmImage + "/out.js"; const argModuleJsAddr = vmImage + "/arg-module.js"; const loadJsAddr = vmImage + "/load.js"; const mounterImage = location.origin + "/imagemounter.wasm.gzip"; const stackWorkerFile = location.origin + "/dist/stack-worker.js"; const containerImageAddress = getImageParam(); Module = await RunContainer.createContainerQEMUWasm(Module, outJsAddr, containerImageAddress, stackWorkerFile, mounterImage, argModuleJsAddr, loadJsAddr, (p) => vmImage + "/" + p); ``` -------------------------------- ### Install packages in WASI container Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/wasi/README.md Updates package lists and installs the 'figlet' package within the running WASI container. This demonstrates package management capabilities over the established network connection. ```bash apk update && apk add --no-progress figlet ``` -------------------------------- ### Compile and Serve Emulated Raspberry Pi Image Source: https://github.com/ktock/container2wasm/blob/main/examples/raspi3ap-qemu/README.md Compiles the project, prepares assets, converts the Docker image for JavaScript execution, and serves it via a Dockerized Apache server. This makes the emulated environment accessible in the browser. ```bash make mkdir -p /tmp/out-js6/htdocs ./out/c2w --dockerfile=Dockerfile --assets=. --to-js --target-arch=aarch64 --pack=/tmp/out/ /tmp/out-js6/htdocs/ cp -R ./examples/raspi3ap-qemu/src/* /tmp/out-js6/ docker run --rm -p 8086:80 \ -v "/tmp/out-js6/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js6/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' ``` -------------------------------- ### Prepare Container Image in OCI Layout Source: https://github.com/ktock/container2wasm/blob/main/examples/no-conversion-emscripten/README.md Builds a specified container image (e.g., ubuntu:22.04) and outputs it in OCI Image Layout format to a specified directory. This uses docker buildx to create the OCI archive. ```bash IMAGE=ubuntu:22.04 mkdir /tmp/imageout/ docker buildx create --name container --driver=docker-container echo "FROM $IMAGE" | docker buildx build --builder=container --output type=oci,dest=- - | tar -C /tmp/imageout/ -xf - ``` -------------------------------- ### Build WASI Polyfill (browser_wasi_shim) Source: https://github.com/ktock/container2wasm/blob/main/examples/wasi-browser/README.md Build the WASI polyfill library using Docker. The output will be placed in the ./htdocs/ directory, making it available for the browser. ```console $ docker buildx build --output type=local,dest=./htdocs/ . ``` -------------------------------- ### Start Emscripten Module and Configure TTY Polling Source: https://github.com/ktock/container2wasm/blob/main/examples/emscripten/htdocs/index.html Initializes the Emscripten module and configures the TTY stream polling mechanism to handle asynchronous readable events from the pseudo-terminal. ```javascript function start(info) { Module['preRun'].push((mod) => { try { FS.mkdir('/pack'); } catch (e) {} mod.FS.writeFile('/pack/info', info); }); var readableCallbacks = [] Module.pty.onReadable(() => { readableCallbacks.forEach(cb => cb()); readableCallbacks = [] }); Module['preRun'].push((Module) => { Module['TTY'].stream_ops.poll = (stream, timeout, notifyCallback) => { if (Module.pty.readable) { return 1; } if (notifyCallback != null) { notifyCallback.registerCleanupFunc(() => { const i = readableCallbacks.indexOf(notifyCallback); if (i != -1) readableCallbacks.splice(i, 1); }); readableCallbacks.push(notifyCallback); } return 0; }; }); (async () => { const instance = await initEmscriptenModule(Module); })(); } ``` -------------------------------- ### Build Debian Image with Curl Source: https://github.com/ktock/container2wasm/blob/main/README.md Builds a Docker image named 'debian-curl' based on Debian sid-slim, installing the curl package. This image can then be converted to a WASM module. ```bash $ cat < vmImage + "/" + p); ``` -------------------------------- ### Generate ASCII art with figlet Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/wasi/README.md Uses the 'figlet' command to generate ASCII art from the input 'hello'. This command is executed within the WASI container after installing the figlet package. ```bash figlet hello ``` -------------------------------- ### Build WASM Network Proxy (Alternative) Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/fetch/README.md Alternatively, build the c2w-net-proxy.wasm from source. Requires Go version 1.21 or later and should be run from the project repository root. ```bash $ PREFIX=/tmp/out-js2/htdocs/ make c2w-net-proxy.wasm ``` -------------------------------- ### Get Container Image from URL Parameter Source: https://github.com/ktock/container2wasm/blob/main/examples/no-conversion-emscripten/htdocs/index.html A utility function to extract the container image address from the URL's query parameters. It looks for an 'image' parameter and returns its decoded value. ```javascript function getImageParam() { var vars = location.search.substring(1).split('&'); for (var i = 0; i < vars.length; i++) { var kv = vars[i].split('='); if (decodeURIComponent(kv[0]) == 'image') { return kv[1]; } } return null; } ``` -------------------------------- ### Build container2wasm Binaries Source: https://github.com/ktock/container2wasm/blob/main/README.md Build the container2wasm command-line tool using make. Requires Go 1.19+. ```bash make sudo make install ``` -------------------------------- ### Convert and Serve Container with WebSocket Delegation Source: https://context7.com/ktock/container2wasm/llms.txt Converts a container image and sets up a web server to host the browser runtime. This configuration, when accessed with the appropriate URL parameter, enables full network access through the WebSocket delegated proxy. ```bash c2w alpine:3.18 /tmp/out-js2/htdocs/out.wasm 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' ``` -------------------------------- ### Build c2w-net-proxy.wasm Source: https://github.com/ktock/container2wasm/blob/main/examples/emscripten/README.md Builds the `c2w-net-proxy.wasm` binary using the project's Makefile. Requires Go version 1.21 or later. ```bash make c2w-net-proxy.wasm ``` -------------------------------- ### Prepare Container Image in OCI Layout Source: https://github.com/ktock/container2wasm/blob/main/examples/no-conversion-wasi-browser/README.md Builds and extracts a specified container image (e.g., ubuntu:22.04) into the OCI Image Layout format at a designated directory. This uses docker buildx to create the OCI image and then extracts it. ```console $ IMAGE=ubuntu:22.04 $ mkdir /tmp/imageout/ $ docker buildx create --name container --driver=docker-container $ echo "FROM $IMAGE" | docker buildx build --builder=container --output type=oci,dest=- | tar -C /tmp/imageout/ -xf - ``` -------------------------------- ### Build Docker Image for Raspberry Pi Source: https://github.com/ktock/container2wasm/blob/main/examples/raspi3ap-qemu/README.md Builds a Docker image and outputs it to a local directory. This prepares the necessary files for emulation. ```bash mkdir /tmp/out/pack/ docker build --output=type=local,dest=/tmp/out/pack/ ./examples/raspi3ap-qemu/image/ ``` -------------------------------- ### Get Image Parameter from URL Source: https://github.com/ktock/container2wasm/blob/main/examples/no-conversion-wasi-browser/htdocs/index.html Retrieves the 'image' parameter value from the current URL's query string. Returns null if the parameter is not found. This function is used to specify the container image to be loaded. ```javascript function getImageParam() { var vars = location.search.substring(1).split('&'); for (var i = 0; i < vars.length; i++) { var kv = vars[i].split('='); if (decodeURIComponent(kv[0]) == 'image') { return kv[1]; } } return null; } ``` -------------------------------- ### Build c2w-net-proxy with Go command Source: https://github.com/ktock/container2wasm/blob/main/extras/c2w-net-proxy/README.md Manually build the c2w-net-proxy.wasm binary using the Go build command. This method allows specifying the target operating system and architecture. ```bash $ GOOS=wasip1 GOARCH=wasm go build -o /tmp/out/c2w-net-proxy.wasm . ``` -------------------------------- ### Build container2wasm Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/websocket/README.md Builds the `c2w` and `c2w-net` tools using `make`. Ensure you are in the project repository root directory. ```bash $ make ``` -------------------------------- ### Build and Prepare Container Image Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/fetch/README.md Builds a Docker image named `debian-curl` containing the `curl` utility and then compiles this image into a WASM file named `out.wasm` located at `/tmp/out-js2/htdocs/`. This prepares the container for execution within the browser. ```bash cat <> /usr/local/apache2/conf/httpd.conf && httpd-foreground' ``` -------------------------------- ### Prepare and Serve Emscripten WASM Application Source: https://github.com/ktock/container2wasm/blob/main/README.md This sequence of commands prepares the output from `c2w --to-js`, bundles it with webpack, downloads a network proxy WASM, and serves the application using an Apache HTTP server. It's used for running containers in the browser with networking capabilities. ```console ( cd ./examples/emscripten/htdocs/ && npx webpack && cp -R index.html dist vendor/xterm.css /tmp/out-js/htdocs/ ) $ wget -O /tmp/c2w-net-proxy.wasm https://github.com/ktock/container2wasm/releases/download/v0.5.0/c2w-net-proxy.wasm $ cat /tmp/c2w-net-proxy.wasm | gzip > /tmp/out-js/htdocs/c2w-net-proxy.wasm.gzip $ cp ./examples/emscripten/xterm-pty.conf /tmp/out-js/ $ docker run --rm -p 127.0.1:8080:80 \ -v "/tmp/out-js/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js/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' ``` -------------------------------- ### Launch Local Docker Registry Source: https://github.com/ktock/container2wasm/blob/main/extras/imagemounter/README.md Launches a local Docker registry and pushes an Ubuntu image to it. This is used for testing registry access with container2wasm. ```console docker run --rm -d -p 127.0.0.1:5000:5000 --name registry registry:2 docker pull ubuntu:22.04 docker tag ubuntu:22.04 localhost:5000/ubuntu:22.04 docker push localhost:5000/ubuntu:22.04 ``` -------------------------------- ### Prepare and Run Emscripten-compiled PHP Image in Browser Source: https://github.com/ktock/container2wasm/blob/main/examples/php-x86_64/README.md Sets up and runs a PHP image converted to JavaScript (via Emscripten) in a browser. This involves webpack compilation, copying files, downloading a proxy, and configuring an Apache server. ```bash $ ( cd ./examples/emscripten/htdocs/ && npx webpack && cp -R index.html dist vendor/xterm.css /tmp/out-js4/htdocs/ ) $ wget -O /tmp/c2w-net-proxy.wasm https://github.com/ktock/container2wasm/releases/download/v0.5.0/c2w-net-proxy.wasm $ cat /tmp/c2w-net-proxy.wasm | gzip > /tmp/out-js4/htdocs/c2w-net-proxy.wasm.gzip $ cp ./examples/emscripten/xterm-pty.conf /tmp/out-js4/ $ docker run --rm -p 8080:80 \ -v "/tmp/out-js4/htdocs:/usr/local/apache2/htdocs/:ro" \ -v "/tmp/out-js4/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' ``` -------------------------------- ### Build WASI image Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/websocket/README.md Builds a WASI image from a specified container image (e.g., `alpine:3.18`) and outputs the WASM file to a designated path. This step prepares the container for browser execution. ```bash $ c2w alpine:3.18 /tmp/out-js2/htdocs/out.wasm ``` -------------------------------- ### Launch Local Docker Registry with CORS Source: https://github.com/ktock/container2wasm/blob/main/extras/imagemounter/README.md Launches a local Docker registry with CORS enabled for browser access. Ensure the registry configuration is correctly set up. ```console mkdir /tmp/regconfig cat < /tmp/regconfig/config.yml version: 0.1 http: addr: :5000 headers: Access-Control-Allow-Origin: ["*"] Access-Control-Allow-Headers: ["*"] Access-Control-Expose-Headers: [Content-Range] X-Content-Type-Options: [nosniff] storage: filesystem: rootdirectory: /var/lib/registry EOF docker run --rm -d -p 127.0.0.1:5000:5000 -v /tmp/regconfig/config.yml:/etc/docker/registry/config.yml --name registry registry:2 docker pull ubuntu:22.04 docker tag ubuntu:22.04 localhost:5000/ubuntu:22.04 docker push localhost:5000/ubuntu:22.04 ``` -------------------------------- ### Run Containers in Browser with WASI Source: https://context7.com/ktock/container2wasm/llms.txt Convert containers for browser execution using `c2w --to-js`. This involves setting up browser runtime files and serving them via an HTTP server with specific configurations for terminal interaction. ```bash c2w ubuntu:22.04 /tmp/out-js2/htdocs/out.wasm ``` ```bash cp -R ./examples/wasi-browser/* /tmp/out-js2/ && chmod 755 /tmp/out-js2/htdocs ``` ```bash 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' ``` -------------------------------- ### Serve Container and Network Proxy in Browser Source: https://github.com/ktock/container2wasm/blob/main/examples/networking/fetch/README.md Sets up and runs an Apache HTTP Server using Docker to serve the container's WASM image (`out.wasm`) and the network proxy WASM binary (`c2w-net-proxy.wasm`). It configures the server to include `xterm-pty.conf` for pseudo-terminal support. The server is exposed on port 8080. ```docker 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' ``` -------------------------------- ### Initialize Terminal and PTY Source: https://github.com/ktock/container2wasm/blob/main/examples/no-conversion-emscripten/htdocs/index.html Sets up xterm.js for terminal emulation and opens a pseudo-terminal (pty) for interaction. This is essential for running command-line applications in the browser. ```javascript import 'https://unpkg.com/xterm@5.3.0/lib/xterm.js'; import 'https://unpkg.com/xterm-pty/index.js'; const xterm = new Terminal(); xterm.open(document.getElementById("terminal")); const { master, slave } = openpty(); xterm.loadAddon(master); ``` -------------------------------- ### Create eStargz and Legacy Images Source: https://github.com/ktock/container2wasm/blob/main/extras/imagemounter/README.md Builds both eStargz and legacy OCI images for a specified container image using `docker buildx`. The eStargz image is created with `compression=estargz` and `force-compression=true` options. ```console $ IMAGE=gcc:13.2 $ mkdir /tmp/gcc-13.2-org/ /tmp/gcc-13.2-esgz/ $ docker buildx create --name container --driver=docker-container $ echo "FROM $IMAGE" | docker buildx build --builder=container --output type=oci,dest=- - | tar -C /tmp/gcc-13.2-org/ -xf - $ echo "FROM $IMAGE" | docker buildx build --builder=container --output type=oci,oci-mediatype=true,compression=estargz,force-compression=true,dest=- - | tar -C /tmp/gcc-13.2-esgz/ -xf - ``` -------------------------------- ### Prepare WASI Container in Main Worker Source: https://github.com/ktock/container2wasm/blob/main/extras/runcontainerjs/README.md Prepares a WASI container in the main worker thread. This involves setting up the container image, mounter, and stack worker, then posting initialization info to a separate worker. Assumes xterm-pty for terminal. ```javascript const worker = new Worker("./worker.js"); const vmImage = location.origin + "/out.wasm.gzip"; const mounterImage = location.origin + "/imagemounter.wasm.gzip"; const stackWorkerFile = location.origin + "/dist/stack-worker.js"; const containerImageAddress = getImageParam(); const infoP = RunContainer.createContainerWASI(vmImage, containerImageAddress, stackWorkerFile, mounterImage); infoP.then((info) => { worker.postMessage({type: "init", info: info, args: ['/bin/sh']}); new TtyServer(slave).start(worker); }) ``` -------------------------------- ### Initialize Terminal and PTY for Browser Container Source: https://github.com/ktock/container2wasm/blob/main/examples/emscripten/htdocs/index.html Sets up Xterm.js for terminal display and configures pseudo-terminal (pty) for communication. Requires 'xterm' and 'xterm-pty' libraries. ```javascript import 'https://unpkg.com/xterm@5.3.0/lib/xterm.js'; import 'https://unpkg.com/xterm-pty/index.js'; import './arg-module.js' import initEmscriptenModule from './out.js'; const xterm = new Terminal(); xterm.open(document.getElementById("terminal")); const { master, slave } = openpty(); xterm.loadAddon(master); Module.pty = slave; ```