Software Engineeringunpacked

Docker and Kubernetes explained: what is Docker, what is Kubernetes, and how containers, images, pods and clusters fit together

Docker packages an app with everything it needs (runtime, libraries, system tools, settings) into an image, and runs it as a container: an ordinary process isolated by the Linux kernel, which shares the host's kernel instead of booting its own operating system like a virtual machine. Kubernetes runs many of those containers across a cluster of machines. You declare what should be running (three copies of the API, version 1.4) and its controllers keep it true: they replace failed pods, scale with traffic and roll out new versions without downtime. You build with Docker and run with Kubernetes.

Video · Docker and Kubernetes Explained: Containers, Images, Pods and Clusters for Beginners · 18:00

What is Docker, in one paragraph

Prefer to watch? The 18-minute video covers the same ground with diagrams for every concept. In a hurry? The one-minute Short: What is Docker?.

Docker packages an app with everything it needs to run into an image, and starts containers from that image. A container is an ordinary process that the Linux kernel isolates: it gets its own files, its own view of the processes and its own network. Because the app carries its runtime and libraries with it, it behaves the same on your laptop, on a test server and in production. Kubernetes comes later, when you have dozens or hundreds of containers to keep running across many machines.

To make every idea concrete, this guide follows one small system: an online coffee shop with three parts. A web API takes the orders, a database keeps them, and a worker in the background sends the confirmation emails. We ship all three with Docker, then run them with Kubernetes.

The problem containers solve: "it works on my machine"

An app never runs alone. It needs a language runtime, libraries, system tools and settings, and your laptop and the server rarely have exactly the same ones. Your laptop has Python 3.12 and version 2 of a library; the server has Python 3.9 and version 1. Same code, different surroundings, and the app breaks on the server.

Containers fix this by shipping the surroundings with the code. What runs in production is the exact package you tested, down to the library versions.

What is a container?

A container is your app, packaged with its code, runtime, libraries, system tools and settings, running as an isolated process. There is no magic and no second computer: Docker's own documentation describes a container as an ordinary process that runs in isolation, with its own filesystem, its own view of processes and networking, and optional limits on memory and CPU.

That isolation comes from two features of the Linux kernel, the core of the operating system:

Docker vs virtual machine

A virtual machine imitates a whole computer. A hypervisor splits the real hardware, and each virtual machine boots its own complete operating system with its own kernel. A container boots nothing: it is a process, so every container on a machine shares that machine's kernel.

Virtual machine Container
What runs a full operating system one isolated process
Kernel its own shared with the host
Start time like a computer booting like a program starting
Typical size gigabytes often megabytes
Isolation stronger, at the hardware level weaker, enforced by the shared kernel

So containers are smaller and faster, and one machine can run many of them, while virtual machines isolate more strongly. The two are not exclusive. Containers often run inside cloud virtual machines, and on macOS and Windows, Docker Desktop runs your Linux containers inside a small Linux virtual machine, because they need a Linux kernel.

Docker images and containers

A container starts from an image: the package of everything it needs, meaning the operating system files, the runtime, the libraries, your code and the command to run. Once built, an image never changes. To change it, you build a new one.

From one image you can start as many containers as you like. Containers do not copy the image; they share its layers, and each one adds a thin writable layer on top where its own changes go.

Image Container
What it is a read-only package a running process started from an image
Can it change? never, you build a new one yes, in its own writable layer
How many? one per version as many as you start
Analogy a program file a program that is running

Keep that writable layer in mind. It is why data needs volumes, two sections below.

Dockerfile layers and the build cache

You build an image with a Dockerfile, a short text file of steps. For the coffee shop API, the steps are:

  1. Base image: a slim Linux with Python.
  2. System packages: a few tools the app needs.
  3. Library list: copy the file that lists the libraries.
  4. Libraries: install them from that list.
  5. App code: copy your source code.
  6. Start command: the command that starts the API (a setting, not a file change).

Each step that changes files produces one layer, a read-only set of file changes: files added, changed or deleted. The image is those layers stacked. Every layer is stored by a digest of its contents, so identical layers are stored once, and ten images built on the same base share it.

Layers also make builds fast, because Docker caches every layer it has built. When you change one line of code, only the code layer changes; everything below it comes from the cache. But a changed layer forces every layer after it to be rebuilt too. That is why steps that rarely change go first and your code goes last. Put the code before the library install, and every small edit would reinstall all your libraries.

Registries, repositories and tags

A registry is a service that stores images so any machine with access can pull them. You build an image on your laptop or in a CI pipeline (like the ones in our Claude Code GitHub Actions guide), push it to the registry once, and every server pulls and runs it.

Images are portable because they follow an open standard. The Open Container Initiative (OCI), launched in 2015 by Docker, CoreOS and others, defines three specifications: the Image spec (what an image is), the Runtime spec (how to run a container) and the Distribution spec (how registries hand images out). An image built with one tool runs with another, which matters for Kubernetes.

Docker volumes and networks

Our database keeps the orders inside its container. What happens when we replace that container with a new version? The writable layer is destroyed with the container, and every order would be gone.

The fix is a volume: storage created and managed by Docker that lives outside the lifecycle of any container. The database writes into the volume, and the next container picks up the same data. One volume can even be mounted into several containers. A bind mount is the other common option: it plugs a folder from your own machine into the container, which is handy during development.

Containers also need to find each other. On Docker's default network, containers can reach each other only by IP address, and those addresses change. On a network you create (a user-defined bridge), Docker provides automatic name resolution, so the API simply connects to the host name db, and only containers attached to that network can talk to each other.

To let customers in, you publish a port, such as 8080, from the machine to the API container. If ports, IP addresses and name lookups are new to you, our guide to how the internet works explains them.

Docker Compose: the whole app in one file

Starting three containers, a volume and a network by hand gets tedious fast. Docker Compose describes the whole app in one file, usually compose.yaml. Each part is a service; our file has three (the API, the database and the worker) plus one volume for the orders.

One command, docker compose up, creates the network, the volume and every container, and one command takes it all down. Compose also sets up a single default network for the app, where each service is reachable by its service name. That is why the API can reach the database at db with no IP address anywhere.

Compose is ideal for development, for tests (a real database in a throwaway container is the basis of good integration tests, see our guide to the types of software testing) and for a small app on one server. But it runs containers on one machine. As the coffee shop grows, it meets questions Compose was never built to answer.

What is Kubernetes?

Imagine forty containers on twenty servers. At 3 a.m. a server dies: who restarts its containers somewhere else? On Black Friday traffic triples: who starts more API containers, and stops them afterwards? On release day, who replaces forty containers with no downtime, and rolls back if something breaks? Doing this automatically is called container orchestration, and Kubernetes is the best known orchestrator.

Kubernetes in one sentence: you declare what you want running, and Kubernetes keeps it that way. The name is Greek for helmsman or pilot, and it is often shortened to K8s, for the eight letters between the K and the s. Google open-sourced it in 2014, building on more than fifteen years of running its own production workloads.

The Kubernetes cluster: control plane and nodes

A Kubernetes cluster is a control plane plus one or more worker machines called nodes.

The control plane is the brain:

Each node runs a kubelet (an agent that makes sure the right containers are running), usually kube-proxy (network rules that implement services) and a container runtime that actually runs the containers.

Kubernetes objects: pods, deployments and services

Kubernetes does not run containers directly; it runs pods. A pod is the smallest unit you can deploy: one or more containers that share network and storage, with an IP address of their own. Most pods hold one container, like our API; a second container is only for a tightly coupled helper. Pods are disposable. A failed pod is not repaired, it is replaced, with a new IP address.

You rarely create pods yourself. A deployment describes what you want, for example three replicas of coffee-api:1.4. It creates a ReplicaSet, whose job is to keep exactly that number of pods running. You never say "start three containers"; you say "there should always be three". That is desired state.

Because pods come and go with new addresses, callers need something stable. A service gives a group of pods one stable name and address, finds its pods by their labels, and spreads traffic across them. Inside the cluster, a service also gets a DNS name, the same kind of name lookup described in how the internet works.

Object What it is In the coffee shop
Node a machine in the cluster twenty servers
Pod the smallest unit: one or more containers, own IP one API container
Deployment desired state for identical pods, through a ReplicaSet coffee-api, 3 replicas, image 1.4
Service a stable name and address for a set of pods coffee-api, used by the website and the worker
StatefulSet like a deployment, for pods that keep state an option for the database
HorizontalPodAutoscaler changes the replica count with demand 3 to 10 API pods

A service is reachable only inside the cluster by default (type ClusterIP). The NodePort type exposes it on a port of every node, and the LoadBalancer type asks the cloud for an external load balancer.

Desired state and self-healing

How does Kubernetes keep its promise? With controllers. The Kubernetes documentation compares them to a thermostat: you set the temperature you want (the desired state), the room has its real temperature (the current state), and the thermostat switches the heating on or off to close the gap. A controller is a loop that never ends: observe the cluster, compare it with what you declared, act on the difference, and start again.

Back to 3 a.m. Node two dies, and its API pod goes with it. The controller sees two pods running where three are desired, a new pod is created, the scheduler places it on a healthy node, and the count is back to three. Nobody woke up. That is self-healing. Kubernetes also restarts containers that crash, kills containers that fail the health check you define, and only sends traffic to pods that report they are ready.

Scaling with the Horizontal Pod Autoscaler

On Black Friday you could change three replicas to ten yourself, or let the Horizontal Pod Autoscaler (HPA) do it. It watches a metric, such as average CPU use against a target, and changes the deployment's replica count. Horizontal means more pods; vertical would mean bigger pods.

The documentation's own example: four pods averaging 200 millicores of CPU against a target of 100 means double the target, so the HPA asks for eight. When traffic drops it scales back down, never below the minimum you set. By default it checks every 15 seconds, and it needs a metrics source in the cluster, usually the Metrics Server add-on.

Rolling updates and rollbacks

Version 1.5 of the API is ready. You change the image in the deployment, and Kubernetes performs a rolling update: it creates a new ReplicaSet, starts a few new pods, removes old ones as the new ones become ready, and repeats until every pod runs 1.5. By default at most 25% extra pods are added (max surge) and at most 25% can be unavailable (max unavailable), so the service keeps answering throughout.

A good habit is to run a quick smoke test right after each rollout, one of the checks described in our guide to the types of software testing.

If version 1.5 misbehaves, one command (kubectl rollout undo) returns the deployment to its previous revision.

One side effect to plan for: a pod that is replaced closes the connections it held. Apps that keep long-lived connections open, such as WebSockets, need clients that reconnect on their own.

Docker and Kubernetes: how they fit together

Docker and Kubernetes are not rivals. In a typical team you build images with Docker, push them to a registry, and Kubernetes runs them in production.

Docker Kubernetes
Main job build images and run containers keep many containers running as declared
Scope one machine a cluster of machines
Key ideas image, container, volume, network pod, deployment, service, node
Many containers Compose, on one host scheduling, self-healing, scaling, rollouts

One detail confuses many people. Since version 1.24, Kubernetes no longer talks to Docker Engine on its nodes; it uses runtimes such as containerd or CRI-O. Your images are not affected: the Kubernetes project states that images produced by docker build keep working, because any OCI-compliant image looks the same to Kubernetes, whatever tool built it.

When you don't need Kubernetes

Kubernetes solves real problems, but it is a large system. Someone has to learn it, secure it, upgrade it and watch it. A few rules of thumb:

The coffee shop on Kubernetes ends up as a deployment for the API (three replicas, an autoscaler up to ten, a service in front), a deployment of two workers, and a database that is either a StatefulSet or, as many teams choose, a managed database outside the cluster. Every new version ships as a rolling update.

Summary

A container is an isolated process that carries everything it needs. It starts from an image, a read-only package built in cached layers. Registries share images, volumes keep data alive, networks let containers find each other, and Compose runs a whole app from one file on one machine. Kubernetes runs containers across a cluster with pods, deployments and services, and its big idea is desired state: you declare what you want, and controllers keep it true.

More guides like this one are in the article library, and if networking terms like ports and IP addresses are still fuzzy, start with how the internet works.

Docker is a trademark of Docker, Inc.; Kubernetes is a registered trademark of the Linux Foundation; this site is not affiliated with either.

Sources: What is a container? (Docker) · What is an image? (Docker) · Understanding image layers (Docker) · Build cache (Docker) · What is a registry? (Docker) · Volumes (Docker) · Bridge network driver (Docker) · Networking in Compose (Docker) · Open Container Initiative · namespaces(7) · cgroups(7) · Kubernetes overview · Kubernetes components · Pods · Deployments · Controllers · Service · Horizontal Pod Autoscaling · Don't Panic: Kubernetes and Docker

FAQ

What is Docker in simple terms?

Docker is a tool that packages an application with everything it needs to run (the runtime, libraries, system tools and settings) into an image, and starts containers from that image. A container is an ordinary process that the Linux kernel isolates, so the app behaves the same on a laptop, a test server and in production.

What is Kubernetes used for?

Kubernetes runs containers across a cluster of machines and keeps them in the state you declared. It schedules pods onto nodes, replaces failed ones, gives groups of pods a stable address through services, scales them with the Horizontal Pod Autoscaler and ships new versions with rolling updates and rollbacks.

What is the difference between Docker and a virtual machine?

A virtual machine boots a complete operating system with its own kernel on top of a hypervisor. A container boots nothing: it is a process that shares the host's kernel. Containers are smaller and start like a normal program; virtual machines are heavier but isolated more strongly, at the hardware level.

Do I need Kubernetes to use Docker?

No. Docker and Docker Compose run containers on one machine, which is enough for development, tests and many small apps. Kubernetes pays off when you run many services on many servers and need self-healing, autoscaling and releases without downtime. Managed Kubernetes services can run the control plane for you.

Does Kubernetes still use Docker?

Not on its nodes. Since version 1.24, Kubernetes talks to container runtimes such as containerd or CRI-O instead of Docker Engine. Images built with Docker still run unchanged, because they follow the Open Container Initiative image format, which every compliant runtime understands.