Linux › Section Q · Virtualization › Topic Q.2

Containers: namespaces and cgroups

A container runs ordinary Linux processes, but with their own view of the filesystem, process list and network, and with limits on what they may consume. There is no second kernel: containers are built from kernel features this course has already met, assembled by tools such as Docker and Podman.

From chroot to containers

"Virtual machines" ended with their costs: each is a full system to install, boot and patch. Containers isolate applications more cheaply. The idea grew from older tools. chroot changes a process's root directory, so it cannot see files outside a chosen tree, and resource limits (rlimits) cap a process's CPU time or file sizes. A container generalises both: a restricted environment for a group of processes, which cannot see or touch the rest of the system. All containers on a host share its single Linux kernel, but each may carry the user-space files of a different distribution, such as an Alpine container on a Fedora host.

Namespaces

A namespace gives a process its own private copy of one kind of system resource. In a PID namespace, the first process is PID 1 and sees only its own descendants. A mount namespace has its own list of mounted filesystems. A network namespace has its own interfaces, addresses, routes and firewall rules. UTS namespaces give a separate host name, IPC namespaces separate message queues and shared memory, and user namespaces map user IDs, so root inside a container can be an unprivileged user outside. Cgroup and time namespaces complete the set. Container tools create one of each kind for every container.

cgroups, layers and the image

Control groups, covered in "Control groups and performance tuning", limit what a container's processes may use: memory, CPU share, I/O and the number of processes. docker run --memory 256m --cpus 1.5 sets two such limits. The container's root filesystem is usually an overlay filesystem: read-only lower layers from the image stacked under a writable upper layer, with a work directory the kernel uses internally. An image is that stack of layers plus settings such as the default command; a container is a running instance of it. Many containers from one image share the lower layers, and each keeps its own changes in its upper layer.

Docker and Podman

Docker is the best-known container tool. Its command talks to a daemon, dockerd, which normally runs as root and does the privileged setup; users in the docker group can use it, which is effectively root access. Podman, developed by Red Hat and the default on Fedora and RHEL, needs no daemon and runs rootless by default, as an ordinary user, using user namespaces. Docker also offers a rootless mode. Podman's commands mirror Docker's, so podman can usually replace docker in any command below. Both follow the Open Container Initiative (OCI) standards, so images built with one run with the other and on Kubernetes.

Worked example: a first hands-on pass

Try this on any host with Docker; with Podman, replace docker by podman. 1. docker run -it --rm alpine:3.20 sh downloads the image if needed and starts a shell in a new container: -i keeps input open, -t gives a terminal, --rm deletes the container on exit. 2. Inside, ps shows only two processes, sh as PID 1 and ps itself: the PID namespace at work. 3. On the host, in another terminal, ps -ef does show that same sh, under a different PID. 4. docker ps lists running containers with their ID, image, command, status and a generated name. 5. docker images lists downloaded images and their sizes. 6. exit ends the shell, and --rm removes the container.

Building an image

A Dockerfile describes an image step by step. FROM alpine:3.20 names the base image. RUN apk add --no-cache bash runs a command at build time, inside a temporary container, and saves the result as a new layer. CMD ["/bin/bash"] sets the default command for containers started later. docker build -t mytool:1.0 . builds from the Dockerfile in the current directory and tags the result. Only RUN steps change the filesystem at build time; CMD acts when a container starts. Pin base images to a version, such as alpine:3.20, rather than latest, so a rebuild next month does not silently change underneath you.

Container networking

By default Docker uses a bridge network. It creates a host interface, docker0, usually on the private subnet 172.17.0.0/16 with the host at 172.17.0.1. Each container gets a network namespace holding a loopback interface and one end of a virtual Ethernet (veth) pair, whose other end attaches to docker0. NAT on the host lets containers reach outside networks, as in "Linux as a router: private networks and NAT". To accept incoming connections, publish a port: docker run -d -p 8080:80 nginx forwards host port 8080 to port 80 in the container. Rootless Podman cannot attach veth pairs to the host, so it uses a user-space network helper, pasta by default in Podman 5.

Worked example: a web server with saved data

Run a web server whose content survives the container. 1. docker volume create site makes a named volume, storage managed outside any container's layers. 2. docker run -d --name web -p 8080:80 -v site:/usr/share/nginx/html nginx:1.27 starts it detached (-d), names it, publishes port 8080 and mounts the volume where nginx serves files. 3. docker logs web shows its output; docker exec -it web sh opens a shell inside it. 4. docker stop web then docker rm web removes the container, but the volume and its files remain. 5. Starting a new container with the same -v option serves the same files. Without the volume, every change would vanish with the container's upper layer.

Housekeeping, LXC and Kubernetes

Stopped containers remain, with their changes, until removed: docker ps -a shows them, docker rm removes them, and docker rmi removes images. Rebuilding with the same tag leaves the old image untagged, listed as <none>, so clean up with docker image prune. Run a minimal init with --init when a container's main process may leave unreaped children. LXC takes a different approach, running a whole distribution with its own init inside a container; Incus, a community fork of LXD, manages such system containers. For many containers across many machines, Kubernetes schedules, restarts, networks and updates them, running OCI images through runtimes such as containerd or CRI-O.

Common mistakes to avoid

Believing a container has its own kernel: a kernel bug or a privileged container exposes the host. Adding users to the docker group without realising this grants root-equivalent power. Storing important data only in a container's writable layer. Using the latest tag in production. Pulling images from unknown publishers without checking their source. Letting stopped containers and old images fill the disk. Forgetting -it for an interactive shell, so the container exits at once. Assuming containers remove the need for resource limits, patching or logging: the host and the images still need maintaining.

Key takeaways

Containers are processes isolated by namespaces (PID, mount, network, UTS, IPC, user, cgroup, time) and limited by cgroups, sharing the host's kernel. Images are stacks of read-only layers; a running container adds a writable overlay layer. Docker uses a root daemon; Podman is daemonless and rootless by default; both use OCI images. docker run, ps, images, build, exec, logs, stop, rm and rmi cover daily work. Bridge networking uses docker0, veth pairs and NAT; -p publishes ports, -v volumes keep data, and pinned tags keep builds reproducible.

Flashcards

Flashcard

Container

Processes isolated by namespaces and limited by cgroups

No separate kernel.

Test your understanding

Question 1 of 5

Inside a new container, ps shows only two processes, with the shell as PID 1. Which kernel feature explains this?

Question 2 of 5

What is the key difference between a container and a virtual machine?

Question 3 of 5

A container's files disappear when it is removed and recreated. How do you keep the data?

Question 4 of 5

What does docker run -d -p 8080:80 nginx:1.27 do?

Question 5 of 5

Why is adding a user to the docker group a security decision?

← Previous topicNext topic →