Kubernetes vs Docker Compose: What's the difference?
Kubernetes vs Docker Compose
Kubernetes and Docker Compose are both container orchestration frameworks. Kubernetes runs containers over a number of computers, virtual or real. Docker Compose runs containers on a single host machine.
Let’s look at the details.
Understanding Docker containers
Docker is a technology that is used to create and run containers. A container is a collection of 1 or more processes, organized under a single name and identifier that are isolated from the other processes running within a computing environment. That computing environment can be a physical computer or a virtual machine. Unlike a virtual machine, a container has no life outside of an operating system. A virtual machine carries its own operating system. This, it’s entirely possible to run a virtual machine with a Windows operating system on top of a host machine that’s running Linux. Containers, on the other hand, leverage an existing host operating system.
Understanding Docker Compose
As mentioned above, Docker Compose is a container orchestration technology that’s intended to run a number of containers on a single host machine. Developers create a Docker Compose file that describes the container images and configuration by which to make up the Docker Compose. The default name of this file is docker-componse.yml. Listing 1 below shows an example of a docker-compose.yml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
version: “3.9” services: web: build: . ports: – “5000:5000” networks: – my_network redis: image: “redis:alpine” networks: – my_network networks: my_network: |
Listing 1: A docker-compose file that publishes two services, web and redis
One of the attractive features of Docker Compose is that it allows developers to expose a container as a service and organize those service(s) under a “network”. This allows a number of containers to run on the host yet be organized and isolated according to the network under which the given container is run.
As you can see in Listing 1 above, that docker-compose.yml declares a network named, my_network at Line 14. The docker-compose.yml file also declared two services, web, at Line 3 and redis at Line 9. These services run under the network, my_network as declared a Lines 8 and 12, respectively. These two services are isolated under my_service. However, because the service, web exposes port 5000 at Line 6, users can access the web service container via port 5000.
One of the additional features of Docker Compose is that it can create containers using container images that are hosted on a container repository such as DockerHub. Also, Docker Compose can build containers based on a Dockerfile stored on the hosting machine. In terms of Listing 1 shown above, notice at Line 10 that the service, redis uses a container image, redis:alpine that hosted on Docker Hub. However, the service, web builds its container from the local Dockerfile according to the build attribute, as defined on Line 4.
The important thing to understand about Docker Compose is that it allows you to run many containers on a single host as distinct services and each service can be configured to run on one or many services on a particular Docker Compose network. Kubernetes on the other hand is intended to run containers as service over one or many machines, virtual or real.
Understanding Kubernetes
As mentioned above, Kubernetes is a container orchestration technology. Under Kubernetes, a web application’s logic is segmented into containers. Containers are organized into an abstraction called a pod. A pod can have one or many containers. A pod’s logic is exposed to the network by way of another Kubernetes abstraction called a service. In short, the network knows about Kubernetes services and a service knows about the pod(s) that has its logic. Within each pod is one or many containers that realize the logic in the given pod. (See Figure 2, below)
Under Kubernetes, containers, pods, and services are hosted within a collection of one or many computers, real or virtual. In Kubernetes parlance, a computer is called a node. Kubernetes runs over a number of nodes. The collection of nodes is called a Kubernetes cluster.
Pods and the containers associated with a service can be hosted over a number of machines. Also, Kubernetes has scaling and fault-tolerance features that make it so Kubernetes can automatically create more pods at runtime to meet increased demand with no loss of service. Also, if a pod fails, Kubernetes will automatically replenish it. Docker Compose supports automatic restarts on failure but is not intended to support auto-scaling.
The important thing to understand about the difference between Docker Compose is that while under Docker Compose, a container and its corresponding service and network run on a single machine, under Kubernetes containers are organized according to an abstraction called a pod. A pod can be used by one or many services and pods associated with a single service are usually distributed over a number of machines.