Skip to content

Beginner · Docker · topic 15 of 53 · 30 min

Containers, images and registries

Explanation

A container is an isolated process that carries its own files, libraries and settings, so the app behaves the same everywhere. Unlike a virtual machine, it shares the host's kernel, which makes it start in a second and use little memory. You build containers from images, which are stacks of read-only layers, and you store and share images in registries such as Docker Hub or GHCR.

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 machineContainer
IncludesFull guest OS and kernelApp, libraries and a minimal file system; shares the host kernel
Start timeTens of seconds to minutesUsually under a second or two
SizeGigabytesMegabytes to a few hundred megabytes
IsolationStrong (separate kernel)Good, but weaker (shared kernel)
Typical useRunning different operating systems, strong separation between customersPackaging 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).

Think of it like this: A VM is like building a separate house for every family, with its own foundation, walls and water tank. Containers are like flats in one apartment building: each family has its own locked flat, but they share the foundation and the water supply (the kernel). Flats are cheaper and faster to set up, but a crack in the foundation affects everyone.

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.

output
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:latest
A digest, such as python@sha256:..., names one exact image by the hash of its content and never changes.
Careful: "latest" is just a tag name, the default when you do not write one. It does not mean newest or stable, and it can point to a different image tomorrow. In production, always use a specific version tag (or a digest), so every server runs the same thing and you can roll back.

Docker 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.
bash
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 stops
Open http://localhost:8080 and you see the nginx welcome page.

Common 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

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.

bash
docker pull ghcr.io/routeco/route-api:3f9c2ab
docker run --rm -p 8000:8000 ghcr.io/routeco/route-api:3f9c2ab

Practice (0/3 done)

Task 1 · Question

What do all containers on one Linux host share?

Task 2 · Code · Python

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.

Task 3 · Question

Yesterday and today you ran docker pull node:latest on two servers. Why might they now run different Node.js versions?

Quick quiz

Question 1

1.Which Linux feature limits how much memory and CPU a container can use?

Question 2

2.Why does a container start much faster than a virtual machine?

Question 3

3.A container edits a file that came from the image. What happens?

Question 4

4.What is the full reference for the image you get with docker pull python?

Question 5

5.What does OCI provide?

Read more (free)

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.

From Containers, images and registries

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.

From Docker: packaging your app

What do containers share with the host that virtual machines do not?Related

Answer: The kernel

Containers run on the host kernel.

From Containers with Podman: images, run, ports and logs

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.

From Image and container security

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

  1. 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.
  2. Module 4 exam: Docker · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Kubernetes fundamentals. 53 topics left in the course before the final exam.

← Previous: Semantic versioning, tags and releases