The Short Answer
A container is a group of processes that the Linux kernel isolates from the rest of the system. Inside it, a program sees its own file system, its own process list, its own network interfaces, and its own host name, but it runs directly on the host's kernel. The file system comes from an image that holds the application and everything it needs. Because nothing is emulated and no second kernel boots, a container starts in moments and adds almost no overhead.
Namespaces: What a Container Sees
| Namespace | Isolates |
|---|---|
pid | Process IDs |
net | Network interfaces, ports |
mnt | Mounted file systems |
uts | Host name |
ipc | Shared memory, queues |
user | User and group IDs |
The first process in a container has process ID 1 in its own view, while the host sees it under an ordinary number. List the namespaces on a host with sudo lsns.
Cgroups: What a Container May Use
Control groups (cgroups) limit and count the resources that a group of processes uses: CPU time, memory, disk input and output, and the number of processes. A memory limit on a container is a cgroup limit. When the container goes over it, the kernel's OOM killer kills a process inside it with SIGKILL; if that is the main process, the container stops with exit code 137. With Docker:
docker run -d --memory 512m --cpus 1.5 nginx
docker stats
docker stats shows each container's live use against its limits.
The Image
An image is a read-only file system in layers, plus metadata such as the command to start. Docker and Podman images follow the OCI image format, so an image built with one tool usually runs with the other. A running container adds a thin writable layer on top. Anything written there disappears when the container is removed, so databases, uploads, and other data belong in a volume or a mounted directory; see Docker volumes.
Container vs Virtual Machine
A virtual machine emulates hardware and boots a kernel of its own, so it can run any operating system and is isolated at the hardware level. A container shares the host's kernel, so it starts faster and uses less memory, but it can only run software for that kernel. Many setups use both, with containers running inside a virtual machine such as a VPS; see what is a virtual machine.
Container Runtimes and Tools
| Tool | Role |
|---|---|
| Docker | Builds and runs containers |
| Podman | Docker-like, no daemon |
| containerd | Runtime under Docker |
| runc | Low-level OCI runtime |
Docker is the most common way to start; see what is Docker and docker run. Podman is compared in Podman vs Docker. To run many containers across several servers, an orchestrator such as Kubernetes schedules them; a small distribution of it is K3s.
Limits of the Isolation
- One shared kernel: a kernel flaw can let a process escape its container. Keep the host's kernel updated.
- Root in the container: unless user namespaces remap it, root inside a container is user ID 0 on the host, with fewer privileges. Run applications as a non-root user in the image, or use rootless mode.
- Privileged containers and containers with the Docker socket mounted can control the host. Avoid both unless you know why you need them.
- Published ports: with Docker they bypass UFW rules on the host; see Docker port mapping.
System Containers and Application Containers
Application containers, such as Docker's, run one service each: a web server, a database, a worker. System containers, such as those of LXC and Incus, run a full init system and several services, and feel like a light virtual machine while still sharing the host's kernel. A virtual server built this way cannot load its own kernel modules or choose its kernel version, which matters for software that needs kernel features. The wider picture is in what is virtualization.