Lesson 1 of 8 in Containers with Docker
What a container actually is
A container is a normal process on the host that the Linux kernel isolates with namespaces and limits with cgroups. It shares the host's kernel, so it starts in moments and weighs far less than a virtual machine.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: Linux and the shell, Networking for DevOps
The problem this solves
Software depends on its surroundings: the language version, system libraries, config files and even the user it runs as. An app that works on your laptop fails on a server because one of those differs, and two apps on one machine can demand conflicting versions of the same library.
A virtual machine solves this by giving each app a whole computer with its own operating system. That works, but each one carries a full OS, takes a while to boot and reserves memory up front.
Containers aim at the same isolation with far less weight. Instead of pretending to be a separate computer, a container is just a process that the kernel has been told to restrict.
How it works
- Runs code
- Security
- Control
A container is an ordinary process; namespaces limit what it can see, cgroups limit how much it can use, and the kernel underneath is the host's own.
- Container to Namespaces: 1 sees its own PIDs, net
- Namespaces to Host kernel: 2 enforced by
- Container to cgroups: 3 capped by
- cgroups to Host kernel: 4 enforced by
- Runs code
- Control
A container runs on the host's shared kernel, while a virtual machine runs its own guest kernel on top of a hypervisor.
- App to Container runtime: runs on
- Container runtime to Host kernel: uses
- App to Guest OS + kernel: runs on
- Guest OS + kernel to Hypervisor: runs on
Linux namespaces give a process its own view of one kind of resource. With a PID namespace the process sees itself as process 1 and cannot see other processes. With a network namespace it gets its own network interfaces. There are namespaces for mounts, hostnames and users too.
Control groups, or cgroups, put a ceiling on what a group of processes may consume: memory, CPU time, number of processes. A container that exceeds its memory limit is killed by the kernel, not by Docker.
Docker is the tool that sets all of this up for you, plus the packaging around it. The consequence of sharing one kernel is that a Linux container needs a Linux kernel. On macOS and Windows, Docker Desktop quietly runs a small Linux VM and your containers run inside that.
Try it yourself
You need Docker installed (Docker Desktop, or Docker Engine on Linux). These commands pull small public images and clean up after themselves.
- 1Check that Docker works
docker version docker run --rm hello-world
hello-worldprints a short message starting withHello from Docker!.--rmremoves the container when it exits. - 2See the container's own view of processes
docker run --rm alpine ps
You only see the
pscommand itself, as process 1. The container cannot see your host's processes because it has its own PID namespace. - 3Show that the kernel is the host's
uname -r docker run --rm alpine uname -r
On Linux both print the same kernel version, because there is no second kernel. On macOS or Windows the first line is your own system and the second is the Linux kernel inside Docker Desktop's VM, so they differ.
- 4Apply a memory limit and read it back
docker run --rm --memory 64m alpine cat /sys/fs/cgroup/memory.max
On a host using cgroup v2 this prints the limit in bytes, which is 64 times 1024 times 1024. This is the cgroup the kernel enforces.
What breaks, and where to look
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
- WHY IT HAPPENS
- The
dockercommand is only a client. The Docker daemon that does the work is not running, or the client points at the wrong place. - CHECK FIRST
- Start Docker Desktop, or on Linux run
systemctl status docker. Then retrydocker version, which should show both a Client and a Server section.
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
- WHY IT HAPPENS
- On Linux the daemon socket is owned by root and the
dockergroup. Your user is in neither. - CHECK FIRST
- Run
idand look fordockerin the groups. Adding yourself to that group gives root-equivalent power on the machine, so decide that deliberately, and log in again afterwards.
A warning that the image platform (linux/amd64) does not match the host (linux/arm64), or a later
exec format error.- WHY IT HAPPENS
- The image was built for a different CPU architecture than your machine, for example an Intel-only image on an Apple silicon Mac.
- CHECK FIRST
- Run
docker image inspect <image> --format '{{.Architecture}}'and compare withuname -m. Look for a multi-architecture tag, or pass--platformon purpose.
When not to use this
- A container shares the host kernel, so it is a weaker isolation boundary than a virtual machine. For running untrusted code from strangers, add stronger isolation instead of relying on containers alone.
- An app that needs a different operating system kernel than the host, such as a Windows app on a Linux host, cannot use a plain Linux container.
Check yourself
What two kernel features make a process behave like a container?
Namespaces limit what the process can see, and cgroups limit how much CPU, memory and other resources it can use.
Why does a container start faster than a virtual machine?
It does not boot a guest operating system. It starts one process on the already-running host kernel.
Why does Docker Desktop on macOS run a hidden Linux VM?
Linux containers need a Linux kernel, and macOS does not have one, so the VM supplies it.
Keep going
Lesson 39 of 92 in the DevOps path