A container is not a virtual machine
Why a container starts in a fraction of a second and what it shares with the host.
A virtual machine emulates a whole computer: it has its own virtual processor, its own memory and its own operating system kernel. That is why it takes tens of seconds to boot and occupies gigabytes. A container works differently: it is a process on the host that the kernel shows a restricted world. All containers share one kernel — the host's.
Isolation comes from two Linux kernel mechanisms. Namespaces hide other processes, files, network interfaces and the machine name from a process: inside a container the process sees itself as number 1 and does not see its neighbours. Control groups (cgroups) limit resources: how much memory and CPU time a container may take. Neither mechanism starts a second kernel, so starting a container is barely different from starting an ordinary program.
This has consequences worth remembering. Linux containers need a Linux kernel. On Windows and macOS, Docker Desktop quietly runs a small Linux virtual machine, and the containers live inside it. That is why paths to host files and networking are a little more complicated on these systems than on a Linux server.
Resource limits are set at start:
docker run --rm --memory=256m --cpus=0.5 alpine:3.20 echo ready
# ready
If the process in such a container tries to take more than 256 MB, the kernel terminates it and Docker reports exit code 137.
A common mistake is to treat container isolation as being as strong as a virtual machine's. A shared kernel means that a kernel vulnerability affects all containers at once. That is why untrusted code is run with extra restrictions: without privileges, as an ordinary user, sometimes in a dedicated sandbox. For your own applications a container is usually enough, but it is worth knowing where the boundary lies.
Practice
Whose kernel
Which operating system kernel does a container use on a Linux server?
- Its own kernel stored in the image
- The host's kernel
- The kernel of Docker's virtual machine on every system
- No kernel is needed: a container runs without one
Isolation mechanisms
Which Linux kernel mechanisms give a container isolation and limits? Mark all correct answers.
- Namespaces
- Control groups (cgroups)
- Hardware CPU virtualisation
- A separate kernel for each container
Exit code on running out of memory
A container started with
--memory=256mwent over the limit and the kernel terminated the process. Which exit code will Docker show?