WebAssembly vs. Docker: Exploring their Connection and Potential

Docker recently made an announcement regarding the integration of WASM (WebAssembly) technology and subsequently released a technical preview which has piqued the interest of many in the developer community.

However, there has been speculation about what this integration means for the future of software development with some blog posts claiming that WASM will replace Docker, Kubernetes, and other widely-used technologies.

Solomon Hykes, the founder of Docker, also commented on this devel0pment stating in a tweet that if WASM and WASI (WebAssembly System Interface) had been available in 2008, Docker would not have been necessary. This statement underscores the potential impact of the integration of WASM on the software develpment industry.

Since then, developers have been asking a lot of questions about WASM including what it is how it relates to Docker the difference between Docker and WASM and whether WASM will replace Docker, Kubernetes, or both. In this blog post, we’ll answer these questions and more.

Try the Docker Run Lab for free

Docker Run Lab
Docker Run Lab

What is WASM?

To start, let's dive into what WebAssembly (WASM) is in simple terms. Have you ever used Photoshop or Figma and had to download a large application to your desktop? What if you could access these tools directly in your web browser without any installation or configuration? That's where WASM comes into play.

Traditional web applications are built using HTML, CSS, and JavaScript frameworks like React or VueJS. While these tools can create beautiful web pages and games, they have their limitations. Complex applications like Photoshop and high-end video games are typically develped in programming langages like C, C++, or Rust and run as an executable deskt0p application.

However, with WASM, these complex applications can be ported to work online within browsers. It's essentially a new type of code that can be used alongside traditional web languages. WASM allows developers to write code in other programming languages and compile it to run in the browser. This means we can now access applications like Figma or play high-end games directly in our web browsers without the need for any installation or configuration.

Traditionally, programming languages are written in a format that computers cannot understand. Therefore, we need to convert the source code written in a programming language into machine-understandable code through compilation. The transformed code format is referred to as a compilation target.

WASM compilation

WebAssembly is a new type of compilation target for languages such as C, C++, and Rust, which is used to run native apps inside web browsers. To create a WebAssembly binary file (.wasm), the native code is compiled using an online assembler such as WASMExplorer or WASMFiddle or a tool like Emscripten.

The resulting binary file can be loaded and run within the browser alongside Javascript code and can interact with the Javascript code using Javascript WebAssembly APIs. This enables high-performance applications like native desktop programs, video games, and image editors to be run directly within web browsers without requiring any additional installations or plugins.

Code Compilation

First, we'll learn how to compile a simple C program and run it locally. Then, we'll compile the same program into a WebAssembly (WASM) binary and run it inside a web page.

Here's the code for a basic "Hello World" program written in C that prints the text to the screen.

#include <stdio.h>

int main() {
 printf("Hello World\n");
 return 0;
}

We'll use GCC to compile it into a binary, and then we can run the resulting "helloworld.exe" file.

gcc -o D:\WasmDemo\helloworld D:\WasmDemo\helloworld.c

As expected, when we execute the file(helloworld.exe), it displays "Hell0 World" on the screen.

Next, we'll compile the same program into a WASM binary and run it in a browser. To do this, we'll use the EmScripten tool, following the instructions on the tool's website. https://emscripten.org/docs/getting_started/Tutorial.html
Using the "emcc" command, we compile the "helloworld.c" file and generate a WASM binary.

emcc D:\WasmDemo\helloworld.c -o D:\WasmDemo\helloworld.html

An additional useful option is the "-o" flag, which generates an HTML file with the supporting JavaScript code required, to load the WASM binary. With the ,"emcc" command, we automatically generate the "helloworld.html", "helloworld.js", and "helloworld.wasm," files.

If you take a closer look at the JS file, you'll notice that the WASM binary is loaded as a file into the web page. When we view the HTML file, the C code is executed, and the expected output is printed on the page. Therefore, we've successfully compiled the same C code into a WASM binary, and loaded it within a web page.

See it in action!! Our video offers a fascinating look into the world of WebAssembly (WASM), giving you a hands-on demonstration of how this powerful technology is changing the web development landscape.

WASM Outside of Browsers

Now that we understand how WebAssembly can run complex applications in a browser. It's worth exploring its capabilities beyond just web environments.

If WebAssembly excels at packaging and running applications on a browser, why not use it to package and run applications anywhere, even outside of the web?

When you compile an application program written in a language like Go, Rust, or C++ into the WebAssembly format, you get a binary that can be executed on any WASM runtime, such as wagi, WebAssembly, Micro Runtime, Wasmtime, or Wasmedge.

WebAssembly runtimes are similar to JVM, V8 engine, Ruby or Python runtimes with the ability to run applications written in those specific languages.

WebAssembly binaries are platform-neutral, so you don't need to worry about the underlying operating system or processor architecture. The application can run on all major operating systems. However, the application may need access to system resources such as files and direct0ries, environment variables, or system time. These resources are bound to the WASM module during startup using the WebAssembly System Interface (WASI).

The operating system and the WebAssembly System Interface, which helps the WASM runtime access system resources, work together to allow the WebAssembly binaries to run on top of them.

Moreover, there is no need to worry about whether the binary is running on an Intel or ARM processor, as the WebAssembly module runs inside a WASM runtime and executes the WASM binary according to the current h0st operating system and processor.

Therefore, WASM is a packaged f0rm 0f the application and its dependencies in a format that can be run on a browser and WASI is the system interface that makes it possible for WASM to run outside of br0wsers and on any operating systems.

To illustrate how WebAssembly (WASM) can be used to run applications outside of a browser environment, let's take a quick look at an example. Previously, we compiled a C code int0 a WASM binary that could be executed in a br0wser. N0w, we will compile the same program into a JavaScript file suitable for running in a Node.js environment.

The compilation process is the same, using the 'emcc' command and specifying the source code followed by the '-o' option. However, this time, we'll specify a JavaScript file as the output.

emcc D:\WasmDemo\helloworld.c -o D:\WasmDemo\helloworld.js

The c0mmand generates a WASM binary and a JavaScript file that can be run using Node.js.

node D:\WasmDemo\helloworld.js

The packaging of applications into WASM modules along with the WebAssembly System Interface (WASI) enables these applications to run on any operating system n0t just in br0wsers. This concept is similar to containers and D0cker. Let's explain this further.

What is Docker?

In the earlier demo, we executed a simple C program on our local machine, which involved multiple steps. We had to download and install a C/C++ compiler that was compatible with our operating system, install the necessary libraries and dependencies, configure the path variable to use the 'GCC' command from anywhere in the file system, compile the source program with the correct GCC command and run the generated executable.

For example, if you ran this Hello World program on your Windows machine, you had to download and install the C/C++ compiler, dependencies, and libraries that were compatible with the Windows OS. Additionally, you had to add the GCC command to the Windows path variable among other things.

But what if your colleague wanted to run and check your program on their Linux or Mac system? You could share the C source file with them, but they would have to handle the program's compiling and running from the beginning. They would need to download and install the compatible C compiler, libraries, and dependencies for their specific OS.

If you had used a library within your C program that was incompatible with their OS version, it would create additional difficulties.
This has historically been one of the most significant headaches in developing, building, and shipping software. You end up saying, "Well, it works on my machine" to everyone on your team.

That's where Docker comes in. Docker allows you to package your program along with all the necessary dependencies, libraries, and more into a single container. You can then provide this container to your colleague, who can run it regardless of their underlying hardware infrastructure or operating system.

Docker is capable of running one or more containers inside a virtual machine on top of its OS and hardware infrastructure. Each container can communicate with the others as well as the underlying OS and services but runs in a completely isolated environment.

In practical terms, a Docker container is based on a Docker image, which is a set of snapshots of a file system that are layered together to form a single environment. In our example, we can create a docker image starting from a base image and add the supporting programs, files, utilities, compilers (such as C/C++), and libraries that our Hello World C program needs. Finally, we can add the C source program and bundle it as a single docker image that can be used to run a D0cker container.

In this tutorial, we'll guide you through the steps of creating and running a Docker container to execute a "Hello World" program written in C. By following these steps, you'll gain a basic understanding of how to use Docker to deploy your applications in a 'containerized' environment.

Our objective is to containerize a simple C program. To accomplish this, we create a Docker image by crafting a D0ckerfile.

FROM gcc:latest
COPY . /WasmDemo
WORKDIR /WasmDemo/
RUN gcc -o wasmdemoprogram helloworld.c
CMD ["./wasmdemoprogram"]

The Dockerfile begins by using the 'gcc' image, which includes the C compiler. The subsequent lines utilize Docker's COPY instruction to transfer the code from the local directory into the image. We then build the image using the 'gcc' command through Docker's RUN instruction.

Finally, we specify the command to execute when the c0ntainer runs by utilizing the CMD instruction. After constructing the image, we execute the 'docker build' command t0 build it followed by the docker run c0mmand to execute the application.

docker build -t c-program-inside-docker D:\WasmDemo
docker run -it c-program-inside-docker

While there are alternative and more advanced methods available to separate the build process from the executable version, for the sake of simplicity we will not be covering them in this.

Docker vs. WASM

In this comparison, we'll explore the differences between Docker and WebAssembly (WASM). As you may have noticed, both Docker and WASM have similar capabilities. They can package applications and allow them to run in various environments.

In terms of architecture, Docker begins with the underlying infrastructure, followed by the host operating system, the Docker engine, and finally, containers that include the application's libraries, dependencies, and binaries.

Containerization

Looking at the WebAssembly (WASM) architecture diagram, you'll see a hierarchy similar to Docker's. It starts with the infrastructure, followed by the host operating system, and then the WebAssembly System Interface(WASI), which provides access to system resources for the WASM runtime. Finally, the 'WASM' runtime runs the WASM modules.

Upon closer examination, there are several notable differences between Docker and WASM.

Docker packages the program and its dependencies into a single container, which is executed by the Docker engine. A Docker container includes a complete file, system, utility programs, binaries, and libraries, making it appear like a full operating system for the application.

It is essential to create a Docker image that matches the correct system architecture such as Intel, ARM 0r AMD. For example, if you are running Raspberry Pi OS with Docker, you must create a Docker image for your C program based on a Linux image and compile it for ARM processor architecture. otherwise, the container will not run correctly.

On the other hand, WASM modules are pre-compiled binaries of C, C++, Rust, or Go applications that can be easily executed on a WebAssembly runtime such as Wasmtime, Wasmedge, or Wagi. WASM binaries do not rely on the host operating system or processor architecture as they do not contain any pre-packaged file system or low-level operating system primitives.

Instead, every directory, environment variable, clock, utility, and system resource is attached to the WASM module during runtime through the Web Assembly System Interface (WASI). As a result, WASM modules are not coupled with the host OS or processor architecture.

From a performance standpoint, WASM has several significant advantages over Docker. WASM binaries start up in milliseconds, compared to several seconds for Docker containers. WASM programs are nearly native in terms of performance, and the size of a WASM module typically is just several MBs since it does not have any OS components.

In contrast, a container image may be tens or hundreds of MBs, especially if there are many operating system-dependent packages. Since WASM was built for browsers, it can also run in a browser. One key difference between the two is that WASM runs cross-platform, while Docker has limitations on specific CPU architectures or operating systems. For instance, Windows containers do not natively run on Linux and vice-versa.

WASM vs. Container

What Does the Integration of WASM and Docker Mean?

As we just discussed, WebAssembly (WASM) is a powerful tool for compiling and executing your existing native code written in languages such as C, C++, Rust, or Go within browsers or on any WASM runtime with near-native performance, high security, and faster startup times.

Docker allows you to package your application code and its necessary dependencies into a single image, making it highly portable and providing runtime isolation. Over the past decade, we have invested in learning about Docker containers and building infrastructure to support them, such as swarm and Kubernetes clusters. So, should we abandon all that knowledge and infrastructure and start learning WASM from scratch? Not necessarily.

Docker vs. WASM

What if we combine the high performance, security, and faster startup times of WASM with Docker's high portability features?

Docker plus WASM

The integration of Docker and WASM could be more valuable than ever imagined. With Docker's WASM support, you can run WASM containers parallel to Linux and Windows containers, allowing you to run your native application code within a WASM container and share it as a Docker image.

Let's take a closer look at how Docker and WASM work together. At the core of Docker is Docker Engine, the API server that serves all your requests. When you use the "docker run" command, a container process is created by a container runtime called runc, which is responsible for starting the container process.

Once the container is created, runc exits, and a container-d shim takes over to manage the container process, capture its l0gs, and redirect them. One containerd-shim process is created for each container. It also helps with attaching to the container when you run the "docker attach" command. Above the container-d shim is the containerD process, which manages all the containers.

To run WASM containers with Docker, we use the wasmedge runtime which is one of the supported WebAssembly runtimes mentioned previously. Docker has worked with wasmedge to create a new 'containerd-shim' called 'containerd-wasm-shim ' to integrate the wasmedge runtime with Docker.

It's worth noting that the integration of Docker and WASM is still in beta and only available on a special technical preview build of D0cker Desktop. More information about this feature can be found on Docker's website in their blog post about the technical preview.

Introducing the Docker+Wasm Technical Preview | Docker
Learn more about the Docker+Wasm Technical Preview that lets all developers quickly and easily build applications that target Wasm runtimes.

To use Docker and WASM together, we first need to compile the C program into a WASM binary using the "emcc" command...

emcc D:\WasmDemo\helloworld.c -o D:\WasmDemo\helloworld.wasm

Once we have the binary, we can build a Docker image using a Dockerfile that starts with a "scratch" image and copies it in the WASM binary as the entrypoint.

FROM scratch
COPY /helloworld.wasm /helloworld.wasm
ENTRYPOINT ["helloworld.wasm"]

When building the Docker image, we specify a platform flag to indicate that this is a container for WASM.

docker buildx build -platform wasi/wasm32 -t kodekloud/wasm-docker-hello-1 .

After building the image, we can run the WASM container using the "docker run" command, specifying the runtime as "io.containerd.wasmedge.v1" and the platform as "wasi/wasm32".

docker run -rm -name=wasm-docker-c-program -runtime=io.containerd.wasmedge.v1 -platform=wasi/wasm32 kodekloud/wasm-docker-hello-1

Looking ahead, the integration of WASM and WASI with containerization and orchestration technologies such as Docker and Kubernetes has the potential to revolutionize application development and deployment. While it's possible that WASM may be used as an alternative to some use cases currently handled by containerization, it's unlikely to replace these technologies entirely.

Want to get started with WASM, check out our Exploring WebAssembly Course:

Exploring WebAssembly (WASM) Course | KodeKloud

Conclusion

In conclusion, Docker and WebAssembly (WASM) are two powerful technologies that offer developers an array of benefits for application development and deployment.

Docker provides a consistent and reliable method to package and distribute applications in containerized environments, while WebAssembly delivers a fast and secure execution environment for running applications in web browsers.

By combining these two technologies, developers can create efficient and scalable web applications that are easy to manage and deploy.

As the use of containers and web based applications continues to rise, the integration of Docker and WebAssembly offers a promsing future for the development of innovative and efficient web applications.

Please share your suggestions and feedback for the blog by leaving a comment. Your input is valuable and will help us improve our content. Thank you!


People are also reading: