How To Use Kubernetes With Docker

How To Use Kubernetes With Docker

Reading time1 min
#Cloud#DevOps#Containers#Kubernetes#Docker#Minikube

Mastering Kubernetes with Docker: A Pragmatic Guide to Streamlined Container Orchestration

Launching a container fleet with Docker tools alone? Fine for a handful of stateless workloads—until reliability and scale start to matter. Kubernetes emerged to meet those very needs, but nuances remain between local Docker workflows and robust Kubernetes orchestration.

Below are core methods, proven configurations, and the actual friction points teams face when bridging Docker and Kubernetes in practice.


Docker and Kubernetes: Distinctions That Matter

Docker excels at packaging applications: docker build, docker push, simple lifecycle management. As soon as node failures, rolling updates, or rapid scaling enter the picture, hand-crafted container lifecycles unravel quickly. Kubernetes schedules, heals, and scales these containers—decoupling workloads from specific hosts.

Summary comparison:

CapabilityDocker EngineKubernetes
Container buildYesNo
App deployYes (manual)Declarative
HA / Self-healingNoYes
ScalingManualAutomatic
OrchestrationNoneBuilt-in

Side note: While Docker was once Kubernetes’ primary container runtime, modern clusters often default to containerd for stability and performance. Most developer workflows still rely on Docker CLI and image formats.


Environment Setup: Local and Cloud Options

Critical: Align all component versions. Docker v25.x, kubectl v1.29+, Minikube v1.33+ as of 2024. Incompatibilities yield opaque errors like unknown flag: --docker.

Local Workflow: Minikube using Docker Driver

Local clusters allow rapid iteration and realistic debugging before pushing to cloud providers. Minikube, when run with the Docker driver, integrates the Minikube VM’s Docker daemon with the user’s CLI.

minikube start --driver=docker --kubernetes-version=v1.29.0

kubectl config use-context minikube
docker version   # Confirm v25.x or newer

Note:
Executing eval $(minikube docker-env) redirects Docker commands to Minikube’s internal daemon. This avoids image-pull headaches during testing. However, these local images aren’t accessible to cloud clusters—remember to push to docker.io, GCR, or ECR when moving to production.


Building and Tagging Docker Images

A common pitfall: building with mismatched architectures (x86 on ARM, for example) or forgetting to match image tags in manifests. Using multi-stage builds reduces image footprint—critical for minimizing attack surface.

Sample Production Dockerfile (Node.js 20 on Alpine)

FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev

COPY . .

FROM node:20-alpine
WORKDIR /app
COPY --from=build /app .
CMD ["node", "server.js"]

Build, tag, and push:

docker build -t yourrepo/my-node-app:2024.6.1 .
docker push yourrepo/my-node-app:2024.6.1

Known issue: Tag drift—Kubernetes won’t always detect newly pushed :latest tags due to aggressive image caching. Always increment tags for updated deployments.


Kubernetes Manifests: Deployment and Exposure

Deployment YAML:
Defines the desired state—replica count, container images, resource requirements, and selectors.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-node-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-node-app
  template:
    metadata:
      labels:
        app: my-node-app
    spec:
      containers:
      - name: api
        image: yourrepo/my-node-app:2024.6.1
        ports:
        - containerPort: 3000
        resources:
          limits:
            memory: "256Mi"
            cpu: "500m"

Apply and verify status:

kubectl apply -f deployment.yaml
kubectl rollout status deployment/my-node-app
kubectl logs -l app=my-node-app --tail=50

Service YAML:
Expose via NodePort for local access or LoadBalancer in cloud.

apiVersion: v1
kind: Service
metadata:
  name: my-node-app-svc
spec:
  selector:
    app: my-node-app
  ports:
  - port: 80
    targetPort: 3000
    protocol: TCP
  type: NodePort

Access on Minikube:

minikube service my-node-app-svc

Troubleshooting: Typical Failure Cases

  1. ImagePullBackOff / ErrImagePull
    Example log:

    Failed to pull image "yourrepo/my-node-app:2024.6.1": rpc error: ...
    
    • Image tag doesn’t exist in registry.
    • Registry auth missing (imagePullSecrets misconfigured).
    • Tag typo or using latest with old cache.
  2. Local Images Not Found

    • Forgot to run eval $(minikube docker-env) before building.
    • Minikube reset: local images wiped, must rebuild.
  3. Pods stuck in Pending

    • Node lacks resources or correct labels.
    • Check with kubectl describe pod <podname>.
  4. Network Isolation

    • Direct IP access to pods is blocked; always use a Service.
    • For pod-to-pod reachability, NetworkPolicy can inadvertently restrict traffic.

Quick check:

kubectl get events --sort-by=.metadata.creationTimestamp | tail -n 15

Non-Obvious Workflow Tips

  • CI Pipelines: For automated test and deploy, integrate docker buildx with Kubernetes manifests templated via Helm.
  • Tag Automation: Use Git SHAs or build timestamps as part of the tag to ensure immutable deploys.
  • Skaffold: Speeds up local dev cycles—auto-rebuilds and redeploys when source changes.
  • Resource Quotas: Define minimal CPU/memory for each container up front; avoids overcommit issues on shared clusters.

Summary

Containers are only as useful as the orchestration around them. Kubernetes operationalizes Docker images into production-grade workloads. Key steps—building images suited to your target cluster, tagging precisely, pushing with the right credentials, and ensuring manifests match reality—determine reliability and speed.

Known alternative: Docker Compose with the kompose tool as a transitional bridge—pros and cons, but rarely used for real prod deployments.

Keep local and cloud environments aligned, document every version and parameter, and monitor logs for friction points. Small misalignments (image tag, pod selector, registry credentials) derail deployments more often than big infra failures.

For advanced topics—persistent volumes, rolling updates, blue/green deploys, or StatefulSets—review upstream documentation or request another focused guide.