Software & OS

Kubernetes vs Docker: What's the Difference?

Docker and Kubernetes are not alternatives: Docker packages an application into an image and runs it as a container on one machine, while Kubernetes manages many containers across a cluster of machines. The usual comparison is really Docker (or Docker Compose) against Kubernetes for running an app, and the short rule is that a single host rarely needs Kubernetes.

Kubernetes vs Docker in one table

DockerKubernetes
What it is"An open platform for developing, shipping, and running applications""A portable, extensible, open source platform for managing containerized workloads and services"
Main jobBuild images, run containersSchedule, scale and heal containers across nodes
ScopeOne host (Docker Engine), or a swarm of enginesA cluster of nodes
Command linedocker, docker composekubectl
Config formatDockerfile, Compose file (YAML)YAML manifests (Pods, Deployments, Services)
Builds images?YesNo: it "does not deploy source code and does not build your application"
Runs containers itself?Yes, through the Docker daemonNo: it asks a container runtime on each node (containerd, CRI-O and others)

The quoted descriptions come from the Docker overview and the Kubernetes overview pages listed in the sources.

What Docker does

Docker is made of a client, docker, and a daemon, dockerd. The daemon "listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes." An image is a read-only template with instructions for creating a container, and a container is a runnable instance of an image. If those terms are new, start with What is Docker; for the difference between a container and a virtual machine see Docker vs virtual machine.

Running a container on your own machine is one command:

docker run -d -p 8080:80 nginx

If you need several containers on one host, for example a web app plus a database, Docker Compose describes them in one YAML file. Docker's documentation says Compose uses "a YAML configuration file, known as the Compose file, to configure your application's services." See Docker Compose explained.

What Kubernetes does

Kubernetes manages containers as a cluster. You describe the state you want, and it works to make reality match. The Kubernetes overview lists what that covers:

  • Service discovery and load balancing: a container can be exposed by a DNS name or its own IP address, and traffic is spread across containers.
  • Automated rollouts and rollbacks: you describe the desired state and Kubernetes changes the actual state to match "at a controlled rate".
  • Self-healing: it restarts containers that fail, replaces them, and kills containers that fail a health check you define.
  • Storage orchestration, secret and configuration management, and horizontal scaling.

Here is the example Deployment from the Kubernetes documentation. It asks for three copies of an nginx container:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Apply it with kubectl apply -f nginx-deployment.yaml, then check it with kubectl get deployments. Example output a few seconds later:

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           18s

The Deployment creates a ReplicaSet, which creates the Pods. If you change the Pod template, Kubernetes scales up a new ReplicaSet while scaling down the old one, and it can roll back to an earlier revision if the new one is unstable. Compare that with the single docker run above: nothing there restarts the container on another machine if the host dies.

How Kubernetes and Docker fit together

They work at different layers. You can build an image with docker build, push it to a registry, and reference it in the image: field of a Kubernetes manifest. Kubernetes then pulls it onto whichever node it picks.

To start the containers, each node needs a container runtime. Kubernetes requires one that implements the Container Runtime Interface (CRI), the protocol between the kubelet and the local runtime. The Kubernetes documentation lists containerd, CRI-O, Docker Engine and Mirantis Container Runtime. It also states that Kubernetes releases before v1.24 included a direct integration with Docker Engine, a component called dockershim, and that this integration is no longer part of Kubernetes. Docker Engine can still be used as a runtime, but through a separate CRI-compatible adapter.

Did Kubernetes drop Docker?

Kubernetes removed dockershim in v1.24, which was announced in v1.20. It did not stop supporting Docker-built images. The Kubernetes dockershim FAQ answers directly: "the images produced from docker build will work with all CRI implementations. All your existing images will still work exactly the same." Private registries also keep working, because all CRI runtimes support the same pull secrets configuration used in Kubernetes.

What changed is on the node. If you run your own cluster and relied on Docker Engine as the node runtime, you had to switch to a CRI runtime such as containerd, or install an adapter. If you use a managed cluster or only build images with Docker, nothing in your workflow changes.

What about Docker Swarm?

Docker has its own orchestrator. Swarm mode is a feature of Docker Engine for managing a cluster of engines, called a swarm. Docker's documentation says "You don't need additional orchestration software to create or manage a swarm", and, like Kubernetes, it works from a declared desired state: you declare the number of tasks for each service, and the manager reconciles any differences. Swarm is driven by the Docker CLI. Kubernetes has a larger feature set and a separate toolchain, which is why the two are often compared.

When to use Docker alone, Compose, or Kubernetes

  • Local development, one app on one machine: Docker, with Compose if you have more than one container.
  • A small app on a single server: Docker and Compose are usually enough. Nothing needs scheduling across machines.
  • Many services across several nodes, with rolling updates, automatic replacement of failed containers and scaling: this is what Kubernetes is designed for.

Kubernetes also costs you something: a cluster to run or pay for, new objects to learn, and its own failure modes. The overview itself notes what it leaves to you. It does not build your application, and it does not provide databases, message buses or caches as built-in services, nor dictate logging, monitoring or alerting solutions. Adopt it when you have the problem it solves, not before.

Frequently asked questions

Is Kubernetes a replacement for Docker?
No. They do different jobs. Docker builds container images and runs containers on a single machine. Kubernetes manages containers across a cluster of machines and needs a container runtime on every node to actually run them. You can use Docker to build an image and Kubernetes to run it.
Did Kubernetes drop support for Docker?
Kubernetes 1.24 removed dockershim, the built-in component that let the kubelet talk to Docker Engine. Images built with docker build still work with every CRI runtime. Docker Engine can still be used as a node runtime through a separate CRI adapter, but most clusters use containerd or CRI-O instead.
Do I need Docker to use Kubernetes?
No. A Kubernetes node needs a container runtime that implements the Container Runtime Interface, such as containerd or CRI-O. Docker is optional. You only need Docker, or another image builder, if you want to build images yourself.
Is Docker Swarm the same as Kubernetes?
Both orchestrate containers across several machines from a declared desired state. Swarm mode is built into Docker Engine and managed with the Docker CLI. Kubernetes is a separate platform with its own API, objects such as Pods and Deployments, and its own tooling, kubectl.

Sources