Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Containers or virtual machines: which one to choose?

By updated

Improve this page

A container is not a small machine. It is a process of the host, that is, of the machine it runs on. The Linux kernel, the part of the system that manages the hardware and the programs, shows it only a part of the system. All the containers of a machine share that same kernel.

Three things follow. A container starts like a program. It moves from one server to another with its libraries. And it does not replace a virtual machine in every case.

This lesson opens the Docker course and requires no installation: a few commands on a Linux machine are enough to see what the kernel already does with any process. You then understand what a container is, how it differs from a virtual machine, and how to choose between the two.

What you will learn

  • Define a container as an isolated process, and not as a machine
  • Read a process's namespaces in /proc
  • See that all the processes of a machine share a single kernel
  • Explain what the portability of an image guarantees, and its limits
  • Justify the choice between a container and a virtual machine on three needs

Prerequisites

You must know what a process is on Linux: a running program, identified by a number called a PID. You need a terminal on a Linux machine, for example a Debian 13 virtual machine, and an account that can use sudo.

The commands come from the base installation: ps, ls, uname and lsns. The outputs of this lesson were tested on Debian 13 (kernel 6.12.118, util-linux 2.41.5), and the answers to the exercise assume a Debian 13: an Ubuntu follows the lesson, but may give other answers to the exercise, because its list of namespaces differs. Your process numbers, your namespace identifiers and the number of processes will be different: only the shape of the output matters.

What is a container?

A container is a process started on the host. The Linux kernel shows it only a part of the system. It carries neither a kernel nor simulated hardware: it uses those of the machine that runs it. The program in the container sees its own file system, that of an image, which is an archive containing the program and its libraries. It also sees its own process numbers and its own network interface. For the host, it remains an ordinary process.

Two kernel mechanisms create this restricted view. Do not confuse them. Namespaces limit what the process sees. The namespaces(7) page of the Linux manual describes them like this: a group of processes has the impression of having its own version of a system resource, such as process numbers or the network. Cgroups (control groups) limit what the process consumes, for example memory, processor time or the number of processes. They are described in cgroups(7).

This difference helps you diagnose. If a container sees the processes of the host, the problem comes from a namespace. If it is killed because it uses too much memory, a cgroup limit was reached.

Docker, which the course installs at level 2, does not create these mechanisms: it assembles them and makes them simple to use. In the next lesson you build a container by hand with the kernel's own tools. To explore namespaces in more detail, the page Understanding Linux namespaces creates and inspects them. Cgroups and the namespaces as Docker uses them are covered in Measure a container's isolation, at level 4. This lesson keeps only what you need to read a process's namespaces and to choose between a container and a virtual machine.

How does a container differ from a virtual machine?

A virtual machine (VM) simulates an entire machine. A hypervisor, the software that shares the hardware between several simulated machines, runs for each of them a complete system with its own kernel. A container skips that step: it starts its program directly on the kernel of the host.

The animated diagram shows the two stacks, layer by layer, from the server to the applications. Move step by step with the arrows, or start playback: a voice explains each step. Look at what sits under the libraries. On the left, each virtual machine has its own guest kernel, placed on the hypervisor. On the right, the three containers share a single kernel, that of the host system. Docker Engine, on the right-hand stack, is the program that runs containers; you will install it at level 2.

The same comparison is available as a short video, with one audio track in English and one in French.

The table compares the two on four criteria.

CriterionContainerVirtual machine
What runsa process of the hosta complete system with its kernel
Kernel usedthat of the host, shareda kernel of its own for each machine
Isolation boundarykernel mechanisms (namespaces, cgroups)the hypervisor and the simulated hardware
Idle costthat of a processthat of a running system

The last row shows up at startup: launching a container amounts to launching a program, whereas a VM must first boot a system. The row before it works the other way: two VMs are better separated from each other, because each has its own kernel and goes through the hypervisor.

Which namespaces does an ordinary process have?

Every Linux process already belongs to namespaces: one per type of resource (processes, network, mount points, host name…). A container is a process to which the kernel gives new namespaces, in place of those of the machine.

To see this, you first need a process. sleep 600 waits ten minutes doing nothing, and & runs it in the background. $! holds the PID of the last process started this way, and ps -o chooses the columns to display for the process given by -p.

Terminal window
sleep 600 &
ps -o pid,ppid,user,cmd -p $!
[1] 2288
PID PPID USER CMD
2288 2281 ansible sleep 600

ansible is the name of the account used for the tests of this lesson: you will see your own.

The line [1] 2288 gives the shell's job number (1), then the PID of the process (2288). Read the PPID column: it is the number of the parent, here the shell that started it. For the kernel, this process is nothing special. Its /proc/<PID>/ns directory lists the namespaces it belongs to.

Terminal window
ls -l /proc/$!/ns
total 0
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 ipc -> 'ipc:[4026531839]'
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 mnt -> 'mnt:[4026531841]'
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 net -> 'net:[4026531840]'
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 pid -> 'pid:[4026531836]'
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 pid_for_children -> 'pid:[4026531836]'
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 time -> 'time:[4026531834]'
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 time_for_children -> 'time:[4026531834]'
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 user -> 'user:[4026531837]'
lrwxrwxrwx 1 ansible ansible 0 Nov 26 06:43 uts -> 'uts:[4026531838]'

Each line is a link whose target carries the namespace type and a numeric identifier in square brackets. There are eight types: cgroup, ipc, mnt, net, pid, time, user and uts. The two lines pid_for_children and time_for_children give the namespaces that child processes will receive. Here they are identical to those of the process itself.

Two processes that show the same identifier for a type share that namespace. For the pid type, they see the same processes; for net, the same network interface; for mnt, the same mount points; for uts, the same host name.

Now compare two processes: process 1, the first program started at boot (/sbin/init), and the ls command itself, which /proc/self designates. Process 1 belongs to root: without sudo, reading its link is refused.

Terminal window
sudo ls -l /proc/self/ns/pid /proc/1/ns/pid
lrwxrwxrwx 1 root root 0 Nov 26 06:43 /proc/1/ns/pid -> 'pid:[4026531836]'
lrwxrwxrwx 1 root root 0 Nov 26 06:43 /proc/self/ns/pid -> 'pid:[4026531836]'

The two identifiers are identical, and it is also the one of sleep above: these three processes are in the same pid namespace, so they see the same list of processes. lsns groups processes by namespace and confirms it for the whole system.

Terminal window
sudo lsns -t pid
NS TYPE NPROCS PID USER COMMAND
4026531836 pid 131 1 root /sbin/init

A single line: one pid namespace, 131 processes in it (your number will be different), the first one being /sbin/init. As long as no container is running, all processes share this same pid namespace. Without sudo, the lsns list is incomplete: it reads only the processes you are allowed to inspect, in practice your own. The sharing of the kernel is not read in the namespaces: the next section shows it with uname.

A container changes precisely these identifiers. Its first process receives a new pid namespace and sees itself as number 1, while the host still sees it as an ordinary process, under another number. The next lesson shows this.

Is the kernel really shared?

Yes. Namespaces do not cut up the kernel: they only change what a process sees. uname -srm prints the kernel name, its version and the processor architecture.

Terminal window
uname -srm
Linux 6.12.118+deb13-amd64 x86_64

uname queries the kernel that runs the command, and there is only one for all the processes of the machine. A container started on this machine would see that same kernel, even if its image is that of another distribution, Alpine for example: the name of the distribution only designates the files of the image, that is, the libraries and the tools. The next lesson checks this with an Alpine rootfs. A virtual machine, for its part, boots its own kernel. The lesson on container images shows how to read the distribution of an image.

Stop the test process, then check that it is gone.

Terminal window
kill %1
ps -p <PID>
PID TTY TIME CMD
[1]+ Terminated sleep 600

Replace <PID> with the number you noted at the start. Only the header remains: the process no longer exists. The shell reports the end of the job with the line Terminated, sometimes only after the next command. kill %1 designates the first process started in the background in this terminal; in another terminal, pass the PID to kill.

Find the namespaces of your machine Exercise 1 of 1 · ~6 min · terminal

You have read the namespaces of a process started on its own and checked that the pid namespace is unique on your machine. Now look at two other types, mnt (mount points) and uts (host name). No container must be running on the machine. The expected answers are those of a Debian 13.

List the mnt namespaces

Use sudo to display the namespaces of type mnt. Find the one with the most processes, then ask yourself whether one mnt namespace is shared by all processes.

Compare the mnt namespace of systemd-udevd with that of process 1

Find the systemd-udevd line in the COMMAND column and note its PID. Then, with readlink and sudo, compare its /proc/<PID>/ns/mnt link with that of process 1.

Look for the same program among the uts namespaces

Do the same reading for the uts type and look for systemd-udevd in the list.

A hint 1 / 2

lsns -t chooses the namespace type. Without sudo, the list is incomplete.

A hint 2 / 2

The PID of the column is used to build the path /proc/<PID>/ns/mnt. readlink prints the target of a symbolic link.

I have tried: show the solution
Terminal window
sudo lsns -t mnt
sudo readlink /proc/1/ns/mnt /proc/<PID>/ns/mnt
sudo lsns -t uts
NS TYPE NPROCS PID USER COMMAND
4026531841 mnt 124 1 root /sbin/init
4026532272 mnt 1 394 systemd-resolve ├─/usr/lib/systemd/systemd-resolve
4026532273 mnt 1 407 root ├─/usr/lib/systemd/systemd-udevd
4026532276 mnt 1 395 systemd-timesync ├─/usr/lib/systemd/systemd-timesyn
4026532287 mnt 1 698 systemd-network ├─/usr/lib/systemd/systemd-network
4026532345 mnt 1 814 polkitd ├─/usr/lib/polkit-1/polkitd --no-d
4026532350 mnt 1 780 root └─/usr/lib/systemd/systemd-logind
4026531862 mnt 1 34 root kdevtmpfs
mnt:[4026531841]
mnt:[4026532273]
NS TYPE NPROCS PID USER COMMAND
4026531838 uts 127 1 root /sbin/init
4026532274 uts 1 395 root ├─/usr/lib/systemd/systemd-timesyncd
4026532275 uts 1 407 root ├─/usr/lib/systemd/systemd-udevd
4026532346 uts 1 814 polkitd ├─/usr/lib/polkit-1/polkitd --no-debug --log-level=notice
4026532350 uts 1 780 root └─/usr/lib/systemd/systemd-logind

This output comes from a fresh Debian 13. Namespace numbers and PIDs change from one machine to another, and the list depends on the services the machine runs.

The largest mnt namespace is the one of the process with PID 1, shown here as /sbin/init: it is the namespace common to almost all processes. The text of the COMMAND column changes with the distribution, but the PID of that line is 1. systemd-udevd has its own, so the two readlink links carry different identifiers (here 4026531841 and 4026532273), and it also has a uts namespace of its own. This is not a container: systemd gave it these namespaces because its unit on Debian 13 declares PrivateMounts=yes (mnt namespace) and ProtectHostname=yes (uts namespace). A namespace is therefore not reserved for containers: several system services of Debian 13 live in their own.

What does image portability guarantee, and where does it stop?

An image carries the libraries and files the program needs. It thus avoids failures of the "it works on my machine, but a library is missing on the server" kind. The same program, with the same files, starts the same way on a development workstation, on a test server and on a production server. The host only has to provide what the image does not contain.

The image does not contain the kernel. The container uses that of the host and cannot ask for another one. A program that needs a feature or a module missing from that kernel therefore fails, whatever the image. In the same way, a kernel flaw affects every container of the host at once.

The image also depends on the processor architecture. An image built for amd64 (Intel and AMD processors) does not run as is on an arm64 host, and the other way round.

VM or container: how to choose?

The right choice depends on your need. Three common needs lead to three answers.

First need: delivering the same application, reproducibly, from the developer's workstation to production. Choose the container. The image embeds the libraries in a precise version, and launching the container amounts to starting a process. You can also run side by side applications whose dependencies are incompatible.

Second need: running another system or another kernel than that of the host, or getting stronger isolation between workloads that do not trust each other. Choose the virtual machine. It carries its own kernel, and the hypervisor separates it from the others. A container cannot provide another kernel.

Third need: running many small isolated services on the same machine, giving each a share of memory and processor. Choose the container. At idle it costs a process, and the cgroups set its limits. With one VM per service, you would have as many complete systems to maintain.

You can also combine the two: containers often run inside virtual machines, for example on the servers rented from a cloud provider. To decide, ask yourself which separation you want between your workloads: that of the kernel, with containers, or that of the hypervisor, with virtual machines.

Troubleshooting

SymptomCauseFix
ls: cannot read symbolic link '/proc/1/ns/pid': Permission deniedProcess 1 belongs to rootAdd sudo in front of ls
lsns -t pid shows only a handful of processesWithout sudo, lsns reads only the processes you can inspect, in practice your ownRun it again with sudo
ls -l /proc/$!/ns looks for the directory /proc//ns$! is empty: no process was started with & in this terminalRun sleep 600 & again in this terminal, or give the PID by hand
-bash: kill: %1: no such jobThe job was started in another terminalUse kill <PID> with the number you noted

Key points

  1. A container is a process of the host that the kernel isolates. It is not a machine that boots a system.
  2. Namespaces limit what the process sees, cgroups limit what it consumes.
  3. Every process already belongs to namespaces, which ls -l /proc/<PID>/ns lists. A container is a process that receives new ones: it sees itself as number 1, and the host sees it as an ordinary process, under another number.
  4. All the processes of a machine, containers included, share a single kernel, which uname -srm prints. A virtual machine boots its own.
  5. An image carries the libraries and files of the program, but not the kernel. It is tied to a processor architecture.
  6. Because the kernel is shared, the container is light. For the same reason, it does not choose its kernel, and a kernel flaw affects them all.
  7. Choose the container to deliver reproducibly and to run small isolated services.
  8. Choose the virtual machine when you need another kernel or stronger isolation. You can also combine the two.

Knowledge check

Check that the essentials of this guide stuck. The questions cover only what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer