Sub topic 14
Emulators and Containers Compared with Virtual Machines
This chapter assumes you know what a host, a guest, and a hypervisor are. It also assumes you know that a virtual machine (VM) runs its own full operating system (OS). Here you will compare VMs with two similar technologies, emulators and containers. You will also learn how to pick the right one in an exam scenario.
The Working Model
All three technologies let one computer run software that was not built for its own setup. They differ in what they copy and what they share.
- A virtual machine uses a hypervisor to give each guest virtual hardware. Each guest has its own full OS and kernel. The guest runs on the host CPU, so it must use the same CPU architecture as the host.
- An emulator is software that imitates different hardware, so software written for another platform can run. Every instruction from the guest is translated for the host CPU. This lets an x86 PC run an Android phone app or an old game console program. The cost is speed, because translation is slow.
- A container packages an application with its libraries and settings. It runs on the host's own OS kernel, so it has no guest OS of its own. Containers start in seconds and use little memory. They can only run software built for the host's kernel type, such as Linux containers on a Linux kernel.
Side-by-Side Comparison
| Feature | Virtual machine | Emulator | Container |
|---|---|---|---|
| Has its own guest OS | Yes | Usually yes | No, shares host kernel |
| CPU architecture | Same as host | Can differ from host | Same as host |
| Speed | Near native | Slow | Near native |
| Resource use | High | High | Low |
| Isolation | Strong | Strong, but varies by product | Weaker, shares kernel |
| Start time | Minutes | Minutes | Seconds |
Choosing the Right Tool
Match the need to the technology.
- You need a full second OS, such as Linux on a Windows PC. Use a VM.
- You need to test untrusted software without risk. Use a VM or a sandbox. A sandbox is an isolated area where code cannot change the rest of the system.
- You need to run software built for different hardware, such as a phone app or old console game. Use an emulator.
- You need to run many copies of a small app quickly and consistently. Use containers.
Common Errors
- Treating an emulator as a VM. A VM cannot run software for a different CPU architecture. Only an emulator can translate between them.
- Assuming containers have full isolation. All containers share one kernel. A flaw in that kernel can affect every container. A VM is the safer choice for hostile code.
- Expecting a container to run any OS. A Linux container cannot run a Windows OS, because it uses the host kernel.
- Forgetting that emulators are slow. If the scenario stresses performance, an emulator is unlikely to be the answer.
Exam-Style Practice
Exercise
A developer must run an application written for a different CPU architecture than her laptop uses. Should she use a VM, an emulator, or a container? Why?
Show answer
An emulator. A VM and a container both run on the host CPU architecture. An emulator translates instructions for the foreign architecture.
Exercise
A team wants to start dozens of identical small web services quickly while using as little memory as possible. All services run on Linux. Which technology fits best, and what is the main isolation drawback?
Show answer
Containers. They share the host kernel, so they start fast and use little memory. The drawback is weaker isolation, because a kernel flaw can affect all containers.