Deploy Docker Container To Kubernetes

Deploy Docker Container To Kubernetes

Reading time1 min
#Docker#Kubernetes#Containers#Deployment#DevOps

Mastering the Transition: Efficiently Deploy Docker Containers to Kubernetes Clusters

Most developers either overcomplicate or oversimplify Docker-to-Kubernetes deployments. This guide cuts through the noise to deliver a practical, no-nonsense approach that balances Kubernetes power without drowning in complexity.

As Kubernetes solidifies its position as the de facto standard for container orchestration, knowing how to seamlessly deploy your Docker containers into Kubernetes clusters is no longer optional — it’s essential. Whether you’re scaling a growing startup, ensuring reliability for critical apps, or streamlining operational workflows, mastering this transition unlocks flexibility and resilience for your applications.

In this post, we’ll walk step-by-step through deploying a Docker container onto a Kubernetes cluster efficiently and effectively. We’ll keep it practical with clear examples — no fluff, just exactly what you need to get it right.


Why Move From Docker Container Run to Kubernetes?

Running containers locally or in simple environments with docker run is fine for early development or small projects. But as your application grows in users and complexity, you’ll quickly want:

  • Scalability: Automatically spin up more container instances under load
  • Resiliency: Health checks and self-healing replace fragile single instances
  • Load Balancing: Seamless routing of traffic across many pods
  • Declarative Management: Manage desired states through configuration files (YAML manifests)

Kubernetes provides all these out of the box.


Before You Start: Pre-requisites

Make sure you have:

  • A working Docker image ready locally (or pushed to a registry)
  • Access to a Kubernetes cluster (kubectl configured)
  • Basic knowledge of kubectl CLI

If you don’t have a cluster handy, tools like Minikube offer easy local Kubernetes environments perfect for learning.


Step 1: Build Your Docker Image

Suppose you have a simple web app in app.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello from Flask running in Docker on Kubernetes!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Create a Dockerfile next to it:

FROM python:3.9-slim
WORKDIR /app
COPY app.py /app/
RUN pip install flask
CMD ["python", "app.py"]

Build and tag your image:

docker build -t your-dockerhub-username/flask-k8s-demo:v1 .

If you’re using Docker Hub or another registry:

docker push your-dockerhub-username/flask-k8s-demo:v1

Step 2: Create Your Kubernetes Deployment Manifest

Kubernetes uses YAML files to declare applications and their desired state.

Create deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: flask-k8s-demo-deployment
spec:
  replicas: 3                    # Run 3 pods for scaling & redundancy
  selector:
    matchLabels:
      app: flask-k8s-demo
  template:
    metadata:
      labels:
        app: flask-k8s-demo 
    spec:
      containers:
      - name: flask-container
        image: your-dockerhub-username/flask-k8s-demo:v1
        ports:
        - containerPort: 5000       # Match port exposed by Flask app inside container

Step 3: Expose Your Deployment Using a Service

To access your pods externally (or internally), create a service resource.

Add service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: flask-k8s-demo-service
spec:
  type: LoadBalancer              # Use LoadBalancer if on cloud; NodePort or ClusterIP otherwise.
  selector:
    app: flask-k8s-demo          # Matches Deployment pods by label selector.
  ports:
  - protocol: TCP
    port: 80                     # Port exposed by the Service.
    targetPort: 5000             # Port inside container where app listens.

Tip: On Minikube or local clusters without LoadBalancer support, use NodePort instead of LoadBalancer.


Step 4: Deploy to Your Cluster!

Apply both manifests with:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

Check your deployment status:

kubectl get deployments

kubectl get pods

kubectl get services

You should see three running pods under your deployment and the service created.


Step 5 (Optional): Access Your Application

If you used LoadBalancer on a cloud-managed cluster like GKE or EKS, run:

kubectl get service flask-k8s-demo-service 

Look for an external IP under the column EXTERNAL-IP. Visit that address in the browser.

For local Minikube clusters:

minikube service flask-k8s-demo-service --url

Open that URL — you should see your Flask greeting page.


Troubleshooting Tips

  • If pods stay in CrashLoopBackOff, check logs with:

    kubectl logs <pod-name> 
    
  • Make sure Docker image is accessible by your cluster nodes (public registries or private registries with credentials)

  • Validate YAML manifesto correctness with:

    kubectl apply --dry-run=client -f deployment.yaml 
    

Wrapping Up

By progressing from building a Docker image through manifest creation and application deployment, this guide has walked you through deploying Docker containers into Kubernetes efficiently and practically.

Remember these core takeaways when mastering the transition:

  • Always start with a solid Docker image build.
  • Define declarative manifests for everything — deployments, services, configmaps.
  • Use labels properly for smooth pod-service mapping.
  • Know how to expose workloads depending on cluster environment (LoadBalancer vs NodePort).
  • Leverage kubectl commands for smooth operational workflows.

Mastering these steps gives you scalable, resilient apps where Kubernetes does the heavy lifting—and frees you up to focus on adding value higher up the stack.

Ready to level up beyond basics? Look next into Helm charts for templated deployments and advanced concepts like Ingress controllers and autoscaling!


Drop me comments below if this guide helped! What’s been your biggest challenge moving from Docker container workflows into Kubernetes?