Skip to main content

Container

A container is an isolated process on a shared Linux kernel, restricted by namespaces and cgroups and started from an image. It gives an application its own filesystem, network, and process view without booting a separate operating system.

What a container is

A container is a normal Linux process that the kernel presents with a private view of the system: its own filesystem, network interfaces, process tree starting at PID 1, hostname, and mounts. Namespaces control what the process can see. Control groups (cgroups) control how much CPU, memory, and I/O it can use. Because no second kernel boots, containers start in well under a second.

flowchart TD subgraph vm [Virtual machine] V1[App] --> V2[Guest OS and kernel] --> V3[Hypervisor] end subgraph ct [Container] C1[App process] --> C2[Shared host kernel] end V3 --> H[(Hardware)] C2 --> H classDef a fill:#DBEAFE,color:#1E293B classDef b fill:#FFEDD5,color:#1E293B class V1,C1 a class V2,V3,C2,H b

Namespaces Docker uses

  • PID: the container sees its app as PID 1, while the host sees a normal PID.
  • NET: separate interfaces, routing table, and ports.
  • MNT: its own mounts and root filesystem.
  • UTS: its own hostname.
  • IPC: isolated shared memory and message queues.
  • Cgroup: its own view of the cgroup tree.
  • User: maps container UIDs to different host UIDs, available through user namespace remapping and rootless mode but not enabled by default.

Limits with cgroups

Bash
docker run -d --name payment-api --memory=512m --cpus=1.5 payment-api:v3.1.0
docker inspect payment-api --format '{{.HostConfig.Memory}}'
# 536870912
# If it exceeds the limit the kernel kills it: exit code 137, OOMKilled true
docker inspect payment-api --format '{{.State.OOMKilled}}'

Image versus container

An image is the read-only template, and a container is a running instance with its own thin writable layer. Many containers can start from one image and share its layers on disk.

Useful commands

Bash
docker run -it --rm ubuntu:24.04 bash # interactive throwaway container
docker ps -a # running and stopped containers
docker logs payment-api # why did it exit
docker exec -it payment-api sh # shell in a running container
Remember

Containers share the host kernel, so a kernel vulnerability can affect every container on that host.

Security

Avoid --privileged. If a container needs one extra permission, add only that capability with --cap-add.

Frequently Asked Questions

How is a container different from a virtual machine?

A VM runs a full guest operating system with its own kernel on emulated hardware. A container is a process on the host kernel with isolated views and resource limits, so it starts faster and uses less memory but offers weaker isolation.

Is a container a security boundary like a VM?

Not by itself. All containers on a host share one kernel, so kernel bugs or dangerous settings like --privileged can lead to escapes. For untrusted workloads, add sandboxes such as gVisor or Kata Containers.

Why is my data gone after the container was removed?

Files written to the container filesystem live in its writable layer, which is deleted with the container. Use a volume for data that must persist.

Found a mistake, an outdated step or a broken link?