Knowledge
Docker: the platform and container architecture
Estimated time: 35 minutes.
Quick summary
-
Infrastructure evolution: physical → virtualization → cloud → container
- Why: The evolution of IT runs from physical machines (one machine = one purpose, low utilization) to virtualization/cloud (80–95% hardware utilization, high density) and on to containers (the highest density, deployment in seconds). Each step improves hardware utilization and deployment speed; the container is seen as an "IaaS 1.5" level between the virtualized infrastructure and the application platform.
- When it breaks: Not understanding why containers appeared leads to treating them as a "fun technology" rather than a tool for optimizing resources and cost (power, hardware).
- In real deployments: Choose the technology layer by density and speed requirements; containers for high-density microservices, VMs when you need strong kernel isolation.
-
The shipping container analogy (conex)
- Why: Before the 1960s, goods packed in ad hoc ways wasted a lot in transport; the standard maritime shipping container (conex) allows uniform, safe packing that makes the best use of space on every vehicle. Software containers apply exactly this idea: package the application to a STANDARD so it runs consistently on a dev PC, staging, production, the cloud, and a customer's on-premises environment.
- When it breaks: Ignoring the "standardized packaging" aspect leads to the wrong expectation that you just reinstall in each place, losing the "build once, run anywhere" value.
- In real deployments: Package the app and its dependencies into a standard image so you can move between environments without setting up the platform again.
-
Container vs virtual machine (shared kernel)
- Why: A container is an isolated process that shares the host kernel; many containers "share the same kernel" instead of each having its own OS (Docker: what is a container). A VM virtualizes the hardware through a hypervisor, and each VM has a full OS and kernel. Containers drop that guest OS layer, so they are lighter, faster, and denser on the same hardware.
- When it breaks: Treating a container like a VM leads to the wrong expectation about kernel isolation: a namespace escape vulnerability affects the host, unlike the hard boundary of a VM.
- In real deployments: Choose containers for density and speed; choose VMs when you need strong kernel isolation or need to run an OS/kernel different from the host.
-
A container is, at its core, a process of the host
- Why: Because it only creates a process plus namespaces (no OS boot), a container starts in a few seconds and appears in the host's process table with its own PID. This is the reason it is "light and fast": there is no full operating system boot as there is with a virtual machine.
- When it breaks: Assuming a container is an independent "machine" leads to misunderstanding resources, process monitoring, and the isolation model.
- In real deployments: Treat a container as a short-lived or long-lived process unit; run-to-completion tasks (serverless-like) do not need a container kept alive.
-
An image is read-only layers plus layer sharing
- Why: An image is built from a chain of read-only layers; each repackaging creates a new layer that inherits the previous one (like version management) (Docker: storage drivers). Many images from the same family share identical base layers: Docker stores only one copy of each layer, so the real total size is much smaller than adding up each image (for example, multiple python versions). A container holds only what is needed to run, so it is very compact.
- When it breaks: Confusing an image (read-only, shared layers) with a container (its own writable layer) leads to storing state in the container and losing it when the container is recreated.
- In real deployments: Keep images immutable and small; take advantage of layer sharing to save space when running many images with the same base.
-
Docker architecture: daemon + registry + CLI
- Why: Docker consists of the Docker Host running the daemon (dockerd) that manages images and containers, the Registry that stores images (public like Docker Hub, or a self-hosted private one), and the CLI (docker build/pull/run) for giving commands (Docker: overview). The common flow: pull an existing image (the majority of cases) or build from a Dockerfile when you need customization, then run (docker pull).
- When it breaks: Not distinguishing pull (fetching an existing image) from build (creating a new image) leads to confusion about when you need a Dockerfile and when you can just pull and run.
- In real deployments: Set up your own registry (Harbor/Nexus/registry:2) plus CVE scanning to manage internal images; most deployments only need to pull an image already built by CI.
-
Immutability and volumes
- Why: Containers are immutable: every run is exactly the image version; to change it you must replace it with a new image. Docker does not store container state; it is only a runtime environment, and data must be written to a volume outside the host (Docker: volumes). If you do not attach a volume, when the container is deleted the data (and any hot edits) goes with it.
- When it breaks: Stuffing code and data into a container as if it were long-term storage is a design mistake: you lose data on recreate or upgrade.
- In real deployments: A container holds only the runtime; important code, data, and logs are mounted out through a volume so they can be updated without affecting the container.
Step 1 — Why containers exist: the evolution chain and the "one machine = one purpose" problem
A traditional physical machine runs only one environment well at a time (install Java and it is hard to install PHP alongside), and it uses hardware poorly. Virtualization and cloud raise density; containers push density and deployment speed to the highest level. The core problem: run many environments (prod/test/dev) at the same time on the same infrastructure, and move applications flexibly between environments without setting up again.
When it breaks: Not grasping this problem leads to overusing physical machines and VMs for everything, which wastes resources and makes deployment slow.
for i in 1 2 3 4 5; do docker run -d --name density-$i nginx:alpine; done
docker stats --no-stream # MEM USAGE column: each container costs only a few MB, not a whole OS
docker rm -f density-1 density-2 density-3 density-4 density-5
Observe with:
docker stats --no-stream (RAM per container; exact numbers vary by machine) ; compare with the minimum RAM of a VM running a full OS
Step 3 — An image is read-only layers and layer sharing
An image is built from a chain of read-only layers, one per repackaging; a container adds a copy-on-write writable layer on top (Docker: storage drivers). docker history shows each layer; many images with the same base share identical layers, so the total size is smaller than adding them up. A container holds only what it needs to run (for example, a web app does not need an SSH server), so it is very compact.
When it breaks: Writing important data into the writable layer loses the data when the container is deleted.
docker pull python:3.11
docker pull python:3.12
docker image ls # the real total < the sum, because layers are shared
docker image history python:3.11
docker image inspect --format "{{.Os}} {{.Architecture}}" python:3.11
Observe with:
docker image ls (compare the real size) ; docker image history <image>
Step 4 — Docker architecture and the pull vs build flow
The Docker Host runs the daemon that manages images and containers; the Registry stores images; the CLI gives commands (Docker: overview). Reading the diagram: a solid line = pull (fetching an existing image), a dashed line = build (from a Dockerfile). In practice most of the time it is a pull; you only build when you need customization. After installation, docker info shows the default network Docker creates and the root directory that stores images and layers (by default /var/lib/docker) (docker info).
When it breaks: Not distinguishing pull from build leads to confusion about when you need a Dockerfile; ignoring a bloated /var/lib/docker causes production to run out of disk.
docker info | grep -E "Root Dir|Containers|Network"
docker pull nginx # solid line: pull an existing image
# docker build -t app . # dashed line: build from a Dockerfile (covered in depth in Part 2)
Observe with:
docker info (default network, Docker Root Dir) ; docker version
Step 5 — The core mechanisms: namespaces and cgroups (not new at all)
Namespaces isolate "what the container sees" (PID/NET/MNT/UTS/IPC/USER — man namespaces(7)); cgroups limit "what the container can use" (CPU/memory/I-O — man cgroups(7)). Both mechanisms have been in the Linux kernel for decades; cgroups came out of the need to divide up CPU on multi-core machines so that hardware is not wasted. Docker just made them easier to use.
When it breaks: Thinking Docker "invented" isolation leads to misunderstanding its nature; it is really an abstraction layer on top of existing kernel mechanisms.
docker run -d --name demo nginx
lsns | head # the namespaces on the host
docker inspect --format "{{.HostConfig.CgroupParent}}" demo
docker rm -f demo
Observe with:
lsns (lists the namespaces on the host) ; docker inspect (cgroup configuration)
Common misconceptions
- Wrong: A container is a lightweight virtual machine, and each container runs its own operating system. Right: A container is an isolated process that shares the host's kernel through namespaces and cgroups; there is no guest OS or separate kernel as in a virtual machine.
Docker defines containers as isolated processes, and when you run many containers "they all share the same kernel", so a full OS is not needed for each container (Docker: what is a container). The isolation comes from Linux namespaces (PID/NET/MNT/UTS/IPC/USER — man namespaces(7)), which decide "what the container sees", while cgroups limit CPU/memory/I-O, that is, "how much the container uses" (man cgroups(7)). With a virtual machine, a hypervisor virtualizes the hardware, and each VM runs a full OS and kernel. The consequences: a container drops one operating system layer, so it starts very fast (a few seconds, since all it does is run a process), its image is small because it does not package a kernel, and the container process appears in the host's process table, quite unlike the hard boundary of a VM.
- Wrong: Docker invented container isolation technology. Right: namespaces and cgroups have been in the Linux kernel for decades; Docker only made them easier to use through a CLI and interface.
Process isolation rests on two kernel-level mechanisms: namespaces isolate resource spaces (man namespaces(7)) and control groups limit resources (man cgroups(7)); both predate Docker by a long way. cgroups came from the practical need to divide CPU among many processes on one multi-core physical machine (so as not to waste expensive hardware), which is exactly the foundation containers reuse today. The consequence: understanding that containers are "not new" removes the illusion of magic. Docker just packages existing Linux mechanisms behind a simple CLI, much as iptables hides the complexity of low-level packet filtering.
- Wrong: You can edit the contents inside a container and the change persists. Right: Containers are immutable: every run is exactly the image version at the time of creation; for a lasting change you must build a new image and replace the container. Data that must last has to be written to a volume outside the host.
A container runs from an image made of read-only layers; changes at run time live only in a thin writable layer, and "when a container is destroyed, the writable layer is destroyed with it" (Docker: volumes, storage drivers). Hot-editing a file in a running container takes effect immediately but is only a "temporary patch"; delete the container and run it again from the same image, and every change disappears and the content returns to the image's default. The consequence: the right way is to edit at the source (the Dockerfile or source code) and build a new image; any state that must last (databases, uploads) has to live in a volume or bind mount outside the container's lifecycle.
- Wrong: A Docker image depends on the Linux distribution (Ubuntu/RedHat) the way system packages do. Right: A container depends only on the kernel underneath, not on a specific distro; thanks to the open OCI standard, images and registries from different vendors are cross-compatible across Docker, Podman, and containerd.
Traditional Linux packages are locked to a distro (Ubuntu uses Ubuntu repositories, not RedHat's RPM repositories). Containers are different: a container packages the application and the libraries it needs and relies only on the host kernel, so it "runs consistently on any infrastructure" (Docker: what is a container). The cross-compatibility comes from the open OCI (Open Container Initiative) standard, which defines the image format and runtime that Docker, Podman, and containerd all follow (Open Container Initiative). The consequence: an image built once can be pulled and run from Docker Hub, quay.io, Harbor, GitLab Registry, and so on, with different runtimes, so you are not locked into one vendor.
Hands-on 1 — Observe the Docker architecture after installation
10 minutes
Before running your first container, read docker info to see the Docker environment: the bridge network Docker creates by itself, the root directory that stores images and layers, and the number of containers (docker info). Build the habit of "check the information before you act".
docker version && docker info
1. Check the client and server: docker version.
It shows the Client and Server (Engine) versions, confirming that the daemon is running.
2. Get an overview of the environment: docker info. Pay attention to the Docker Root Dir line and the Network section.
Docker Root Dir is usually /var/lib/docker; there is a default network (bridge) that Docker creates at install time.
3. Compare: before installing Docker the machine has only localhost plus its physical interface; after installation it also has its own bridge.
This shows that Docker adds a bridge network layer for containers.
Analysis
Observation: docker info shows the Docker Root Dir and the bridge network created by Docker.
Mechanism: The daemon creates the bridge network and the directory that stores layers and images when it initializes the Docker environment.
Takeaway: The /var/lib/docker directory grows over time, so it should be placed on a large partition or disk from the start.
Troubleshooting:
- docker info reports "permission denied" when connecting to the daemon: Add the user to the docker group (sudo usermod -aG docker $USER) and log in again, or run with sudo.
Hands-on 2 — Prove layer sharing across multiple images
10 minutes
Pulling two (or three) python versions and comparing the real total size against the sum of the individual sizes shows that the images share identical base layers (Docker: storage drivers).
docker pull python:3.11 && docker pull python:3.12 && docker image ls
1. Pull two versions: docker pull python:3.11 then docker pull python:3.12.
The second pull downloads faster because many base layers are already present (it shows "Already exists").
2. List the images: docker image ls and add up the size of each line in your head.
The real total size on disk is smaller than the sum of the individual images.
3. Look at the shared layers: docker image history python:3.11 and docker image history python:3.12.
Many of the first layers (the base) are identical between the two images.
Analysis
Observation: Pulling the second image shows "Already exists" for the base layers; the real total size < the sum.
Mechanism: Docker stores one copy of each layer and shares it among images with the same base (layer contents are identified by digest).
Takeaway: Layer sharing saves space and bandwidth when you run many images from the same family.
Review questions
Question 1. In the infrastructure evolution chain, what step comes immediately BEFORE "cloud"?
- Physical
- Virtualization
- Container
- Serverless
Answer: Virtualization. The evolution chain: physical → virtualization → cloud → container. Virtualization (VMware/KVM/Hyper-V) is the foundation of the cloud (OpenStack/CloudStack) before containers came along.
Question 2. What is the core difference between a container and a virtual machine?
- Containers run faster thanks to a more powerful CPU
- Containers share the host kernel and have no guest OS of their own like a VM
- Containers do not need hardware
- A VM cannot run Linux
Answer: Containers share the host kernel and have no guest OS of their own like a VM. A container is an isolated process that "shares the same kernel" with the host, dropping the guest OS layer that every VM must have through a hypervisor (Docker: what is a container), which is why it is light and starts fast.
Question 3. Which Linux mechanism isolates "what the container sees" (processes, network, mounts)?
- cgroups
- namespaces
- hypervisor
- systemd
Answer: namespaces. Namespaces isolate the resources a container sees (PID/NET/MNT/UTS/IPC/USER — man namespaces(7)). cgroups limit the AMOUNT of resources that can be used (man cgroups(7)); they do not isolate visibility.
Question 4. Why is the total size smaller than the sum of the individual sizes when you pull several python images (3.11, 3.12, 3.13)?
- Docker compresses the images
- The images share identical base layers
- Docker keeps only the newest image
- The python image is inherently very small
Answer: The images share identical base layers. An image consists of read-only layers; images from the same family inherit identical base layers, and Docker stores only one copy of each layer, shared among the images (Docker: storage drivers).
Question 5. Which standard makes images and registries cross-compatible across Docker, Podman, and containerd?
- POSIX
- OCI (Open Container Initiative)
- HTTP
- ISO 9001
Answer: OCI (Open Container Initiative). OCI defines the common image format and container runtime that Docker, Podman, and containerd all follow (Open Container Initiative), so an image is not locked into one vendor.
Question 6. What is the right way to make a LASTING change to the application contents in a container?
- exec into the container, edit the file, and leave it as is
- Edit at the source (Dockerfile or source code) → build a new image → replace the container
- Use docker pause to keep the change
- Write directly into /var/lib/docker on the host
Answer: Edit at the source (Dockerfile or source code) → build a new image → replace the container. Containers are immutable: a hot edit is only temporary and is lost on recreate. The right way is to edit the source, then build a new image and replace; state that must last is written to a volume (Docker: volumes).
Flashcards
-
What is the core difference between a container and a VM? A container shares the host kernel (a process isolated by namespaces and cgroups); a VM runs its own OS and kernel through a hypervisor. Explains why containers are light and start fast.
-
namespaces vs cgroups? namespaces isolate "what is seen" (PID/NET/MNT/UTS/IPC/USER); cgroups limit "how much is used" (CPU/memory/I-O).
-
Is the container a new technology from Docker? No. namespaces and cgroups have been in the Linux kernel for decades; Docker only made them easier to use.
-
What does an image consist of? Why does it share space? An image = a chain of read-only layers; many images with the same base share layers, and Docker stores one copy of each layer.
-
What does "immutable container" mean? Every run is exactly the image version; to change it you must build a new image and replace, not edit in place for the long term.
-
Does Docker store container data? No. Docker is only a runtime environment; data that must last has to be written to a volume outside the host, otherwise it is lost when the container is deleted.
-
What are the 3 components of the Docker architecture? Docker Host/daemon (dockerd) manages images and containers · Registry stores images · CLI (build/pull/run) gives commands.
-
What is OCI? The Open Container Initiative: an open standard defining the image format and runtime, making Docker, Podman, containerd, and registries cross-compatible.