Module 11 · Lesson 83 of 107

Choosing between VMs and containers

Choose between a container and a VM by evaluating kernel requirements, isolation, and resource costs, then measure both with the same workload.

  • Beginner to advanced
  • 8 min read
  • 45 min lab
What you will need

An Ubuntu 24.04 machine with Docker and a local Ubuntu 24.04 VM that can be powered off and started on the same machine. Use Ubuntu directly or in a VM with nested virtualization configured. WSL2 can be used for the container measurements.

Your place in the module
By the end you can 4 objectives
  • Compare containers and VMs across isolation, startup time, density, image size, and kernel flexibility.
  • Distinguish the isolation models used by Kata Containers, Firecracker, and gVisor.
  • Measure startup to readiness and host memory use for the same service in a container and a VM.
  • Interpret kernel release strings and process visibility from host, container, and guest observations.
Video coming soonChoosing between VMs and containers
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Container networking and rootless mode
  • A container's network is a network namespace joined to the host by a veth pair plugged into a Linux bridge, with NAT for outbound traffic
  • CNI plugins are ordinary executables called with CNI_* environment variables and JSON on stdin; runc itself does no network setup
  • A user namespace maps container UID 0 to an unprivileged host UID, so a rootless container escape lands as your user, not root
01

Decide which kernel the workload needs

4 min read

The last lesson completed the container mechanics. This lesson answers the decision matrix, and the middle ground of Kata, Firecracker and gVisor.

Suppose an embedded team needs to test a service with the same libraries as its target image, then check whether the service behaves differently under a candidate kernel. These are separate requirements. The first can be met by packaging the service and its libraries in a Linux container. The second requires booting the candidate kernel in a VM or on a board. The Linux uname(2) manual describes the release field as information about the running kernel. Changing the distribution in a container image can change the libraries without changing the kernel release reported by uname -r.

Container or VMLinuxcontainerVMIsolationShared kernelSeparatekernelStartup timeStartsapplicationBoots guestfirstDensityUsuallyhigherGuest memoryoverheadImage sizeApplicationand userspaceBootableguest systemKernel choiceHost LinuxkernelCompatibleguest kernel
Five criteria for choosing a Linux container or a VM

Isolation strength. An ordinary Linux container shares the host kernel. The kernel applies the container's process visibility, permissions, and resource limits, and it also handles the container's system calls. A VM boots a separate guest kernel. With hardware virtualization, the hypervisor and CPU enforce a boundary around guest memory and privileged execution. The guest kernel can fail and take down its own services while the host and other guests continue. If the workload comes from a separate trust domain and policy requires a hardware-backed boundary, use a VM or a sandbox that provides one. The choice depends on the actual configuration, including the host resources you expose to the guest.

Startup time. With the image already local, a container runtime can set up the environment and start the application without booting another kernel. A VM starting from a powered-off state normally boots its kernel, initializes its guest environment, and then starts the application. Containers therefore usually reach readiness sooner for small services. Measure readiness using a successful application request. A running container, a powered-on VM, and a service that can answer a request are three distinct observations. The application's own initialization can dominate the result in either environment.

Density. Density is the number of workload instances a host can run within its resource and service limits. Multiple containers can share the same running kernel, and containers from the same image can reuse read-only image layers. Separate VMs typically add guest kernel memory, guest services, and virtual machine monitor overhead for each VM. On a memory-constrained gateway, that difference can matter for a collection of small services. For a large workload, application memory or CPU demand may determine the limit in both cases. Set a service target, such as a response-time budget, before drawing a conclusion about how many instances fit.

Image size. A container image carries the application and its required user-space files. A conventional VM disk image typically also contains a bootable guest system, including its kernel and any installed system services. This generally favors containers when distributing small applications to embedded devices. The packaging choice still matters. A container image full of build tools can be larger than a minimal guest image. When comparing sizes, distinguish download bytes from storage used on disk and count shared layers only once when estimating storage for multiple locally cached images.

Guest-kernel flexibility. A VM can boot a different Linux kernel and, given a supported CPU architecture and virtual hardware, can boot Windows or a compatible real-time operating system (RTOS). An ordinary Linux container cannot boot a Windows or RTOS kernel. An Ubuntu 24.04 container can supply Ubuntu user space, but the kernel features it uses must be present on the host. Running an RTOS in a VM provides a way to run its kernel. Meeting a real-time deadline remains a property of the configured system and needs separate verification.

For a trusted telemetry collector on a gateway, start with the container choice if the host supports the collector's kernel interfaces and the required device access. The same choice fits a test that varies libraries or application dependencies. If the test varies the kernel itself, use a VM with the required kernel and virtual devices. If the test needs the gateway's actual device behavior, the board may be necessary.

02

Keep the container workflow with a different isolation boundary

2 min read

An image and the commands used to deploy it describe how operators package and run a workload. The execution environment determines what handles its system calls and where its isolation boundary lies. That distinction matters when a team wants to keep its container images and deployment workflow but needs stronger isolation than ordinary shared-kernel execution provides.

Kata Containers integrates with container tooling and runs workloads inside a VM. With this setup the workflow can still start from an OCI image, while the application runs against a guest Linux kernel. A Kata sandbox can contain multiple containers from the same pod, so check the unit that gets its own VM. Containers placed in the same sandbox share that sandbox's guest kernel. This matters when the requirement is to isolate workloads from each other as well as from the host.

Firecracker is a virtual machine monitor for small VMs called microVMs. A platform can use it to run container workloads inside microVMs and preserve a container-style deployment workflow. The platform handles the container image and starts its workload in the guest. Firecracker supplies the VM boundary. In both the Kata and Firecracker arrangements described here, the separate guest kernel and hardware-backed virtualization provide VM-grade isolation at the configured VM boundary.

gVisor takes a different approach. Its user-space kernel implements Linux interfaces and handles application system calls inside a sandbox. This reduces the workload's direct exposure to host kernel interfaces. The application still needs the interfaces it uses to be supported by the sandbox, so test both compatibility and performance with the actual workload. The presence of a user-space kernel describes a different isolation design from the VM with a booted guest Linux kernel used by Kata.

Kernel and isolation modelsOrdinarycontainerKata orFirecrackergVisorApplicationpackagingContainerimageContainerimageContainerimageSystem callhandlerHost LinuxkernelGuest LinuxkernelUser-spacekernelIsolationboundaryHost kernelcontrolsVirtualmachinegVisorsandbox
The kernel and isolation boundary used by each execution model

When a requirement calls for a separate guest kernel, first identify which workloads get their own VM. When the goal is to reduce exposure to host kernel interfaces while keeping container tooling, gVisor is another option to evaluate. For any of these approaches, measure the startup and memory costs of the configured platform. A familiar image format and deployment command can conceal substantial differences in what starts beneath the application.

03

Measure the same service in both environments

3 min read

Use a small HTTP service so you can observe the moment it becomes usable. On the Ubuntu 24.04 comparison host and guest, use the same Python version where practical, and serve identical index.html files from the container and guest working directories. The service command in each environment is:

bash
python3 -m http.server 8000 --bind 0.0.0.0

Use a private lab network. Publish the container's port to the comparison host and make the guest's port reachable from that same host. Configure the guest to start the service automatically at boot and verify both URLs before recording trials. Keep the container image local and the guest disk prepared. Use a stopped container and a powered-off guest for the timed trials. This measures startup with local artifacts and includes guest boot. Record your configuration so another learner can tell what the measurement includes.

Measure startup to readinessStart timerStartenvironmentPoll HTTPserviceVerifyresponseRecordelapsedtimeStart timerStartenvironmentPoll HTTPserviceVerifyresponseRecordelapsedtime
Use the same application response to mark readiness in both trials

For each trial, start a timer on the comparison host immediately before issuing the start command. Poll the service from that host with curl -fsS --max-time 1 "$url" at the same interval in both cases, and stop timing at the first successful response containing your file's expected text. Pick a polling interval short enough for the difference you want to resolve. Use an overall deadline so a failed run has a clear outcome. Repeat each configuration at least three times, record the individual times, and compare medians. Keep the runtime daemon running throughout and avoid unrelated load between trials.

For a host-side memory estimate, read MemAvailable in /proc/meminfo on the comparison host just before startup and again after readiness, once the service settles. Subtract the second value from the first and record the difference in the reported units. Apply the same settling rule and request workload to both runs. MemAvailable estimates how much memory the host can make available to new applications without swapping. Its change can reflect caching and background activity, so record variation across trials. A VM's configured RAM and the RSS of the HTTP server inside it answer different questions. The host measurement gives you a common comparison that includes the environment around the service.

Finally, leave the service running and start a harmless marker process, sleep 300, on the comparison host. Record its host PID. Run the following on the host, inside the running container, and inside the guest:

bash
uname -r
ps -eo pid,comm

The process listing reads the local /proc view. With a private container PID namespace and its matching /proc mount, the host marker is visible on the host and absent from the container listing. The guest has its own kernel and process table, so its listing also lacks the host marker. A different sleep or a coincidentally identical PID can appear in another environment. Use an otherwise quiet lab and compare each listing before and after launching the marker.

The ordinary container reports the host kernel release. The guest reports the release of its own kernel, which can be identical if the host and guest boot the same kernel build. Matching release strings alone cannot establish kernel sharing. A limited process view alone cannot establish that a VM is involved. Record both observations alongside the runtime and VM configuration, then state which setup fits the workload and what the measurements contribute to that choice.

The next lesson, Firecracker and the microVM idea, asks what a VM can omit when its job is to run a narrowly defined workload. The startup and memory measurements here give you two criteria for evaluating that design.

Key takeaways

  • An ordinary Linux container shares the host kernel, while a VM boots a separate guest kernel.
  • Containers usually start sooner and support higher density when the workloads fit the same host kernel.
  • Kata and Firecracker can put container workloads behind a VM boundary, while gVisor uses a user-space kernel.
  • Kernel release strings and process listings reveal different properties of an execution environment.
Reflect
  1. Explain to a colleague, in two sentences: compare containers and VMs across isolation, startup time, density, image size, and kernel flexibility.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close