Skip to content
Tutorials Center
My learning

Topics

Lesson 54 of 77 ยท DevOps, Tools and System Design

Docker, CI/CD and Cloud Questions

Answer Docker, CI/CD, deployment and Kubernetes interview questions: images vs containers, layers and caching, Compose, pipelines, rollouts and reconcile loops.

DevOps questions test whether you can get code from a laptop to production and keep it running. Interviewers rarely want a list of commands. They want to hear the model behind them: an image is a recipe frozen into layers, a container is a running process with walls around it, a pipeline is a repeatable gate, and Kubernetes is a loop that keeps comparing what you asked for with what exists.

Docker, a CI server and a cluster cannot run inside this site, so the real commands, Dockerfiles, YAML files and pipelines are read-only. Each idea also has a runnable stand-in in JavaScript that models the behavior (layer caching, pipeline stages, a reconcile loop). Be clear in interviews about which part is real: the stand-ins explain the logic, not the products. For the real tools, see the Docker tutorial.

Docker in four words

Dockerfile The recipe.
Image Read-only layers built from the recipe.
Container A running instance of an image.
Registry Where images are stored and shared.
1 / 2

What is the difference between an image and a container?

Short answer: An image is a read-only template: a filesystem snapshot plus metadata (default command, environment, exposed ports). A container is a running (or stopped) instance of an image, with a thin writable layer on top and its own process, network and filesystem view. One image can start many containers, like a class and its objects.

A container is not a virtual machine. It shares the host's kernel and is isolated by Linux features: namespaces (what a process can see: PIDs, network, mounts) and cgroups (how much CPU and memory it can use). That makes containers start in milliseconds and be far lighter than VMs, but the isolation is weaker, because the kernel is shared.

Terminal
# Read-only
docker build -t shop-api:1.0 .        # Dockerfile -> image
docker run -d -p 8080:80 shop-api:1.0 # image -> container
docker ps                              # running containers
docker logs <container>
docker exec -it <container> sh
docker stop <container>

Data written inside a container's writable layer disappears when the container is removed. Persist data with volumes (managed by Docker) or bind mounts (a host folder), and configure with environment variables, not by baking secrets into images.

What are image layers and why does the order of Dockerfile lines matter?

Each instruction that changes the filesystem (RUN, COPY, ADD) creates a layer. Layers are content-addressed and shared between images, so pulling a second image that uses the same base downloads only the layers that differ. During a build, Docker reuses a cached layer if the instruction and its inputs are unchanged; once one layer changes, every layer after it is rebuilt.

That is why the classic optimization is to copy the dependency manifest first and install, then copy the rest of the source:

DOCKERFILE
# Read-only: slow, cache-busting order
FROM node:24-slim
WORKDIR /app
COPY . .
RUN npm ci
CMD ["node", "server.js"]

# Better: dependencies change rarely, source changes constantly
FROM node:24-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
CMD ["node", "server.js"]

In the first version any source edit invalidates COPY . . and therefore reruns npm ci. In the second, npm ci only reruns when the package files change. Other points to mention: use a small base image, add a .dockerignore, run as a non-root USER, and use a multi-stage build so compilers and build tools stay out of the final image.

The stand-in below mimics Docker's cache rule: each layer's cache key is a hash of the previous layer's key plus its own instruction and inputs.

JS Layer cache model: why order matters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is Docker Compose for?

Docker Compose describes a multi-container application in one YAML file: services, networks and volumes, started together with docker compose up. It is ideal for local development and small deployments: your app, a database and a cache in one command. Services reach each other by service name on a shared network (db:5432), not by localhost.

YAML
# Read-only
services:
  web:
    build: .
    ports: ["8080:80"]
    environment:
      DATABASE_URL: postgres://app:secret@db:5432/app
    depends_on:
      db:
        condition: service_healthy
  db:
    image: postgres:17
    volumes: [dbdata:/var/lib/postgresql/data]
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "app"]
volumes:
  dbdata:

Note that depends_on only controls start order; a database being started is not the same as being ready, which is why the healthcheck matters. Compose is not a cluster manager: it runs on one host and has no self-healing across machines.

What is CI/CD?

Continuous Integration means every change is merged often and automatically built and tested, so broken code is found in minutes. Continuous Delivery means every passing build is deployable at any time; Continuous Deployment goes further and ships every passing build to production automatically.

A typical pipeline has stages: lint and unit tests, build the artifact (often a Docker image), integration tests, publish to a registry, deploy to staging, then production. Good answers mention fast feedback (run quick checks first), caching dependencies, keeping secrets in the CI system's secret store, and one artifact promoted through environments rather than rebuilding for each.

YAML
# Read-only: a GitHub Actions-style pipeline
name: ci
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npm test
  publish:
    needs: test          # only runs if tests pass
    runs-on: ubuntu-latest
    steps:
      - run: echo "build and push the image here"
JS A pipeline runner that stops at the first failure

Press Run (or Ctrl + Enter) to see the console output here.

No output.

How do you deploy without downtime, and how do you roll back?

Know these strategies and their trade-offs:

  • Rolling update: replace instances a few at a time. Cheap, but old and new versions run together briefly, so changes must be backward compatible (especially database schemas).
  • Blue-green: run a full second environment ("green"), test it, then switch traffic. Rollback is a switch back, at the cost of double capacity.
  • Canary: send a small percentage of traffic to the new version, watch error rates and latency, then increase it.
  • Feature flags: deploy code switched off and enable it separately from the release.

Database changes follow expand and contract: add the new column first (compatible with old code), deploy code that uses it, backfill, and only later remove the old column. Rolling back code is easy; rolling back data is not.

What is Kubernetes and what are its core objects?

Kubernetes is a system that runs containers across many machines and keeps them in the state you declared. The key idea is declarative desired state with reconciliation: you say "I want 3 replicas of this image"; controllers constantly compare reality to that wish and fix the difference (restart a crashed pod, start a new one on another node).

  • Pod: the smallest unit; one or more containers that share a network address and storage. Pods are disposable.
  • Deployment: manages a set of identical pods, rolling updates and rollbacks.
  • Service: a stable address and load balancing in front of changing pods.
  • Ingress: HTTP routing from outside the cluster to services.
  • ConfigMap and Secret: configuration and sensitive values injected into pods.
  • Node: a machine that runs pods; the control plane (API server, scheduler, controllers, etcd) decides what runs where.
YAML
# Read-only
apiVersion: apps/v1
kind: Deployment
metadata: { name: shop-api }
spec:
  replicas: 3
  selector: { matchLabels: { app: shop-api } }
  template:
    metadata: { labels: { app: shop-api } }
    spec:
      containers:
        - name: api
          image: registry.example.com/shop-api:1.0
          readinessProbe: { httpGet: { path: /health, port: 8080 } }

The readiness probe tells Kubernetes when a pod may receive traffic; a liveness probe tells it when to restart a stuck pod. Mixing them up is a common mistake.

JS A reconcile loop: desired state vs reality

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Quick answers for the follow-ups

  • CMD vs ENTRYPOINT: ENTRYPOINT is the executable; CMD supplies default arguments that docker run can override.
  • COPY vs ADD: prefer COPY; ADD also extracts archives and fetches URLs, which surprises people.
  • Why not latest? The tag moves, so builds are not reproducible. Pin versions or digests.
  • Infrastructure as code: servers and cloud resources described in files (Terraform, CloudFormation) and reviewed like code.
  • IaaS, PaaS, SaaS: you manage the most in IaaS (virtual machines), less in PaaS (you deploy code), and nothing in SaaS (you use the product).
  • Horizontal vs vertical scaling: more machines versus a bigger machine. See system design: the building blocks.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You edit one source file, and Docker reruns the dependency install step. What is the most likely cause?

    A changed layer invalidates every layer after it. Copy the dependency manifest first, install, and copy the rest of the code afterwards.
  2. 2. How does a container differ from a virtual machine?

    Sharing the kernel makes containers start quickly and use little memory, but the isolation boundary is weaker than a VM's.
  3. 3. In Docker Compose, how does the web service usually reach a database service named db?

    Compose creates a network where each service name resolves to its container. localhost inside the web container refers to the web container itself.
  4. 4. What is the main benefit of a canary release?

    A canary gives you real-world feedback with a small blast radius; you widen traffic only if metrics look healthy.
  5. 5. A Kubernetes Deployment asks for 3 replicas and one pod crashes. What happens?

    That is the reconcile loop: controllers compare desired and actual state and act to close the gap.

Summary

  • An image is a read-only layered template; a container is a running instance isolated with namespaces and cgroups, sharing the host kernel.
  • Order Dockerfile instructions from least to most frequently changing so the layer cache works for you; use multi-stage builds and non-root users.
  • Compose runs multi-container apps on one host; CI/CD automates build, test, publish and deploy with fast failure.
  • Rolling, blue-green and canary releases trade cost against risk; database changes use expand and contract.
  • Kubernetes keeps declared state through reconciliation: pods, Deployments, Services, probes.

Next: System Design: The Building Blocks.