How to Use Docker and Kubernetes Together: A Practical Guide

Docker packages applications into portable containers, while Kubernetes (K8s) orchestrates those containers across a cluster. Used together, you get consistent development environments and automated deployment, scaling, and management in production. This tutorial walks through the essential workflow of combining both tools.

Before diving in, ensure you have Docker Desktop (with Kubernetes enabled) or a local cluster like Minikube installed. The core idea: you build and test containers with Docker, then deploy them using Kubernetes YAML manifests.

Article illustration

1. Understand Each Tool’s Role

Docker is the container runtime—it builds images from a Dockerfile and runs single containers. Kubernetes is the orchestration layer—it manages groups of containers (pods), handles networking, load balancing, and self-healing. They complement each other: Docker creates the units, Kubernetes runs them at scale.

2. Build and Push a Docker Image

Start by creating a simple application and a Dockerfile. For example:

  • Write a Node.js app that listens on port 3000.
  • Create a Dockerfile that copies the code and runs npm start.
  • Build the image: docker build -t my-app:v1 .
  • Push to a registry like Docker Hub: docker push myusername/my-app:v1

Your image is now available for Kubernetes to pull.

3. Write a Kubernetes Deployment Manifest

Create a file deployment.yaml that tells Kubernetes how to run your container:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: myusername/my-app:v1
        ports:
        - containerPort: 3000

This defines three replicas of your Docker container.

4. Deploy and Expose Your Application

Apply the manifest with kubectl apply -f deployment.yaml. Kubernetes pulls the Docker image and creates pods. To access the app, expose it via a Service:

  • Create a service.yaml of type LoadBalancer or NodePort.
  • Run kubectl apply -f service.yaml.
  • Get the external IP with kubectl get svc.

Now your Docker-built application runs on a Kubernetes cluster, with automatic scaling and load distribution.

Conclusion

Docker and Kubernetes work hand‑in‑hand: Docker standardises container creation, Kubernetes standardises container orchestration. By mastering this combined workflow—build with Docker, deploy with Kubernetes—you can develop locally and confidently scale to production.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *