Knowledge / Computer Science and Engineering / Containers: Docker and Kubernetes

Containers: Docker and Kubernetes

Computer Science and Engineering (B.E./B.Tech) · Web, cloud and design · free study notes for exams, placements and interviews

In short

A container packages an application with its libraries and runtime into one image that runs identically on a laptop, a test server and a cloud VM, while sharing the host operating system kernel instead of booting its own. Docker is the dominant tool for building and running single containers; Kubernetes is the orchestrator that runs thousands of them across a cluster, restarting, scaling and load-balancing them automatically. Together they are the standard deployment layer at Indian product companies and service firms alike (Flipkart, Swiggy, Zomato, Razorpay, PhonePe, Jio, Infosys, TCS, Wipro, Zoho), and Docker/Kubernetes questions appear in almost every DevOps, backend and SRE interview. University syllabi cover them under cloud computing, virtualisation and DevOps electives; GATE CS touches virtualisation concepts rather than Docker commands. Being able to write a Dockerfile, read a Kubernetes YAML manifest and explain why a pod is in CrashLoopBackOff is a practical hiring filter for freshers.

Explained

Virtualisation, containers and images

Docker: architecture, Dockerfile and commands

Kubernetes architecture and objects

Build-to-deploy pipeline and deployment strategies

  1. Developer pushes code to Git (GitHub/GitLab/Bitbucket).
  2. CI (Jenkins, GitHub Actions, GitLab CI, Azure DevOps) runs tests, then docker build with a tag = commit SHA.
  3. Image is scanned (Trivy, Clair) and pushed to the registry (ECR/ACR/Harbor).
  4. CD updates the image tag in the manifest/Helm values; kubectl apply or Argo CD syncs it.
  5. The Deployment performs a rolling update bounded by maxUnavailable and maxSurge; readiness probes gate each new pod; kubectl rollout undo reverts.
  6. Strategies: Rolling (default, zero downtime, two versions live briefly), Recreate (downtime, single version — needed for incompatible schema), Blue-Green (two full environments, switch the Service selector; instant rollback, double cost), Canary (5% → 25% → 100% traffic, needs a service mesh or weighted ingress), A/B (route by header/cookie).
  7. Observability: Prometheus + Grafana for metrics, Loki/ELK/EFK for logs (containers log to stdout/stderr; the platform collects them), Jaeger/OpenTelemetry for traces, Istio/Linkerd service mesh for mTLS, retries and traffic splitting.
  8. Managed services: Amazon EKS, Azure AKS, Google GKE, plus Indian/sovereign clouds — Jio Cloud, Zoho, E2E Networks, CtrlS and NIC's MeghRaj offer Kubernetes-based platforms. Exact prices change, so quote current provider pricing rather than memorised figures.
  9. Twelve-factor fit: config from the environment, stateless processes, logs as event streams, disposability — containers are the practical embodiment of these rules.

Best practices, security and India-specific compliance

Key points

Formulas and examples

Container density ≈ usable host RAM / (per-container RAM) — compare with VM density where each guest OS also consumes RAM (overhead typically ~0.5-1 GB per VM, ~0 for a container).
Pod capacity per node = min( floor(allocatable CPU / CPU request) , floor(allocatable RAM / RAM request) , node pod limit (default 110) )
allocatable = node capacity − kube-reserved − system-reserved − eviction threshold.
Rolling update: pods running ∈ [replicas − maxUnavailable, replicas + maxSurge]; update batches ≈ ceil(replicas / maxSurge) when maxUnavailable = 0.
HPA desired replicas = ceil( currentReplicas × (currentMetricValue / targetMetricValue) ), clamped to [minReplicas, maxReplicas].
CPU units: 1 core = 1000m (millicores); memory: 1 Mi = 1024² bytes, 1 MB = 10⁶ bytes (Ki/Mi/Gi are binary, K/M/G decimal).

Example 1 — Containers vs VMs on one 64 GB server. A service needs 512 MB RAM per instance. Each VM also needs ~1 GB for its guest OS; assume 4 GB is reserved for the host/hypervisor.

Example 2 — Pods per node and cluster size. A node of 4 vCPU / 16 GiB has allocatable ≈ 3.9 vCPU (3900m) and ≈ 14.5 GiB after reservations. A pod requests 250m CPU and 512 Mi RAM.

Example 3 — Rolling update timeline. A Deployment has 10 replicas, maxSurge: 2, maxUnavailable: 0, and each new pod takes 30 s to pass its readiness probe.

Example 4 — HPA scaling arithmetic. Deployment runs 4 pods; CPU request 200m each; HPA target = 50% average CPU utilisation. Current average usage = 180m per pod.

Example 5 — Image size from a multi-stage build. A Spring Boot service: maven:3.9-eclipse-temurin-21 build image = 780 MB, the fat JAR = 45 MB, runtime base eclipse-temurin:21-jre-alpine = 190 MB.

AspectVirtual machineContainer
Isolation unitFull guest OS + own kernelProcess group, shared host kernel
Start-upTens of seconds to minutesMilliseconds to a few seconds
SizeGBsTens to hundreds of MB
Overhead per instance~0.5-1 GB RAM for the OSNegligible
Isolation strengthStronger (hardware-level)Weaker (kernel shared)
PortabilityOS image, hypervisor-specificOCI image, runs on any Linux host
Typical useMixed/untrusted OS workloads, legacyMicroservices, CI, stateless scaling
NeedDocker aloneDocker ComposeKubernetes
ScopeOne containerMulti-container, one hostMulti-container, many nodes
Self-healingNo (restart policy only)Restart policyReschedules pods, replaces nodes
AutoscalingNoNoHPA / VPA / Cluster Autoscaler
Rolling update + rollbackManualLimitedBuilt into Deployment
Service discoveryContainer name on a user networkService nameService DNS + kube-proxy
Best forLocal build/runDevelopment, CIProduction at scale
# Multi-stage Node.js image
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci                 # cached unless package files change
COPY . .
RUN npm run build

FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --omit=dev && npm cache clean --force
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
HEALTHCHECK CMD wget -qO- http://localhost:3000/health || exit 1
ENTRYPOINT ["node", "dist/server.js"]
# Deployment + Service: the two manifests every interviewer expects
apiVersion: apps/v1
kind: Deployment
metadata: { name: api, namespace: prod }
spec:
  replicas: 3
  strategy:
    rollingUpdate: { maxSurge: 1, maxUnavailable: 0 }
  selector:
    matchLabels: { app: api }
  template:
    metadata:
      labels: { app: api }
    spec:
      containers:
      - name: api
        image: registry.example.in/api@sha256:abc123
        ports: [{ containerPort: 3000 }]
        envFrom: [{ configMapRef: { name: api-config } }]
        env:
        - name: DB_PASSWORD
          valueFrom: { secretKeyRef: { name: db-secret, key: password } }
        resources:
          requests: { cpu: "250m", memory: "512Mi" }
          limits:   { cpu: "500m", memory: "512Mi" }
        readinessProbe:
          httpGet: { path: /ready, port: 3000 }
          initialDelaySeconds: 5
        livenessProbe:
          httpGet: { path: /healthz, port: 3000 }
          periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata: { name: api-svc, namespace: prod }
spec:
  type: ClusterIP
  selector: { app: api }       # must match pod labels, not Deployment name
  ports: [{ port: 80, targetPort: 3000 }]

Where it is used

Interview questions

Common mistakes

Related topics

Cloud computing basics · AWS core services · System design basics for freshers · REST APIs · Back-end development with Node.js · Caching, queues and CDNs

Open in the Knowledge reader →Find jobs →