Containers versus virtual machines
What is a container?
A container is an isolated environment for running one or more application processes. It packages an application with the files and libraries it needs, while ordinary Linux containers use the kernel of their Linux host. A virtual machine instead presents virtual hardware to a separate guest operating system.
Imagine our bookstore needs two copies of its catalog service. The team prepares one container image containing the service and its runtime. It starts two containers from the same image, each with its own process state. Both can handle requests, but neither is a second physical computer. This is a conceptual example; no software installation is needed.
Image, container and registry
An image is a packaged, read-only starting point for creating containers. A container is a running or stopped instance created from an image with runtime settings such as memory limits, environment configuration and storage mounts. A registry is a service that stores and distributes images. Docker provides tools for building, storing and running these artifacts; it is not the only container technology.
Suppose the catalog image contains version 1 of the application. Container A and container B start from it. A change to a file in A's writable layer does not rewrite the image or automatically change B. When the team builds version 2, it can create new containers from that new artifact. A tag such as v2 is a name that can be reassigned; a digest identifies specific content. Pinning a digest helps identify what was deployed, but does not prove that the contents are secure or compatible.
How Linux isolates processes
A kernel manages processes, memory and devices. Linux namespaces can give processes different views of resources such as process IDs, mount points and network interfaces. Control groups (cgroups) organize processes and support accounting and resource limits. A runtime uses these facilities alongside filesystem permissions and other security controls. Limits must actually be configured; a container is not automatically constrained to a sensible memory budget.
Conceptual stack (ordinary Linux containers, not a universal runtime architecture):
VM A VM BApplication A Application BGuest OS A Guest OS B Hypervisor Hardware
Container A Container BApplication + libs Application + libs Host Linux kernel Hardware or virtual machineText alternative: in the first stack each application has a guest OS above a hypervisor. In the second stack both applications use the host kernel, with runtime-enforced isolation. In many desktop environments Linux containers run inside a Linux VM. A container image does not carry an independent running Linux kernel. CPU architecture, OS and runtime support still matter.
Choose a VM or container from requirements
A VM is useful when an application needs its own kernel, a different supported guest OS or a stronger separate-kernel isolation boundary. Containers can be convenient when several compatible applications need repeatable packaging and frequent replacement. Both approaches require patching, access controls and resource monitoring.
| Requirement | Candidate | Reason |
|---|---|---|
| Legacy app requires a supported Windows guest on a Linux host | VM | Separate guest OS |
| Two compatible Linux web services, same platform | Containers | Reusable application artifacts |
| Untrusted tenant code | Dedicated security design | A standard container alone is not sufficient evidence of safe isolation |
| Existing small app runs reliably on one VM | Keep VM if appropriate | Migration has costs |
A container often has less startup work than booting a full guest OS, but actual time and resource usage depend on image availability, startup tasks, workload and host. Containers and VMs are not mutually exclusive: running containers inside cloud VMs is common.
Worked example: a small capacity plan
Assume a Linux host has 8 GiB of RAM and the operator reserves 2 GiB for its OS and other overhead. A catalog container has a planned memory budget of 1.5 GiB and a search container has 1 GiB. Three catalog copies plus one search copy need 3 × 1.5 + 1 = 5.5 GiB. The application budget is 8 - 2 = 6 GiB, leaving 0.5 GiB. Four catalog copies plus search would need 7 GiB, which exceeds the budget by 1 GiB.
These are fictional planning numbers, not measurements or recommended limits. This simple no-overcommit policy ignores CPU, disk, network and actual usage peaks. Fitting the memory budget does not guarantee that the host can meet response-time targets. A real plan also accounts for runtime overhead and verifies load behavior.
Where does application data live?
A running container commonly has a writable layer on top of its image. Stopping and restarting the same container is different from deleting it and starting a replacement. When a container is removed, files kept only in that writable layer are lost. Data held in an explicitly retained named volume can survive container replacement, provided the volume is retained and mounted again. A separate database can also keep records outside the application instance. Neither a volume nor an external database automatically provides a backup.
For the bookstore, catalog code belongs in the image, temporary search caches can be rebuilt and customer orders need durable storage with tested recovery. Do not mount broad host paths or share a privileged runtime socket to make access easier. Keep secrets out of images and use a restricted runtime identity.
Practice and recap
For a host with 12 GiB RAM, reserve 3 GiB. Each web container needs a 2 GiB planning budget and a helper needs 1 GiB. Predict the maximum whole number of web copies under this policy. Answer: four, because 4 × 2 + 1 = 9 GiB. Five would need 11 GiB. Next, explain why an order file in a removed container's writable layer cannot be recovered simply by launching the same image.
Remember: an image is an artifact, a container is an instance, and a VM has separate virtual hardware and guest OS. Choose based on requirements rather than assuming containers always win. The next lesson explains how an application can be designed to tolerate replacement. The Docker and Kubernetes tracks can provide tool-specific practice once their lesson identities are verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Containers versus virtual machines — knowledge check
Choose the best answer for each offline scenario.
Which kernel does an ordinary Linux container use?
What happens to the image if one container writes to its own writable layer?
Which Linux mechanism can account for and limit grouped process resources?
A host has 8 GiB RAM, 2 GiB reserved, and four services require 7 GiB total. What is the result under the no-overcommit policy?
Which design keeps order records when a web container is removed?