Kubernetes comparison
Docker Compose vs Kubernetes: Which One to Use and When
Docker Compose runs a multi-container app from one YAML file on one Docker host. Kubernetes runs containers across a cluster of nodes and keeps them at a declared state, with rolling updates, service discovery and restarts. Use Compose for local development and a single host you control. Move to Kubernetes when you need several nodes, scheduled placement, or the Kubernetes API and ecosystem.
By Muhammad Soliman, Founder at SeaGit
·
Key takeaways
- Compose defines services, networks and volumes in one file and starts them with one command. Its scope is the Docker host it runs on.
- Kubernetes schedules pods onto nodes, replaces failed pods, and rolls out new versions across replicas.
- Compose works in production as well as development, according to Docker docs. The difference is operational: who places the containers and who replaces them when they fail.
- Most Compose services map to a Deployment plus a Service, not to a single Kubernetes object.
What each one runs on
The Docker Compose page describes Compose as a tool for defining and running multi-container applications, with services, networks and volumes in a single YAML configuration. It also says Compose works in production, staging, development, testing and CI. What it does not do is place containers on a fleet of machines. A Compose stack lives on the Docker engine that runs it.
Kubernetes is a control plane that places pods on nodes, watches them, and reconciles them to the desired state. The unit it manages is the pod, and it describes that unit through objects such as Deployments, Services and ConfigMaps.
Side by side
| Question | Docker Compose | Kubernetes |
|---|---|---|
| Where containers run | One Docker host | Many nodes in a cluster |
| Replace a crashed container | Restart policy on that host | Controller creates a new pod, possibly on another node |
| Rolling update | Recreate services; you script the order | Deployment strategy with maxSurge and maxUnavailable |
| Service discovery | Service names on a Compose network | Service objects and cluster DNS |
| Learning and operating cost | Low; one file and one host | Higher; control plane, ingress, storage and upgrades to run |
The table shows the honest trade. Kubernetes handles placement and failure across machines, and you pay for that with more moving parts. Compose is simpler, and it stops being enough when a single host is the single point of failure.
Mapping a Compose service to Kubernetes objects
A Compose service becomes a Deployment for the containers and a Service for the ports. Volumes become PersistentVolumeClaims. Environment files become ConfigMaps or Secrets. The official init containers page is the Kubernetes answer to depends_on when a service must wait for a prerequisite to finish before it starts.
# docker-compose.yml fragment
services:
api:
image: registry.example.com/api:1.4.2
ports:
- "8080:8080"
environment:
LOG_LEVEL: info
depends_on:
- db
# Kubernetes equivalent (abridged)
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 2
selector:
matchLabels: { app: api }
template:
metadata:
labels: { app: api }
spec:
initContainers:
- name: wait-for-db
image: busybox:1.36
command: ["sh", "-c", "until nc -z db 5432; do sleep 2; done"]
containers:
- name: api
image: registry.example.com/api:1.4.2
ports:
- containerPort: 8080
env:
- name: LOG_LEVEL
value: info
---
apiVersion: v1
kind: Service
metadata:
name: api
spec:
selector:
app: api
ports:
- port: 8080
targetPort: 8080There is a conversion helper on this site that reads a Compose file and drafts the equivalent manifests. Treat its output as a starting point: it does not know about your secrets, your storage class or your ingress.
Decision rule: which one for which job
Local development and CI
Compose wins. One command, fast feedback, and no cluster to keep alive.
One server, small traffic, one team
Compose is a reasonable production tool here if you accept that one host failing means downtime. Put a restart policy on every service and back up the volumes.
Several nodes, rolling deploys, or self-healing
Kubernetes. The cluster replaces failed pods and rolls out a change without taking the service down, which a single Compose host cannot do.
- Need to survive the loss of a machine? Kubernetes, or at least two hosts with a load balancer in front of Compose.
- Need a team to deploy to several environments the same way? Kubernetes gives you one object model for all of them.
- Need the smallest thing that works? Compose, and revisit when the host becomes the bottleneck.