Docker vs Docker Compose vs Kubernetes: A Comprehensive Guide

sheak imransheak imran
5 min read

Introduction

In the world of containerization and orchestration, three tools dominate the landscape: Docker, Docker Compose, and Kubernetes. While they're often mentioned together, they serve different purposes in the application deployment lifecycle. This post will explain each technology, compare them, and show when to use each with practical examples.

1. Docker: The Containerization Engine

What is Docker?

Docker is a platform that enables developers to package applications and their dependencies into lightweight, portable containers. These containers run consistently across different environments.

Key Features:

  • Container creation and management

  • Image building and distribution

  • Isolation of applications

  • Cross-platform compatibility

Example: Running a Simple Web App with Docker

Let's run a basic Nginx web server using Docker:

# Pull the Nginx image from Docker Hub
docker pull nginx

# Run the Nginx container
docker run -d -p 8080:80 --name my-nginx nginx

This command:

  1. Downloads the official Nginx image

  2. Runs it in detached mode (-d)

  3. Maps host port 8080 to container port 80

  4. Names the container "my-nginx"

Diagram: How Docker runs a single container

Diagram: How Docker runs a single container

2. Docker Compose: Multi-Container Orchestration

What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file to configure the application's services, networks, and volumes.

Key Features:

  • Single-file multi-container configuration

  • Service dependencies and startup order

  • Simplified networking between containers

  • Environment variable management

Example: WordPress with MySQL using Docker Compose

Create a docker-compose.yml file:

version: '3.8'

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress

volumes:
  db_data:

Run it with:

docker-compose up -d

Diagram: Docker Compose managing multiple connected containers

Diagram: Docker Compose managing multiple connected containers

3. Kubernetes: Production-Grade Container Orchestration

What is Kubernetes?

Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications across clusters of hosts.

Key Features:

  • Automatic scaling

  • Self-healing capabilities

  • Load balancing

  • Rolling updates and rollbacks

  • Storage orchestration

  • Secret and configuration management

Example: Deploying a Web App on Kubernetes

Create a deployment file web-app.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  selector:
    app: web
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

Deploy it with:

kubectl apply -f web-app.yaml

Diagram: Kubernetes managing containers across multiple nodes

Diagram: Kubernetes manages containers across multiple nodes

Comparison Table

FeatureDockerDocker ComposeKubernetes
PurposeSingle container runtimeMulti-container apps on a single hostContainer orchestration across clusters
ScalingManualManualAutomatic
Load BalancingNoneBasicAdvanced
High AvailabilityNoNoYes
Self-healingNoNoYes
ComplexityLowMediumHigh
Best forDevelopment, single servicesLocal multi-service appsProduction deployments

When to Use Each

  1. Use Docker when:

    • You need to run a single container

    • You're developing or testing an application

    • You need a lightweight environment

  2. Use Docker Compose when:

    • Your application has multiple services that need to work together

    • You're developing locally and need to simulate a production environment

    • You want to simplify complex docker run commands

  3. Use Kubernetes when:

    • You need to deploy applications in production

    • You require automatic scaling and high availability

    • Your application needs to run across multiple machines

    • You need advanced features like rolling updates and self-healing

Real-World Example: Application Lifecycle

Let's follow a typical application's journey through these tools:

  1. Development Phase:

    • Developer uses Docker to containerize the application

    • Creates a Dockerfile to build the application image

FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
  1. Local Testing:
  • Uses Docker Compose to set up the full environment with dependencies (database, cache, etc.)
version: '3'
services:
  app:
    build: .
    ports:
      - "3000:3000"
  redis:
    image: redis
  1. Production Deployment:
  • Uses Kubernetes to deploy across a cluster with monitoring, scaling, and high availability
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: app
        image: my-app:1.0
        ports:
        - containerPort: 3000

Conclusion

Docker, Docker Compose, and Kubernetes form a powerful trio in the container ecosystem:

  1. Docker is your foundation - it packages your applications into containers.

  2. Docker Compose builds on Docker to handle multiple containers in development.

  3. Kubernetes takes containers to production at scale.

Understanding when and how to use each tool will make you more effective in developing, testing, and deploying containerized applications. Start with Docker for your individual services, use Compose when you need multiple services working together, and graduate to Kubernetes when you're ready for production-scale deployment.

0
Subscribe to my newsletter

Read articles from sheak imran directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

sheak imran
sheak imran

System Administrator FM Associates BD | Network Specialist | RHCE, RHCSA, RHCVA certified.