Beginner · Docker · topic 15 of 53 · 30 min
Containers, images and registries
Explanation
The "works on my machine" problem
A developer builds an API with Python 3.12 and a certain version of a PDF library. On the test server, Python is 3.10 and the library is older, so invoice generation crashes. Every machine is slightly different: operating system version, installed packages, environment variables. Containers solve this by shipping the app together with its environment, so the thing you tested is exactly the thing you run.
Containers versus virtual machines
A virtual machine (VM) pretends to be a whole computer. It runs a complete guest operating system with its own kernel (the core of the operating system that talks to the hardware) on top of a hypervisor. A container is much lighter: it is a normal process on the host, but fenced off so it sees only its own files, processes and network. All containers on a host share the host's kernel.
| Virtual machine | Container | |
|---|---|---|
| Includes | Full guest OS and kernel | App, libraries and a minimal file system; shares the host kernel |
| Start time | Tens of seconds to minutes | Usually under a second or two |
| Size | Gigabytes | Megabytes to a few hundred megabytes |
| Isolation | Strong (separate kernel) | Good, but weaker (shared kernel) |
| Typical use | Running different operating systems, strong separation between customers | Packaging and running applications and microservices |
Two Linux kernel features make containers possible. Namespaces control what a process can SEE: its own process list, network interfaces, mount points, hostname and user IDs. Control groups (cgroups) control how much it can USE: CPU, memory, disk I/O and number of processes. Because Linux containers need a Linux kernel, Docker Desktop on Windows and macOS runs a small Linux VM in the background (on Windows, usually through WSL 2).
Images and containers
An image is a read-only template: a file system (for example a slim Debian plus Python plus your code) and settings such as which command to run. A container is a running instance of an image. From one image you can start ten identical containers. When a container is removed, anything it wrote inside itself is gone, while the image stays unchanged.
Layers and copy-on-write
An image is made of layers. Each instruction in a Dockerfile that changes files (such as RUN, COPY or ADD) adds a layer containing just those changes. Layers are shared: if ten images use the same python:3.12-slim base, those base layers are stored and downloaded only once. When a container starts, Docker adds a thin writable layer on top. If the container changes a file from the image, the file is first copied up into the writable layer and changed there. This is copy-on-write, and it is why starting a container is so fast: nothing is copied in advance.
Registries, names and tags
A registry is a server that stores images. Docker Hub (docker.io) is the default public one. Others include GitHub Container Registry (ghcr.io), Amazon ECR, Google Artifact Registry and Azure Container Registry, and companies often run private ones. An image reference has up to three parts: registry, repository and tag.
nginx:1.27-alpine
registry: docker.io (the default when none is given)
repository: library/nginx (official images live under library/)
tag: 1.27-alpine
ghcr.io/acme/shop-api:2.3.1
registry: ghcr.io
repository: acme/shop-api
tag: 2.3.1
python → docker.io/library/python:latestDocker Engine, CLI and Desktop, and OCI
- Docker Engine: the background service (dockerd) that builds and runs containers on a Linux host.
- Docker CLI: the docker command you type; it sends requests to the engine.
- Docker Desktop: an app for Windows, macOS and Linux that bundles the engine, CLI, Compose and a GUI. It is free for personal use, education and small businesses; larger companies need a paid subscription (check the current terms).
- OCI (Open Container Initiative): open standards for image formats, runtimes and registries. Because of OCI, images built with Docker also run on containerd, Podman and Kubernetes.
docker version # client and engine versions
docker pull nginx:1.27-alpine # download an image from the registry
docker images # list local images
docker run --rm -p 8080:80 nginx:1.27-alpine # start a container, remove it when it stopsCommon mistakes
- Thinking a container is a small VM and installing SSH, cron and many services in it. One container should run one main process; start more containers for more services.
- Storing important data only inside a container. When it is removed, the data is gone. Use volumes (covered later).
- Expecting a Linux image to run natively on Windows or an x86 image on an ARM Mac. Images are built for a CPU architecture; multi-platform images include several.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Containers, images and registries · 1/7
Containers fix the works on my machine problem by shipping the app together with its environment.
Prefer a video by a teacher? Find free videos on “docker containers vs virtual machines images layers explained” on YouTube ↗
In real life: Onboarding at a Chennai logistics startup
A logistics startup runs a route-planning API that needs Python, a geospatial library with C dependencies and a specific PostgreSQL client. New developers used to lose a day installing all of it on Windows laptops. The team now publishes the API as an image to their private registry. A new joiner installs Docker Desktop, runs one docker run command and has the exact build that runs in production. The CI pipeline tests that same image, tagged with the git commit, before it is deployed to their servers.
docker pull ghcr.io/routeco/route-api:3f9c2ab
docker run --rm -p 8000:8000 ghcr.io/routeco/route-api:3f9c2abPractice (0/3 done)
What do all containers on one Linux host share?
Write parse_image(ref) that splits an image reference into a dict with "registry", "repository" and "tag". Rules: 1. If there is a "/" and the part before the first "/" contains "." or ":" or is "localhost", that part is the registry. Otherwise the registry is "docker.io". 2. A ":" AFTER the last "/" separates the tag. With no tag, use "latest". 3. On docker.io, a repository with no "/" gets "library/" in front (official images). References with a digest (@sha256:...) are not tested.
Yesterday and today you ran docker pull node:latest on two servers. Why might they now run different Node.js versions?
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Writing Dockerfiles: caching, multi-stage builds and small imagesDocker · same module
- Running containers: ports, volumes, networks and limitsDocker · same module
- Docker Compose: multi-container appsDocker · same module
- Containers, images and registriesCloud Computing (AWS, Azure, Google Cloud) · Containers in the cloud
- Docker: packaging your appBackend Development · Deployment and scaling
- Containers with Podman: images, run, ports and logsLinux and Red Hat (RHCSA to RHCE) · Boot, recovery and containers
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which Linux feature limits how much memory and CPU a container can use?
Answer: Control groups (cgroups)
cgroups limit and account for resources; namespaces limit what a process can see.
Why does a container start much faster than a virtual machine?
Answer: It does not boot an operating system kernel; it is just an isolated process
The host kernel is already running, so starting a container is close to starting a process.
A container edits a file that came from the image. What happens?
Answer: The file is copied into the container's writable layer and changed there; the image is unchanged
Copy-on-write keeps image layers read-only and shared.
What is the full reference for the image you get with docker pull python?
Answer: docker.io/library/python:latest
The default registry is docker.io, official images live under library/, and the default tag is latest.
What does OCI provide?
Answer: Open standards for image formats, runtimes and registries
Thanks to OCI standards, images work across Docker, containerd, Podman and Kubernetes.
What is Google Cloud's current registry for container images?Related
Answer: Artifact Registry
Artifact Registry replaced Container Registry.
What is the difference between an image and a container?Related
Answer: An image is the package (recipe); a container is a running copy of it
You build an image once and start one or many containers from it.
What do containers share with the host that virtual machines do not?Related
Answer: The kernel
Containers run on the host kernel.
Why is mounting /var/run/docker.sock into a container dangerous?Related
Answer: It lets the container control the Docker daemon, and so effectively the whole host
With the socket, a container can start a new privileged container that mounts the host file system.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 4 more topics in “Docker” before its exam: Writing Dockerfiles: caching, multi-stage builds and small images, Running containers: ports, volumes, networks and limits, Docker Compose: multi-container apps, Image and container security.
- Module 4 exam: Docker · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Kubernetes fundamentals. 53 topics left in the course before the final exam.