OneHub Technical Interview Masterclass: Docker & Kubernetes
See Also: For a dedicated 300-question Kubernetes deep dive, see
../17-kubernetes/01-kubernetes.md. For EKS-specific questions, see../18-eks-kubernetes/01-eks-kubernetes-deep-dive.md. This file covers Docker internals and K8s together.
Total Questions: 48 Curriculum: Core Fundamentals + Applied + Principal (2026 additions: multi-stage builds, HPA, PSS, service mesh, NetworkPolicies)
🏗️ Section 1: Core Fundamentals
Q1: What are Linux Namespaces and Cgroups? How do they enable containerization?
Technical Answer: Namespaces provide isolation: they make a process see only its own resources (PID, Net, Mount, IPC, UTS, User). Cgroups (Control Groups) provide resource limiting: they control how much CPU, memory, and I/O a process can use. Docker combines these kernel features to create an isolated environment (container) that shares the host kernel but behaves like a standalone system.
Code/Syntax Justification:
lsns # List namespaces
cat /sys/fs/cgroup/system.slice/docker-<container_id>.scope/cpu.weight # cgroup v2 (systemd driver)
# cgroup v1 hosts: /sys/fs/cgroup/cpu/docker/<container_id>/cpu.shares
🔍 Behind the Question: Deep container internals knowledge. ❌ The Red Flags: Thinking a container is a "mini VM" with its own kernel.
Q2: Explain the Kubernetes Control Plane components.
Technical Answer:
- kube-apiserver: The front end for the K8s control plane.
- etcd: Consistent, high-availability key-value store for all cluster data.
- kube-scheduler: Watches for new Pods and assigns them to Nodes.
- kube-controller-manager: Runs controller processes (Node, Replication, Endpoints).
- cloud-controller-manager: Links the cluster to your cloud provider's API.
Code/Syntax Justification:
kubectl get pods -n kube-system # See control plane components
🔍 Behind the Question: Distributed system management and K8s architecture. ❌ The Red Flags: Not knowing where cluster state is stored (etcd).
Q3: What is the difference between a Docker image and a Docker container?
Technical Answer: An image is a read-only template made of filesystem layers and metadata. A container is a running instance of that image with a writable layer, process namespace, and runtime state. In interview terms, the image is the blueprint; the container is the executed process set.
Code/Syntax Justification:
docker pull nginx:latest
docker run -d --name web nginx:latest
docker ps
docker inspect web
🔍 Behind the Question: Testing whether the candidate understands immutable infrastructure and runtime isolation. ❌ The Red Flags: Saying the container is a VM or that images are mutable at runtime.
Q4: How do Kubernetes Deployments manage rollout and rollback of application versions?
Technical Answer: A Deployment declares the desired state for Pods and ReplicaSets. When the pod template changes, Kubernetes creates a new ReplicaSet and gradually shifts traffic by scaling the new ReplicaSet up while scaling the old one down. Rollback reverts to a previous ReplicaSet revision. This allows zero-downtime updates when readiness probes are configured correctly.
Code/Syntax Justification:
kubectl set image deployment/api api=myrepo/api:v2
kubectl rollout status deployment/api
kubectl rollout undo deployment/api
🔍 Behind the Question: Testing rollout strategy, revision history, and operational safety. ❌ The Red Flags: Not mentioning readiness probes or confusing Deployment with StatefulSet.
Q5: What problem do Kubernetes Services solve?
Technical Answer: Pods are ephemeral and their IPs can change, so a Service provides a stable virtual IP and DNS name to expose a logical set of Pods. The Service selects Pods using labels and load balances traffic among healthy endpoints. This decouples consumers from the lifecycle of individual Pods.
Code/Syntax Justification:
apiVersion: v1
kind: Service
metadata:
name: api-svc
spec:
selector:
app: api
ports:
- port: 80
targetPort: 8000
🔍 Behind the Question: Testing understanding of service discovery and stable networking. ❌ The Red Flags: Assuming clients talk directly to Pod IPs.
Q6: What is a Kubernetes readiness probe and why does it matter?
Technical Answer: A readiness probe determines whether a Pod should receive traffic. A container can be running but not yet ready to serve requests because it is warming caches, loading models, or waiting for dependencies. If readiness fails, Kubernetes removes the Pod from Service endpoints without restarting it. This prevents user-facing errors during startup or degradation.
Code/Syntax Justification:
readinessProbe:
httpGet:
path: /health/ready
port: 8000
initialDelaySeconds: 5
periodSeconds: 10
🔍 Behind the Question: Testing production traffic gating and health semantics. ❌ The Red Flags: Confusing readiness with liveness.
Q7: What is the purpose of Kubernetes liveness probes?
Technical Answer: Liveness probes answer a different question: is the container still alive and not stuck? If a liveness probe fails repeatedly, Kubernetes restarts the container. This is useful for deadlocks, crash loops, or applications that stop making forward progress. Unlike readiness, liveness is about recovery, not traffic routing.
Code/Syntax Justification:
livenessProbe:
httpGet:
path: /health/live
port: 8000
initialDelaySeconds: 15
periodSeconds: 20
🔍 Behind the Question: Testing reliability and self-healing behavior. ❌ The Red Flags: Using liveness for dependency checks like database connectivity.
Q8: What is a multi-stage Docker build and why use it?
Technical Answer: A multi-stage build copies only what you need into the final image. Earlier stages compile/build; the final stage starts FROM a slim runtime image and COPY --from=builder the artifacts. This keeps images small (less attack surface, faster pull) and eliminates build tools from production images.
Code/Syntax Justification:
FROM python:3.12-slim AS builder
COPY requirements.txt .
RUN pip install --prefix=/install -r requirements.txt
FROM python:3.12-slim
COPY /install /usr/local
COPY app /app
CMD ["python", "/app/main.py"]
🔍 Behind the Question: Image size, security, best practices. ❌ The Red Flags: Shipping compiler/toolchains in prod images.
Q9: How do you secure container images?
Technical Answer: Start from minimal base images (distroless/slim), use multi-stage builds, run as non-root user, pin base image digests, scan images in CI (Trivy/Grype), and sign images with cosign. Avoid installing dev tools; keep only runtime deps. This limits CVEs and attacker tools inside containers.
🔍 Behind the Question: Supply chain security and CVE management. ❌ The Red Flags: Running as root with bare ubuntu + full toolchains.
Q10: What is the difference between a Deployment and a StatefulSet?
Technical Answer: Deployments manage stateless pods with interchangeable replicas (shared storage/DB external). StatefulSets give each pod stable identity, stable network name, ordered scaling, and persistent volume per replica — for databases, queues, or anything needing sticky identity. StatefulSets are heavier; prefer Deployments unless state in the pod is required.
🔍 Behind the Question: Choosing workload types by state requirements. ❌ The Red Flags: Putting DBs in plain Deployments with shared volumes.
Q11: What is the difference between an Ingress and a Service?
A Service exposes pods inside the cluster (ClusterIP) or externally (LoadBalancer/NodePort) with stable DNS. An Ingress is an API object that routes HTTP/HTTPS traffic from outside to Services — one entry point with path/host rules, TLS termination, and load balancing. Services = connectivity; Ingress = routing policy.
🔍 Behind the Question: K8s networking layers. ❌ The Red Flags: Confusing Ingress with a Service; no Ingress Class understanding.
Q12: How does Horizontal Pod Autoscaling (HPA) work?
HPA watches a metric (CPU/memory/custom) and scales to desiredReplicas = ceil(currentReplicas × currentMetricValue / targetMetricValue). It reads from the Metrics API (metrics-server for resource metrics) and adjusts Deployment replicas. Combine with VPA (suggests pod resource requests) and cluster autoscaler (adds nodes) for full elasticity. Add HPA on request rate via Prometheus-adapter for app-level scaling.
🔍 Behind the Question: Autoscaling design, metrics sources. ❌ The Red Flags: Autoscaling on CPU only with bursty traffic.
Q13: What are Pod Security Standards (PSS)?
Pod Security Standards define three profiles — privileged (unrestricted), baseline (blocks known privilege escalations) and restricted (hardened: non-root, dropped capabilities, seccomp) — covering privilege, host namespaces, capabilities and seccomp. They are enforced by the built-in Pod Security Admission controller, configured per namespace with labels such as pod-security.kubernetes.io/enforce: restricted. PodSecurityPolicy was removed in Kubernetes 1.25. Apply restricted in prod unless an explicit reason not to.
🔍 Behind the Question: Pod hardening, admission control. ❌ The Red Flags: Allowing privileged containers by default.
Q14: What is a Service Mesh (Istio/Linkerd)?
A service mesh adds a sidecar proxy to each pod that handles mTLS, traffic routing, retries, timeouts, circuit breaking, and observability — without changing app code. The control plane (e.g. Istiod) pushes config to the data-plane proxies (Envoy in Istio; Linkerd uses its own Rust proxy). Trade-off: added latency, complexity, and resource overhead; adopt when you need policy/mTLS/gradual rollout across many services.
🔍 Behind the Question: Service-to-service security and traffic management. ❌ The Red Flags: Adding a mesh to every app regardless of size.
Q15: What is the difference between readiness and startup probes?
Readiness: when ready to receive traffic (removed from Service endpoints). Liveness: when alive (restart if stuck). Startup: when app initialization completes — used to protect liveness from killing slow-starting containers. Have distinct paths; misconfiguring them causes cascading restarts or traffic loss.
🔍 Behind the Question: Kubernetes health semantics. ❌ The Red Flags: One probe for everything; liveness failing on external deps.
Q16: How do you handle application config and secrets in K8s?
ConfigMaps for non-sensitive config (env var or mounted volume); Secrets for sensitive data (base64 is only encoding, not encryption; Secrets are stored unencrypted in etcd unless encryption at rest, ideally with a KMS provider, is configured, and access must be limited with RBAC). Mount secrets as files to avoid env dump leaks; use external secret operators (External Secrets/Helm secrets) with Vault/AWS Secrets Manager to keep YAML secret-free.
🔍 Behind the Question: Config management and secret hygiene. ❌ The Red Flags: Committing secrets as plaintext in YAML.
Q17: How do you roll out a new image version with zero downtime?
Use a Deployment with rollingUpdate (maxSurge/maxUnavailable): K8s starts new pods, waits for readiness, then terminates old ones. Report readiness based on real health checks. For risky changes: canary (separate Deployment/Argo Rollouts) or blue-green (label switch). Rollback = kubectl rollout undo or revert image tag.
🔍 Behind the Question: Deploy strategies and availability. ❌ The Red Flags: Recreate strategy; readiness not tied to app health.
Q18: What are NetworkPolicies?
NetworkPolicies define allowed ingress/egress traffic between pods at L3/L4 (pod selector + port rules). Requires a CNI that enforces them (Calico, Cilium, Weave). Default-deny + explicit allow is the secure baseline. They matter because cluster-internal traffic is otherwise open between all pods.
🔍 Behind the Question: Zero-trust networking inside the cluster. ❌ The Red Flags: Believing NetworkPolicies block traffic without a policy engine.
📦 Section 2: Images, Layers & Builds
Q19: How do image layers and the build cache work? How should you order Dockerfile instructions?
Technical Answer: Each filesystem-changing instruction (RUN, COPY, ADD) produces a read-only layer; instructions like ENV, LABEL, EXPOSE and CMD only change image metadata. Layers are content-addressed and stacked with a union filesystem (overlay2), and identical layers are shared between images on disk and in registries.
During a build, each instruction is a cache lookup:
RUN: cache hit if the parent layer and the command string are identical (Docker does not know whetherapt-get updatewould return something new).COPY/ADD: cache hit only if the checksums of the copied files are unchanged.- Once one instruction misses, every instruction after it is rebuilt.
So order from least to most frequently changing: base image, OS packages, dependency manifests, dependency install, then source code. Also remember that deleting a file in a later layer does not shrink the image; the bytes remain in the earlier layer. Clean up in the same RUN that created the files.
Code/Syntax Justification:
FROM python:3.12-slim
WORKDIR /app
# Changes rarely: OS packages, cleaned up in the same layer
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq5 \
&& rm -rf /var/lib/apt/lists/*
# Changes sometimes: dependency manifest, then install
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Changes on every commit: source code last
COPY . .
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
docker history myapp:latest # layers, sizes, and the instruction that created each
docker build --no-cache -t myapp . # force a full rebuild
🔍 Behind the Question: Whether the candidate can make builds fast and images small by understanding cache invalidation rather than memorising rules.
❌ The Red Flags: COPY . . before installing dependencies; believing RUN rm in a later step reduces image size.
Q20: What is the difference between CMD and ENTRYPOINT, and between exec form and shell form?
Technical Answer:
ENTRYPOINTdefines the executable the container always runs.CMDprovides default arguments (or a default command if there is noENTRYPOINT). Arguments after the image name indocker runreplaceCMD; replacingENTRYPOINTrequires--entrypoint.- Exec form (
["executable", "arg"], JSON array) runs the binary directly as PID 1, so it receives signals such as SIGTERM. - Shell form (
CMD executable arg) runs as/bin/sh -c "executable arg". Depending on the shell,shmay stay as PID 1 and not forward SIGTERM to your app, sodocker stopwaits for the timeout and then sends SIGKILL. Shell form also means a shell must exist in the image (not true for distroless or scratch).
A common pattern is an entrypoint script that prepares the environment and then hands over with exec "$@".
Code/Syntax Justification:
FROM python:3.12-slim
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
#!/bin/sh
# docker-entrypoint.sh
set -e
if [ "$RUN_MIGRATIONS" = "true" ]; then
python manage.py migrate --noinput
fi
exec "$@" # replace the shell with CMD so the app becomes PID 1
docker run myapp # runs gunicorn via the entrypoint
docker run myapp python manage.py shell # replaces CMD, entrypoint still runs
docker run --entrypoint sh -it myapp # replaces ENTRYPOINT for debugging
🔍 Behind the Question: Signal handling and image ergonomics; this connects directly to graceful shutdown.
❌ The Red Flags: Shell-form CMD for long-running services; entrypoint scripts without exec.
Q21: What is the build context and why does .dockerignore matter?
Technical Answer: docker build . sends the context directory (.) to the builder. Anything in it can be referenced by COPY and, with COPY . ., ends up in the image. Without a .dockerignore:
- Builds are slower (large
node_modules,.git, datasets get transferred). - The cache busts on irrelevant changes (any edit inside
.gitchanges the checksum ofCOPY . .). - Secrets leak:
.env, cloud credentials, SSH keys and local databases get baked into a layer. - Host-built artifacts (for example
node_modulescompiled for macOS) overwrite the ones built inside the image.
.dockerignore uses glob patterns and supports ! exceptions.
Code/Syntax Justification:
# .dockerignore
.git
.gitignore
**/node_modules
**/__pycache__
*.log
.env
.env.*
!.env.example
coverage/
dist/
Dockerfile*
docker-compose*.yml
.vscode/
.idea/
🔍 Behind the Question: Security and build performance hygiene.
❌ The Red Flags: No .dockerignore at all; relying on .gitignore (Docker does not read it).
Q22: How do you choose a base image: full, slim, Alpine, distroless or scratch?
Technical Answer:
| Base | Contents | Good for | Watch out for |
|---|---|---|---|
Full (e.g. python:3.12) | Debian plus compilers and many tools | Build stages | Large, many CVEs to triage |
Slim (e.g. python:3.12-slim) | Minimal Debian plus the runtime | Default runtime for most apps | Need to add a few libs yourself |
| Alpine | musl libc and BusyBox, very small | Static or simple apps | musl differences: some prebuilt wheels/binaries do not work, DNS and performance behaviour can differ from glibc |
Distroless (gcr.io/distroless/...) | Runtime and its libraries only; no shell or package manager | Hardened production images | Debugging needs ephemeral debug containers or the :debug variants |
scratch | Empty | Static binaries (Go with CGO_ENABLED=0, Rust with musl) | You must add CA certificates, timezone data and a non-root user yourself |
Fewer packages means a smaller attack surface, fewer CVEs to triage, and faster pulls. A common production pattern is a full image as the build stage and a slim or distroless image as the runtime stage.
Code/Syntax Justification:
# Go service: build with full toolchain, ship on distroless static
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/server ./cmd/server
FROM gcr.io/distroless/static-debian12:nonroot
COPY /out/server /server
USER nonroot:nonroot
ENTRYPOINT ["/server"]
🔍 Behind the Question: Trade-offs between size, compatibility, security and debuggability.
❌ The Red Flags: "Always use Alpine" without knowing about musl; shipping ubuntu:latest with build tools.
Q23: How do BuildKit cache mounts and build secrets work?
Technical Answer: BuildKit (the default builder in current Docker releases) adds RUN --mount options:
--mount=type=cache,target=...keeps a directory (package manager cache) between builds without storing it in the image layer. Dependency installs reuse downloaded packages even when the layer cache misses.--mount=type=secret,id=...exposes a secret file only for the duration of thatRUNstep (by default at/run/secrets/<id>). It is never written to a layer or the image history.--mount=type=sshforwards the host's SSH agent, for cloning private repositories without copying keys.
Code/Syntax Justification:
# syntax=docker/dockerfile:1
FROM node:20-slim AS deps
WORKDIR /app
COPY package.json package-lock.json ./
# Private registry token is available only during this RUN
RUN
\
npm ci
# syntax=docker/dockerfile:1
# Python equivalent: pip's download cache survives between builds
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN \
pip install -r requirements.txt
docker build --secret id=npmrc,src="$HOME/.npmrc" -t web .
# Share the layer cache between CI runs through the registry
docker buildx build \
--cache-from type=registry,ref=registry.example.com/web:buildcache \
--cache-to type=registry,ref=registry.example.com/web:buildcache,mode=max \
-t registry.example.com/web:1.4.2 --push .
🔍 Behind the Question: Modern build optimisation and keeping credentials out of images.
❌ The Red Flags: Passing tokens via ARG or ENV; thinking cache mounts end up in the final image.
Q24: What is the difference between COPY and ADD, and between ARG and ENV?
Technical Answer:
COPYcopies files from the build context (or another stage with--from). Predictable; use it by default.ADDdoes the same plus two implicit behaviours: it auto-extracts local tar archives, and it can fetch remote URLs. The implicit behaviour surprises people, so useADDonly when you want tar extraction; for downloads, preferRUN curl ...with checksum verification.ARGis a build-time variable, set with--build-arg. It is not present in the running container, but its values can appear indocker historywhen used inRUN, so it is not a secret store. AnARGdeclared beforeFROMis only usable inFROMlines unless re-declared inside the stage.ENVsets an environment variable that persists into the image and the running container.
Code/Syntax Justification:
ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}-slim
ARG APP_VERSION=dev # build-time only
ENV APP_ENV=production \
PYTHONUNBUFFERED=1 # present at runtime
LABEL org.opencontainers.image.version=$APP_VERSION
COPY requirements.txt /app/
ADD vendor/assets.tar.gz /app/static/ # tar is extracted into /app/static/
docker build --build-arg APP_VERSION=1.4.2 -t api:1.4.2 .
docker run --rm api:1.4.2 env | grep -E 'APP_' # APP_ENV is set; APP_VERSION is not
🔍 Behind the Question: Precision about build-time versus runtime configuration.
❌ The Red Flags: Passing passwords through ARG; using ADD for plain copies.
Q25: How do you build images for both amd64 and arm64?
Technical Answer: Use docker buildx to build a multi-platform image: a manifest list (image index) that points to one image per platform. The runtime pulls the variant that matches the node's architecture. Two ways to produce the per-platform images:
- Emulation (QEMU): simple but slow for compile-heavy builds.
- Cross-compilation: run the build stage on the builder's native platform (
$BUILDPLATFORM) and compile for the target (TARGETOS,TARGETARCH). Much faster for Go, Rust or Java.
Running an image built for the wrong architecture fails with exec format error, which is common when images are built on Apple Silicon laptops and deployed to amd64 servers.
Code/Syntax Justification:
# syntax=docker/dockerfile:1
FROM golang:1.22 AS build
ARG TARGETOS
ARG TARGETARCH
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12:nonroot
COPY /out/app /app
ENTRYPOINT ["/app"]
docker buildx create --use --name multi
docker buildx build --platform linux/amd64,linux/arm64 \
-t registry.example.com/app:1.4.2 --push .
# Verify which platforms the pushed tag contains
docker buildx imagetools inspect registry.example.com/app:1.4.2
# Check the architecture of a local image
docker image inspect --format '{{.Os}}/{{.Architecture}}' registry.example.com/app:1.4.2
🔍 Behind the Question: Real-world portability now that arm64 is common in laptops and cloud instances.
❌ The Red Flags: Not recognising exec format error; building only on a laptop and pushing to production.
⚙️ Section 3: Runtime, Networking & Storage
Q26: What is the "PID 1 problem" in containers and how do you solve it?
Technical Answer: The container's main process runs as PID 1 inside its PID namespace. PID 1 is special:
- Signals: the kernel does not apply the default action (terminate) for signals that PID 1 has no handler for. An app that does not explicitly handle SIGTERM therefore ignores
docker stop, and Docker sends SIGKILL after the timeout (10 seconds by default). - Zombie reaping: orphaned processes are re-parented to PID 1, which must
wait()on them. Most apps do not, so zombies accumulate if the app spawns subprocesses.
Fixes:
- Use exec-form
CMD/ENTRYPOINT(andexecin entrypoint scripts) so the app itself is PID 1 and receives signals. - Handle SIGTERM in the app: stop accepting work, drain, exit.
- Add a minimal init (
tini, ordocker run --init, which injects Docker's bundled tini) that forwards signals and reaps zombies. - Avoid wrappers that do not forward signals reliably (for example running
npm startinstead ofnode server.js).
Code/Syntax Justification:
FROM node:20-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends tini \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY . .
USER node
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["node", "server.js"]
// server.js: graceful shutdown on SIGTERM
const server = app.listen(8080); // app is an Express application
process.on('SIGTERM', () => {
server.close(() => process.exit(0)); // stop accepting, finish in-flight
setTimeout(() => process.exit(1), 25000).unref(); // hard deadline below the grace period
});
docker run --init myapp # alternative: let Docker inject tini
docker stop -t 30 myapp # SIGTERM, wait up to 30s, then SIGKILL
docker inspect --format '{{.State.ExitCode}}' myapp # 137 = SIGKILLed, 143 = exited on SIGTERM
🔍 Behind the Question: Graceful shutdown and correct process semantics, which directly affect zero-downtime deploys.
❌ The Red Flags: "Docker sends SIGKILL immediately"; no SIGTERM handling; shell-form CMD.
Q27: What is the difference between volumes, bind mounts and tmpfs mounts?
Technical Answer:
| Type | Where the data lives | Managed by | Typical use |
|---|---|---|---|
| Named volume | Docker's storage area (/var/lib/docker/volumes/... on Linux) | Docker (docker volume ...) | Database data, persistent app state |
| Bind mount | Any host path you specify | You | Local development (live source code), host config files |
| tmpfs | Host memory, never written to disk | Kernel | Scratch space, sensitive temporary data, read-only rootfs helpers |
Details interviewers probe:
- A new, empty named volume mounted over a directory is pre-populated with that directory's contents from the image; a bind mount simply hides what was in the image.
- Bind mounts depend on host paths and host file ownership (UID/GID mismatches cause "permission denied").
- The container's writable layer is not for persistent data: it is deleted with the container and copy-on-write makes it slow for heavy writes.
--mountis the explicit syntax; with-v, a missing host path is silently created as a directory, while--mount type=bindfails if the source does not exist.
Code/Syntax Justification:
# Named volume for Postgres data
docker volume create pgdata
docker run -d --name db -e POSTGRES_PASSWORD=devpass \
--mount type=volume,src=pgdata,dst=/var/lib/postgresql/data postgres:16
# Bind mount for local development (read-only)
docker run --rm -it --mount type=bind,src="$(pwd)/src",dst=/app/src,readonly myapp
# tmpfs for scratch space with a size cap
docker run --rm --read-only --tmpfs /tmp:rw,size=64m myapp
docker volume ls
docker volume inspect pgdata
🔍 Behind the Question: Data persistence and the ephemeral nature of containers.
❌ The Red Flags: Storing database files in the container's writable layer; believing docker rm keeps container data.
Q28: Explain Docker networking modes.
Technical Answer:
- bridge (default): containers get a private IP on a virtual bridge (
docker0) and reach the outside through NAT. Publishing with-p 8080:80adds port-forwarding rules. - User-defined bridge: created with
docker network create. Adds built-in DNS so containers resolve each other by name, and better isolation. Docker Compose creates one per project, which is why services can reach each other by service name. The default bridge does not provide name resolution. - host: the container shares the host's network namespace; no isolation and no port mapping, but no NAT overhead. Port conflicts with host processes become possible.
- none: only a loopback interface.
- container:
<name>: share another container's network namespace (the same idea as containers in a Kubernetes Pod sharinglocalhost). - overlay: spans multiple Docker hosts (Swarm). macvlan / ipvlan: give containers addresses on the physical network.
Inside a container, localhost means the container itself, not the host.
Code/Syntax Justification:
docker network create appnet
docker run -d --name db --network appnet -e POSTGRES_PASSWORD=devpass postgres:16
docker run -d --name api --network appnet -e DATABASE_HOST=db -p 8080:8080 myapi
docker run --rm --network appnet busybox nslookup db # resolves via Docker's embedded DNS
docker network inspect appnet
# Publish only on the host's loopback interface (not reachable from other machines)
docker run -d -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD=devpass postgres:16
# Debug from inside another container's network namespace
docker run --rm -it --network container:api nicolaka/netshoot ss -ltnp
🔍 Behind the Question: Service discovery and network isolation basics, a precursor to Kubernetes networking.
❌ The Red Flags: Connecting containers by hard-coded IP addresses; using localhost to reach another container.
Q29: How does the HEALTHCHECK instruction work, and who acts on it?
Technical Answer: HEALTHCHECK tells Docker to run a command inside the container periodically. Exit code 0 means healthy, 1 means unhealthy. After --retries consecutive failures the status becomes unhealthy; --start-period gives slow-starting apps a grace window during which failures do not count.
Who uses the status:
docker psanddocker inspectshow it.- Docker Compose can wait for it with
depends_on: condition: service_healthy. - Docker Swarm replaces unhealthy tasks.
- Plain
docker rundoes not restart an unhealthy container by itself. - Kubernetes ignores Docker
HEALTHCHECK; it uses its own liveness, readiness and startup probes.
The check needs a binary inside the image (curl, wget, or the app's own health subcommand); distroless images have no shell, so use exec form with a binary you ship.
Code/Syntax Justification:
FROM python:3.12-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
COPY . /app
WORKDIR /app
HEALTHCHECK \
CMD curl -fsS http://localhost:8000/healthz || exit 1
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
docker ps # STATUS column shows (healthy) / (unhealthy)
docker inspect --format '{{json .State.Health}}' api
🔍 Behind the Question: Health semantics and knowing which orchestrator acts on which signal.
❌ The Red Flags: Assuming Kubernetes uses HEALTHCHECK; health endpoints that check every downstream dependency.
Q30: How do container resource limits work, and how do runtimes see them?
Technical Answer: Docker sets limits through cgroups:
--memory: hard memory limit. Exceeding it triggers the kernel OOM killer inside the container's cgroup; the container exits with code 137 andState.OOMKilled=true.--memory-swap: memory plus swap. Setting it equal to--memorydisables swap for the container.--cpus: CFS quota (for example1.5= 150 ms of CPU time per 100 ms period). Exceeding it throttles the container rather than killing it, which shows up as latency.--cpu-shares: relative weight, only matters under contention.--pids-limit: caps the number of processes/threads (protects against fork bombs and thread leaks).
Runtimes must be container-aware. Modern JVMs read cgroup limits and size the heap from them (tune with -XX:MaxRAMPercentage). Other runtimes (Node's V8 heap, Go's GC, Python worker counts based on CPU count) may need explicit settings (--max-old-space-size, GOMEMLIMIT, an explicit worker count) to match the limit.
Code/Syntax Justification:
docker run -d --name api \
--memory=512m --memory-swap=512m \
--cpus=1.5 \
--pids-limit=256 \
-e JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75.0" \
myorg/api:1.4.2
docker stats --no-stream api
docker inspect --format '{{.State.OOMKilled}} {{.State.ExitCode}}' api
# From inside the container (cgroup v2)
cat /sys/fs/cgroup/memory.max
cat /sys/fs/cgroup/cpu.max
cat /sys/fs/cgroup/cpu.stat # nr_throttled / throttled_usec reveal CPU throttling
🔍 Behind the Question: Understanding that containers are cgroup-limited processes, and the difference between being killed (memory) and being throttled (CPU). ❌ The Red Flags: Running production containers with no limits; not knowing what exit code 137 means.
Q31: How is a container different from a virtual machine?
Technical Answer:
| Aspect | Container | Virtual machine |
|---|---|---|
| Isolation boundary | Kernel namespaces, cgroups, seccomp, capabilities | Hypervisor and hardware virtualisation |
| Kernel | Shares the host kernel | Runs its own guest kernel |
| Startup | Milliseconds to seconds (it is a process) | Seconds to minutes (boot an OS) |
| Size | MBs to hundreds of MBs | GBs |
| Density | Many per host | Fewer per host |
| Security | Weaker boundary; a kernel exploit can affect the host | Stronger boundary |
| OS flexibility | Must match host kernel type (Linux containers need a Linux kernel; Docker Desktop runs a Linux VM on macOS and Windows) | Any OS |
For untrusted multi-tenant workloads, teams add stronger isolation: sandboxed runtimes such as gVisor, or micro-VMs such as Firecracker and Kata Containers, which combine container ergonomics with a VM boundary.
Code/Syntax Justification:
uname -r # host kernel version
docker run --rm alpine uname -r # same kernel version: containers share it
docker run --rm alpine ps # the container sees only its own processes
🔍 Behind the Question: Fundamental mental model and security implications. ❌ The Red Flags: "A container is a lightweight VM with its own kernel"; assuming containers are a security boundary as strong as VMs.
Q32: How should containers handle logging, and how do you stop logs filling the disk?
Technical Answer: Containers should log to stdout/stderr, ideally as structured JSON, and let the platform collect logs. Docker's logging driver captures the streams. The default json-file driver writes to a file per container on the host and, unless configured, does not rotate, so a chatty container can fill /var/lib/docker. Options:
- Configure
max-sizeandmax-fileforjson-file, or use thelocaldriver (rotates by default, more compact). - Ship logs to a central system (Fluent Bit, Vector, or a driver such as
awslogs,gelf,fluentd). - Daemon-level log options apply only to containers created after the change.
Code/Syntax Justification:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "5"
}
}
# /etc/docker/daemon.json holds the JSON above; then restart the daemon
sudo systemctl restart docker
# Per container override
docker run -d --log-opt max-size=10m --log-opt max-file=3 myapp
docker logs --since 10m --tail 100 -f api
docker inspect --format '{{.LogPath}}' api # where the json-file log lives on the host
🔍 Behind the Question: Twelve-factor logging and operational hygiene on hosts. ❌ The Red Flags: Writing log files inside the container filesystem; no rotation on Docker hosts.
🔐 Section 4: Container Security & Supply Chain
Q33: Why and how should containers run as non-root? What else hardens a container at runtime?
Technical Answer: By default, a container's process runs as root (UID 0). Without user namespace remapping, that is the same UID 0 as on the host, restricted only by dropped capabilities, seccomp and namespaces. If an attacker escapes the container (kernel bug, misconfigured mount, privileged flag), root inside means root outside. Running as a non-root user removes a large class of escalations.
Layered runtime hardening:
USERwith a fixed numeric UID/GID in the Dockerfile.- Drop all Linux capabilities and add back only what is needed.
no-new-privilegesso setuid binaries cannot raise privileges.- Read-only root filesystem, with
tmpfsfor scratch directories. - Never use
--privilegedor mount/var/run/docker.sockfor application containers. - Keep the default seccomp profile; consider rootless Docker or user namespace remapping on shared hosts.
Code/Syntax Justification:
FROM python:3.12-slim
RUN groupadd --system --gid 10001 app \
&& useradd --system --uid 10001 --gid app --no-create-home app
WORKDIR /app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
USER 10001:10001
EXPOSE 8000
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
docker run -d --name api \
--read-only --tmpfs /tmp:rw,size=64m \
--cap-drop=ALL \
--security-opt no-new-privileges:true \
-p 8000:8000 myorg/api:1.4.2
docker exec api id # uid=10001(app) gid=10001(app)
Note: non-root processes cannot bind ports below 1024 without CAP_NET_BIND_SERVICE, so listen on a high port (8000, 8080) and map it.
🔍 Behind the Question: Defence in depth and least privilege.
❌ The Red Flags: --privileged to "fix permissions"; mounting the Docker socket into app containers.
Q34: What are image scanning, SBOMs and image signing, and how do they fit into CI?
Technical Answer:
- Vulnerability scanning matches the packages in an image (OS packages and language dependencies) against CVE databases. Tools: Trivy, Grype, Docker Scout, and registry-native scanners. Fail the build on fixable HIGH/CRITICAL findings rather than on every CVE, or the gate gets ignored.
- SBOM (Software Bill of Materials) is a machine-readable inventory of everything in the image, in SPDX or CycloneDX format (generated by Syft, Trivy, or BuildKit). When the next major CVE lands, you can query stored SBOMs to see which images are affected without re-pulling them all.
- Signing and provenance: sign images (Sigstore cosign) and attach build provenance attestations, then verify signatures at deploy time (for example with an admission controller in Kubernetes) so only images built by your pipeline can run.
- Images are scanned at build time and rescanned on a schedule, because new CVEs are published against images that have not changed.
Code/Syntax Justification:
# Scan and fail CI on fixable high/critical issues
trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 registry.example.com/api:1.4.2
# Generate an SBOM and scan the SBOM later without the image
syft registry.example.com/api:1.4.2 -o spdx-json > sbom.spdx.json
grype sbom:./sbom.spdx.json
# BuildKit can attach SBOM and provenance attestations at build time
docker buildx build --sbom=true --provenance=true \
-t registry.example.com/api:1.4.2 --push .
# Sign by digest (not tag), then verify before deploy
IMAGE=$(docker inspect --format '{{index .RepoDigests 0}}' registry.example.com/api:1.4.2)
cosign sign --key cosign.key "$IMAGE"
cosign verify --key cosign.pub "$IMAGE"
🔍 Behind the Question: Software supply chain security maturity. ❌ The Red Flags: Scanning once and never again; a gate that fails on every unfixable low-severity CVE so teams bypass it.
Q35: Why pin images by digest instead of tag? What is wrong with :latest?
Technical Answer: Tags are mutable pointers: python:3.12-slim today and next week can be different images, and anyone with push rights can move myapp:1.4.2. A digest (@sha256:...) is the content hash of the image manifest and is immutable. :latest is just a default tag name; it does not mean "newest" and gives no reproducibility or rollback target.
Good practice:
- Deploy by immutable reference: unique tags (git SHA or semver) plus digests in the deployment manifest.
- Pin base images by digest for reproducible builds, and let a bot (Renovate or Dependabot) open PRs when the digest changes, so you still get security patches.
- Enable tag immutability in the registry where supported.
Code/Syntax Justification:
# Find the digest behind a tag
docker buildx imagetools inspect python:3.12-slim
docker inspect --format '{{index .RepoDigests 0}}' python:3.12-slim
# Tag kept for readability; the digest is what is actually resolved
FROM python:3.12-slim@sha256:4f1c0a7e2b9d8c6f5e3a1b0c9d8e7f6a5b4c3d2e1f0a9b8c7d6e5f4a3b2c1d0e
The digest above is illustrative; use the value printed by the inspect command.
🔍 Behind the Question: Reproducibility, rollback safety and supply chain integrity.
❌ The Red Flags: Deploying :latest to production; re-pushing the same version tag with different content.
Q36: Why is mounting /var/run/docker.sock into a container dangerous? What are the alternatives?
Technical Answer: The Docker socket is the Docker Engine API. Any process that can talk to it can start a container with --privileged, mount the host's / and get root on the host. Mounting it into a CI job, a monitoring agent or a web app is equivalent to giving that container host root.
Alternatives:
- Building images in CI: use rootless builders: BuildKit in rootless mode, Kaniko, or Buildah, or a remote BuildKit service.
- Rootless Docker / Podman: the daemon and containers run under an unprivileged user, so an escape lands as that user.
- Monitoring: read cgroup and
/procdata or use runtime APIs with read-only, scoped access instead of the full socket. - If you must expose the API, put an authorising proxy in front that allows only specific endpoints.
Code/Syntax Justification:
# Why it is root-equivalent: from a container that has the socket mounted
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock docker:cli \
docker run --rm --privileged -v /:/host alpine chroot /host id
# prints uid=0(root) for the HOST
🔍 Behind the Question: Whether the candidate recognises a common, high-impact misconfiguration.
❌ The Red Flags: "It's fine, the container is internal"; Docker-in-Docker with --privileged on shared CI runners.
🛠️ Section 5: Local Development & the Kubernetes Bridge
Q37: How do you use Docker Compose effectively for local development?
Technical Answer: Compose describes a multi-container app (API, database, cache, queue) in one file. Practices that make it work well:
- Use
healthcheckplusdepends_on: condition: service_healthyso the API waits for the database to be ready, not just started. - Keep production-like settings in
compose.yamland developer conveniences (bind mounts, debug ports) incompose.override.yaml, which Compose merges automatically. - Use profiles for optional services (admin tools, mock services).
- Use
develop.watch(withdocker compose watch) or bind mounts for fast feedback without rebuilding. - Services reach each other by service name on the project network;
localhostinside a container is that container.
Code/Syntax Justification:
# compose.yaml
services:
api:
build: .
ports:
- "8080:8080"
environment:
DATABASE_URL: postgres://app:devpass@db:5432/app
REDIS_URL: redis://cache:6379/0
depends_on:
db:
condition: service_healthy
cache:
condition: service_started
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: rebuild
path: package.json
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: devpass
POSTGRES_DB: app
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 10
cache:
image: redis:7
adminer:
image: adminer
ports:
- "8081:8080"
profiles: ["tools"]
volumes:
pgdata:
docker compose up -d --build
docker compose --profile tools up -d # include optional services
docker compose watch # sync/rebuild on file changes
docker compose logs -f api
docker compose down -v # also delete named volumes (wipes the DB)
🔍 Behind the Question: Developer experience and parity between local and production environments.
❌ The Red Flags: sleep 30 in entrypoints to wait for the database; committing real credentials in compose files.
Q38: How do Docker concepts map to Kubernetes when you move a container to a cluster?
Technical Answer: The image is the same; the runtime contract changes:
| Docker | Kubernetes Pod spec |
|---|---|
ENTRYPOINT | command (overrides it) |
CMD | args (overrides it) |
-e / --env-file | env / envFrom (ConfigMap, Secret) |
--memory, --cpus | resources.limits (plus resources.requests for scheduling, which Docker has no direct equivalent for) |
HEALTHCHECK | Ignored; use livenessProbe, readinessProbe, startupProbe |
docker stop -t 10 (default 10 s) | terminationGracePeriodSeconds (default 30 s), optional preStop hook |
USER, --cap-drop, --read-only | securityContext (runAsNonRoot, capabilities.drop, readOnlyRootFilesystem) |
| Named volume | PersistentVolumeClaim |
--restart=always | Controllers (Deployment) recreate Pods; restartPolicy restarts containers |
The same failure signals carry over: an OOM-killed container shows Reason: OOMKilled and exit code 137 in kubectl describe pod.
Code/Syntax Justification:
apiVersion: v1
kind: Pod
metadata:
name: api
spec:
terminationGracePeriodSeconds: 30
securityContext:
runAsNonRoot: true
runAsUser: 10001
containers:
- name: api
image: registry.example.com/api:1.4.2
args: ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
resources:
requests:
cpu: "250m"
memory: "256Mi"
limits:
memory: "512Mi"
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
readinessProbe:
httpGet:
path: /healthz
port: 8000
volumeMounts:
- name: tmp
mountPath: /tmp # writable scratch space despite the read-only rootfs
volumes:
- name: tmp
emptyDir: {}
For deeper Kubernetes coverage see the dedicated Kubernetes and EKS guides linked at the top of this page.
🔍 Behind the Question: Whether the candidate can carry container knowledge into an orchestrator without surprises.
❌ The Red Flags: Expecting HEALTHCHECK to work in Kubernetes; not knowing that command replaces ENTRYPOINT.
🔥 Section 6: Senior & Principal Real-World Scenarios
Q39: Every deploy causes a burst of 502s because containers ignore SIGTERM and get killed. How do you fix it?
Level: Senior
Scenario: A Node.js API is rolled out several times a day. Each rollout produces a spike of 502/connection-reset errors. docker stop (or a Pod deletion) always takes exactly the full grace period, and the old containers exit with code 137.
Impact: User-visible errors on every deploy; teams start deploying less often to avoid them.
Root Cause:
- The Dockerfile used
CMD npm start(shell form, plus npm as a wrapper). The process tree wassh -c→npm→node; SIGTERM reached PID 1 (the shell), which did not forward it, so the app never started shutting down. - Even when the signal arrived, the app had no SIGTERM handler, and as PID 1 an unhandled SIGTERM is ignored.
- After the grace period the runtime sent SIGKILL, dropping in-flight requests and keep-alive connections.
- In Kubernetes there is an additional race: the Pod receives SIGTERM while load balancers and kube-proxy may still be routing new requests to it for a short time.
Fix:
FROM node:20-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends tini \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
USER node
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["node", "server.js"] # exec form, no npm wrapper
// server.js
const express = require('express');
const app = express();
let shuttingDown = false;
app.get('/healthz', (req, res) => res.status(shuttingDown ? 503 : 200).end());
const server = app.listen(8080);
process.on('SIGTERM', () => {
shuttingDown = true; // readiness fails: stop receiving new traffic
setTimeout(() => {
server.close(() => process.exit(0)); // finish in-flight requests, then exit
server.closeIdleConnections?.(); // close idle keep-alive sockets (newer Node versions)
}, 5000); // give load balancers time to deregister
setTimeout(() => process.exit(1), 25000).unref(); // hard stop before the grace period ends
});
# Kubernetes: keep the grace period longer than drain delay + longest request
spec:
terminationGracePeriodSeconds: 30
containers:
- name: api
image: registry.example.com/api:1.4.3
readinessProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 2
# Verify locally: should exit quickly with code 0 or 143, not 137 after 10s
docker run -d --name api-test registry.example.com/api:1.4.3
docker top api-test # tini should be the parent of node
time docker stop api-test
docker inspect --format '{{.State.ExitCode}}' api-test
Workflow Diagram:
Before After
────── ─────
SIGTERM ─► sh (PID 1) ─╳ not forwarded SIGTERM ─► tini (PID 1) ─► node (handler)
│ │
10s/30s grace mark not-ready, wait for LB to drain
│ │
SIGKILL ─► in-flight requests dropped server.close(): finish in-flight
│ │
exit 137, 502s at the LB exit 0 well before the deadline
Interviewer Notes: Strong candidates trace the process tree, explain the PID 1 signal rule, and handle the endpoint-removal race with a short drain delay (in Kubernetes often a preStop sleep or a readiness flip). Principal-level: standardise a shutdown contract in a shared base image or service template and test it in CI by sending SIGTERM to a container under load.
Red Flags: Increasing the grace period without fixing signal delivery; kill -9 as normal shutdown; not knowing exit code 137 versus 143.
Q40: Our image is 2 GB and deploys and autoscaling are slow. How do you shrink it?
Level: Senior
Scenario: A Python ML-adjacent API image is 2.1 GB. New nodes take minutes to pull it, so autoscaling reacts too late during traffic spikes, and CI spends a long time pushing it.
Impact: Slow scale-out causes errors at peak; slower rollbacks; higher registry egress and storage costs; a larger CVE surface.
Root Cause: Investigation with docker history and dive shows:
- Base image
python:3.12(full Debian with compilers) instead ofslim. build-essentialand headers installed for compiling wheels, left in the final image.- pip cache and apt lists left in layers.
COPY . .brought in.git, test data and a local virtualenv (no.dockerignore).- A 300 MB model file downloaded in one layer and deleted in a later layer, so it still ships.
Fix:
# syntax=docker/dockerfile:1
FROM python:3.12 AS build
WORKDIR /app
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY requirements.txt .
RUN \
pip install -r requirements.txt
FROM python:3.12-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends libgomp1 \
&& rm -rf /var/lib/apt/lists/*
RUN useradd --system --uid 10001 --no-create-home app
COPY /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
WORKDIR /app
COPY src/ ./src/
USER 10001
CMD ["gunicorn", "-b", "0.0.0.0:8000", "src.app:app"]
# Find where the bytes are
docker history --no-trunc --format '{{.Size}}\t{{.CreatedBy}}' myorg/api:old | sort -h | tail
dive myorg/api:old # interactive layer explorer, shows wasted space
docker image ls myorg/api # compare sizes before and after
Also: add a .dockerignore, load large models from object storage or a volume at startup (or bake them into a separate, rarely changing base layer), keep stable layers first so nodes reuse cached layers between versions, and consider image pre-pulling or lazy-loading snapshotters for very large images.
Workflow Diagram:
docker history / dive ─► rank layers by size
│
├─ base image too big ───────► slim / distroless runtime stage
├─ build tools in final ─────► multi-stage: compile in builder, copy venv/artifacts
├─ caches & apt lists ───────► cache mounts, clean in same RUN
├─ junk in context ──────────► .dockerignore
└─ big data files ───────────► fetch at runtime / separate stable layer
│
▼
CI gate: fail if image size grows > N% versus main
Interviewer Notes: Look for evidence-driven analysis (history/dive) instead of guessing, an understanding that deleted files in later layers still ship, and multi-stage builds. Principal-level: treat image size as a tracked metric with a CI budget, and connect image size to autoscaling latency and cost.
Red Flags: Switching to Alpine blindly for a Python app with compiled dependencies (musl wheels may be missing and builds can get slower and bigger); docker export | docker import squashing as the main strategy, losing layer sharing.
Q41: Every CI build reinstalls all dependencies even when only one source file changed. Why?
Level: Senior
Scenario: A Node service's CI build takes 9 minutes, 7 of them in npm ci, on every commit, including one-line changes.
Impact: Slow feedback loops, expensive CI minutes, and queued deploys during incidents.
Root Cause: Several cache busters stacked up:
COPY . .came beforeRUN npm ci, so any source change invalidated the install layer.- An
ARG GIT_SHAwas declared near the top of the Dockerfile and changed on every commit; a changed build arg causes cache misses on theRUNinstructions after its declaration. - CI runs on ephemeral runners, so there was no local layer cache at all, and nothing imported a remote cache.
- No
.dockerignore, so even reordering would not help:.gitchanges on every commit.
Fix:
# syntax=docker/dockerfile:1
FROM node:20-slim AS deps
WORKDIR /app
COPY package.json package-lock.json ./ # only manifests first
RUN npm ci
FROM deps AS build
COPY . .
RUN npm run build
FROM node:20-slim
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY /app/dist ./dist
USER node
# Per-commit metadata LAST, so it only affects this final metadata
ARG GIT_SHA=unknown
LABEL org.opencontainers.image.revision=$GIT_SHA
CMD ["node", "dist/server.js"]
# Ephemeral runners: import/export the layer cache from the registry
docker buildx build \
--cache-from type=registry,ref=registry.example.com/web:buildcache \
--cache-to type=registry,ref=registry.example.com/web:buildcache,mode=max \
--build-arg GIT_SHA="$(git rev-parse HEAD)" \
-t registry.example.com/web:"$(git rev-parse --short HEAD)" --push .
On GitHub Actions, type=gha is an alternative cache backend for --cache-from/--cache-to.
Workflow Diagram:
Commit changes src/x.ts
│
▼
FROM ─► COPY package*.json (unchanged: HIT) ─► RUN npm ci (HIT) ─► COPY . . (MISS) ─► build ─► ...
▲
only layers from here down are rebuilt
Remote cache (registry/gha) ─► provides HITs even on a brand-new runner
Interviewer Notes: Candidates should explain cache-key semantics per instruction type, the "everything after a miss rebuilds" rule, build-arg effects, and why ephemeral runners need an external cache. mode=max (caching intermediate stages too) is a nice detail.
Red Flags: "Just use --no-cache to be safe"; putting timestamps or SHAs in early instructions; not knowing CI runners start with an empty cache.
Q42: Containers keep getting OOMKilled (exit code 137) even though the app "only uses 300 MB". What is happening?
Level: Senior
Scenario: A Java service with a 1 GiB container memory limit restarts several times a day with exit code 137 and OOMKilled: true. The JVM heap dashboards never show more than about 300-600 MB of heap used.
Impact: Random request failures and restart loops under load.
Root Cause: The container limit applies to all memory charged to the cgroup, not just the heap:
- Heap was set with
-Xmx1g, equal to the container limit, leaving no room for non-heap memory: metaspace, thread stacks (hundreds of threads), code cache, direct/NIO buffers, GC structures, and native libraries. - Under load the heap grew toward 1 GiB while non-heap used another 250 MB; the total crossed the limit and the kernel OOM killer killed the JVM. The JVM itself never threw
OutOfMemoryError, which is why application logs were silent. - The same pattern hits Node (V8 heap plus buffers), Python (many worker processes in one container), and Go (GC target not aware of the limit unless
GOMEMLIMITis set).
Fix:
# Confirm it is a container OOM kill, not an app crash
docker inspect --format '{{.State.OOMKilled}} {{.State.ExitCode}}' orders
journalctl -k | grep -i 'memory cgroup out of memory' # on the host
# Kubernetes equivalent: Last State shows Reason: OOMKilled, Exit Code: 137
kubectl describe pod -l app=orders | grep -A4 'Last State'
FROM eclipse-temurin:21-jre
COPY target/orders.jar /app/orders.jar
# Size heap relative to the container limit, leaving headroom for non-heap memory
ENV JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=70.0 -XX:+ExitOnOutOfMemoryError"
USER 10001
CMD ["java", "-jar", "/app/orders.jar"]
# Inspect non-heap usage in a running JVM. Requires -XX:NativeMemoryTracking=summary at startup,
# and jcmd ships with the JDK, so use a JDK-based image (or a debug image) when diagnosing.
docker exec orders jcmd 1 VM.native_memory summary
# Watch real cgroup usage, not just heap
docker stats orders
Then right-size: measure peak working set under load testing, set the limit with headroom, cap thread pools, and alert on memory usage approaching the limit (for example above 85%) before kills happen.
Workflow Diagram:
Container limit 1 GiB
┌───────────────────────────────────────────────┐
│ Heap (-Xmx1g!) ███████████████████████████████│██████ ◄─ heap + non-heap > limit
│ Metaspace / threads / direct buffers / GC ████│ ─► kernel OOM kill, exit 137
└───────────────────────────────────────────────┘
Fix: heap ≈ 70% of limit ─► non-heap fits ─► GC works harder instead of being killed
Interviewer Notes: Strong answers distinguish a JVM OutOfMemoryError from a kernel cgroup OOM kill, know where to find each, and understand non-heap memory. Principal-level: memory budgets per runtime in a shared base image, load-test-driven limits, and dashboards comparing working set with limits across the fleet.
Red Flags: Setting -Xmx equal to the container limit; "just double the memory" without measuring; confusing exit code 137 with an application error.
Q43: A security review found database credentials inside a published image. How did it happen and how do you respond?
Level: Senior / Principal
Scenario: An external researcher reports that docker history on your public image shows a production database password, and an old layer contains an .env file, even though the Dockerfile has RUN rm .env.
Impact: Credential compromise; anyone who pulled the image (including all its old tags) has the secret. Potential data breach and disclosure obligations.
Root Cause:
ARG DB_PASSWORDandENV DB_PASSWORD=$DB_PASSWORDwere used to pass the secret at build time.ENVvalues are stored in the image config and visible indocker inspect; build args used byRUNappear indocker history.COPY . .copied.envinto a layer. The laterRUN rm .envcreated a new layer that hides the file, but the original layer still contains it and anyone can extract it withdocker save.- No
.dockerignore, and no secret scanning in CI.
Fix:
Respond first, then prevent:
- Rotate the credential immediately. Assume it is compromised; deleting the image does not undo pulls.
- Review database access logs for use of the leaked credential.
- Delete or overwrite affected tags in the registry, and check mirrors and caches.
- Rebuild without the secret, using BuildKit secret mounts for build-time needs and runtime injection (orchestrator secrets, a secrets manager) for runtime needs.
# How the leak is found (and how attackers find it)
docker history --no-trunc myorg/api:1.3.0 | grep -i password
docker inspect --format '{{json .Config.Env}}' myorg/api:1.3.0
docker save myorg/api:1.3.0 -o api.tar && mkdir layers && tar -xf api.tar -C layers
# each layer is a tarball inside layers/; the .env file is still in one of them
# syntax=docker/dockerfile:1
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
# Build-time secret (private package index token): never stored in a layer
RUN \
PIP_INDEX_URL="$(cat /run/secrets/pip_index_url)" \
pip install --no-cache-dir -r requirements.txt
COPY src/ ./src/
USER 10001
CMD ["gunicorn", "-b", "0.0.0.0:8000", "src.app:app"]
# Runtime secrets (DB password) come from the environment or mounted files at deploy time
docker build --secret id=pip_index_url,env=PIP_INDEX_URL -t myorg/api:1.3.1 .
Prevention: .dockerignore excluding .env* and key files; secret scanning on the repository and on built images in CI; and if you export the build cache with mode=max, remember intermediate stages are stored in the cache too, so secrets must never be in any stage's layers.
Workflow Diagram:
Leak reported
│
├─► 1. rotate credential (minutes, not days)
├─► 2. audit access logs for misuse
├─► 3. purge tags / caches / mirrors
└─► 4. rebuild clean ──► secret mounts (build) + runtime injection (run)
│
▼
CI gates: .dockerignore, secret scan of repo + image layers, review
Interviewer Notes: The first word should be "rotate". Strong candidates explain why rm in a later layer does not remove data, distinguish build-time from runtime secrets, and know --mount=type=secret. Principal-level: organisation-wide secret scanning, short-lived credentials (workload identity) so leaked values expire quickly, and an incident process.
Red Flags: "Delete the image and we're fine"; squashing layers as the fix while keeping the secret in ENV; passing secrets via --build-arg.
Q44: An image works on a developer's laptop but crashes in production with "exec format error". What happened?
Level: Senior
Scenario: A developer on an Apple Silicon laptop builds a hotfix image locally and pushes it to the registry to unblock a release. In production, every new container exits immediately; logs show exec /usr/local/bin/app: exec format error.
Impact: The hotfix deploy fails, and if old replicas were already replaced, the service goes down.
Root Cause: docker build on an arm64 machine produces a linux/arm64 image by default. Production nodes are linux/amd64. The kernel cannot execute binaries for a different CPU architecture, so it returns ENOEXEC. The process was also flawed: manual pushes from laptops bypassed CI, which builds for the correct platforms.
Fix:
# Diagnose: what architecture is the pushed tag?
docker buildx imagetools inspect registry.example.com/api:1.4.4-hotfix
docker image inspect --format '{{.Os}}/{{.Architecture}}' registry.example.com/api:1.4.4-hotfix
uname -m # on the production node: x86_64 = amd64, aarch64 = arm64
# Immediate fix: rebuild for the right platform (or both)
docker buildx build --platform linux/amd64,linux/arm64 \
-t registry.example.com/api:1.4.5 --push .
# syntax=docker/dockerfile:1
# Cross-compile fast instead of emulating the whole build
FROM golang:1.22 AS build
ARG TARGETOS
ARG TARGETARCH
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12:nonroot
COPY /out/app /usr/local/bin/app
ENTRYPOINT ["/usr/local/bin/app"]
Process fixes: only CI may push to production repositories (registry permissions), CI builds multi-platform images or the exact platform production uses, and deploys reference digests produced by CI.
Workflow Diagram:
Laptop (arm64) ── docker build ──► image linux/arm64 ──push──► registry ──pull──► node (amd64)
│
exec format error
CI ── buildx --platform linux/amd64,linux/arm64 ──► manifest list ──► node pulls matching variant
Interviewer Notes: Recognising exec format error instantly is a good signal. Strong answers fix both the artifact (multi-platform build) and the process (no laptop pushes). Principal-level: arm64 nodes can be cheaper, so multi-platform images also enable cost optimisation.
Red Flags: Debugging the application code; enabling QEMU emulation on production nodes as the fix.
Q45: CI runners and Docker hosts keep running out of disk space. How do you diagnose and fix it?
Level: Senior
Scenario: Self-hosted CI runners fail builds with no space left on device every few days. On a long-lived Docker host, /var/lib/docker has grown to 180 GB.
Impact: Failed builds and deploys; on application hosts, a full disk can take down every container on the node.
Root Cause: Docker never cleans up automatically:
- Every build leaves old images (dangling
<none>images after re-tagging) and build cache. - Stopped containers and their writable layers remain until removed.
- Anonymous volumes from test containers accumulate.
json-filelogs grow without bound unless rotation is configured; one chatty container can write tens of GB.
Fix:
# Where is the space going?
docker system df
docker system df -v | head -50
sudo du -sh /var/lib/docker/* 2>/dev/null | sort -h
sudo sh -c 'du -h /var/lib/docker/containers/*/*-json.log' | sort -h | tail
# Immediate relief for one huge container log (keeps the container running)
sudo truncate -s 0 "$(docker inspect --format '{{.LogPath}}' noisy-worker)"
# Targeted cleanup, oldest first
docker container prune -f --filter "until=24h"
docker image prune -a -f --filter "until=168h"
docker builder prune -f --filter "until=72h"
docker volume prune -f # anonymous, unused volumes only (by default on current releases)
{
"log-driver": "local",
"log-opts": {
"max-size": "20m",
"max-file": "5"
}
}
The JSON above goes in /etc/docker/daemon.json (restart the daemon; it applies to newly created containers). Then schedule cleanup (a systemd timer or a cron job running the prune commands), alert on /var/lib/docker usage, consider putting /var/lib/docker on its own volume so a full Docker disk cannot fill the root filesystem, and prefer ephemeral CI runners that are destroyed after each job.
Workflow Diagram:
Disk alert on Docker host
│
▼
docker system df ──► Images? ──► image prune -a --filter until=...
│ ──► Build cache? ──► builder prune --filter until=...
│ ──► Containers/volumes? ──► container / volume prune
└────────────► none of these? ──► du containers/*-json.log ──► truncate + rotation config
│
▼
Prevention: log rotation, scheduled prune, separate volume, ephemeral runners, disk alerts
Interviewer Notes: Look for a measured approach (docker system df first), awareness that container logs are a major hidden consumer, and that daemon log settings affect only new containers. Principal-level: ephemeral, autoscaled runners with remote build cache remove the problem rather than managing it.
Red Flags: rm -rf /var/lib/docker/... while the daemon is running; docker system prune -a --volumes on a host with stateful containers without checking what it deletes.
Q46: A container slowly accumulates hundreds of zombie processes and eventually cannot fork. Why?
Level: Senior
Scenario: A PDF rendering service shells out to a headless browser for each request. After a day, requests fail with fork: Resource temporarily unavailable / spawn EAGAIN. docker top shows hundreds of <defunct> processes.
Impact: The service stops processing jobs; restarts temporarily fix it, so it looks like a "memory leak" and gets misdiagnosed.
Root Cause: The Node app runs as PID 1. The browser launches helper processes; when the browser's main process exits, its helpers are orphaned and re-parented to PID 1. Node does not reap arbitrary children it did not spawn, so they remain zombies. Each zombie holds a PID, and the container's --pids-limit (or the Pod's PID limit) is eventually exhausted.
Fix:
# Confirm: many Z-state processes (docker top runs ps on the host, so it works
# even though slim images do not ship ps; PIDs shown are host PIDs)
docker top renderer -eo pid,ppid,stat,comm | awk '$3 ~ /^Z/' | head
docker top renderer -eo stat | grep -c '^Z'
docker inspect --format '{{.HostConfig.PidsLimit}}' renderer
# Quick fix without rebuilding: Docker injects tini as PID 1
docker run -d --init --name renderer --pids-limit 512 myorg/renderer:2.3.0
FROM node:20-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends tini chromium \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY . .
RUN npm ci --omit=dev
USER node
ENTRYPOINT ["/usr/bin/tini", "--"] # reaps zombies and forwards signals
CMD ["node", "server.js"]
In Kubernetes there is no --init flag: bake tini (or another minimal init such as dumb-init) into the image, or set shareProcessNamespace: true on the Pod so the pause container becomes PID 1 and reaps zombies (this also lets containers in the Pod see each other's processes, which has security implications).
Workflow Diagram:
Without init With tini as PID 1
──────────── ───────────────────
node (PID 1) tini (PID 1) ── reaps orphans
└─ chromium ─ exits └─ node
└─ helpers ─► orphaned ─► PID 1 └─ chromium ─ exits
never reaped └─ helpers ─► orphaned ─► tini ─► wait() ─► gone
│
▼
zombies accumulate ─► pids limit ─► fork EAGAIN
Interviewer Notes: Seniors should connect "zombie" to "parent not calling wait()", and know that PID 1 in a container inherits init's duties. Principal-level: make a minimal init part of the standard base image for any service that spawns subprocesses, and alert on process counts per container.
Red Flags: "Kill the zombies" (they are already dead); raising the PID limit as the fix; restarting on a cron schedule.
Q47: The API container cannot reach the database, and the published port does not respond from the host. How do you debug it?
Level: Senior
Scenario: A new service works with npm run dev on the laptop. Inside Docker Compose it logs ECONNREFUSED 127.0.0.1:5432, and curl localhost:8080 from the host returns "connection reset" / "empty reply" even though docker ps shows 0.0.0.0:8080->8080/tcp.
Impact: Lost developer time and, when the same assumptions reach production, outages.
Root Cause: Two network-namespace misunderstandings:
- The app's
DATABASE_HOST=localhostcame from the laptop config. Inside a container,localhostis the container's own loopback, not the host and not thedbcontainer. - The dev server listened on
127.0.0.1:8080inside the container. Port publishing forwards traffic to the container's network interface, not its loopback, so nothing answers. The app must listen on0.0.0.0.
A related production trap: ports published with -p 8080:8080 bind on all host interfaces, and Docker's own firewall rules can bypass host firewalls such as UFW, exposing services you thought were private.
Fix:
# compose.yaml
services:
api:
build: .
environment:
DATABASE_HOST: db # service name, resolved by Compose's DNS
HOST: 0.0.0.0 # app binds all interfaces inside the container
ports:
- "127.0.0.1:8080:8080" # reachable from this host only
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: devpass
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
retries: 10
# What is the app actually listening on? (use a debug container sharing the namespace)
docker run --rm --network container:"$(docker compose ps -q api)" nicolaka/netshoot ss -ltnp
# Can the api container resolve and reach db?
docker compose exec api getent hosts db
docker run --rm --network container:"$(docker compose ps -q api)" nicolaka/netshoot nc -zv db 5432
# Reaching a service on the host machine from a container
# Docker Desktop: host.docker.internal works out of the box
# Linux engine: add the mapping explicitly
docker run --rm --add-host=host.docker.internal:host-gateway alpine ping -c1 host.docker.internal
Workflow Diagram:
Host Container "api" (own netns) Container "db"
──── ─────────────────────────── ──────────────
curl :8080 ──► NAT ──► eth0:8080 ──► app bound to 127.0.0.1? ─╳
│ fix: bind 0.0.0.0 ──► ✔
│
app ─► localhost:5432 ─╳ (its own loopback)
app ─► db:5432 ──► Docker DNS ──► db eth0:5432 ✔
Interviewer Notes: Strong candidates reason in terms of network namespaces: each container has its own loopback, ports are published to the container interface, and Compose provides DNS by service name. Bonus points for the published-port firewall bypass and for binding to 127.0.0.1 on the host side for local-only services.
Red Flags: network_mode: host everywhere to "make it work"; hard-coding container IP addresses; not knowing why 127.0.0.1 inside a container is unreachable from outside.
Q48: A critical CVE hits a base image used by 200 services. How do you remediate across the fleet?
Level: Principal
Scenario: A critical, remotely exploitable vulnerability is published in a system library (for example OpenSSL or glibc) present in your common base images. Security asks within the hour: which running services are affected, and when will they all be patched?
Impact: Exposure across the fleet, compliance deadlines, and pressure to patch quickly without breaking production.
Root Cause: Not a single bug but an organisational gap: teams each chose their own base images and tags, images were rebuilt only when application code changed, and there was no inventory of what is inside each running image. Answering "where is this library?" would require pulling and scanning hundreds of images by hand.
Fix: Build the capability before the incident, then use it:
- Inventory: generate an SBOM for every image in CI and store it alongside the image. Query SBOMs (or the registry's scanner results) to list affected images, then map digests to what is actually running.
- Golden base images: a platform team publishes a small set of hardened base images, rebuilt on a schedule and immediately on critical CVEs.
- Automated propagation: Renovate or Dependabot opens PRs to bump the base image digest in every repository; CI rebuilds, tests and deploys automatically for low-risk changes.
- Enforcement: admission policy (for example Kyverno or Sigstore policy-controller in Kubernetes) blocks images that are unsigned or older than a threshold, and rescans flag running images with new critical CVEs.
- Track to completion: a dashboard of affected services versus patched, with owners.
# Find affected images from stored SBOMs (one SBOM file per image digest)
for sbom in sboms/*.spdx.json; do
if grype "sbom:$sbom" --only-fixed -o json \
| jq -e '.matches[] | select(.vulnerability.severity == "Critical")' > /dev/null; then
echo "AFFECTED: $sbom"
fi
done
# Rebuild pulling the patched base layer, then verify the fix before rollout
docker buildx build --pull -t registry.example.com/orders:1.9.3 --push .
trivy image --severity CRITICAL --exit-code 1 registry.example.com/orders:1.9.3
{
"extends": ["config:recommended", "docker:pinDigests"],
"packageRules": [
{
"matchDatasources": ["docker"],
"matchPackageNames": ["registry.example.com/base/python"],
"automerge": true
}
]
}
The JSON above is a renovate.json that pins Docker base images by digest and auto-merges updates to the internal golden base image once CI passes.
Workflow Diagram:
CVE published
│
▼
Platform: rebuild golden base images ──► scan ──► sign ──► push new digest
│
▼
SBOM query ──► list affected images ──► map to running workloads + owners
│
▼
Renovate PRs bump base digest in each repo ──► CI build + test ──► auto-deploy (low risk)
│ └─► manual review (high risk)
▼
Admission policy blocks old/unsigned digests ──► dashboard: patched vs remaining ──► done
Interviewer Notes: Principal candidates think in systems: inventory (SBOMs), a small number of owned base images, automation for propagation, and enforcement. They also address risk: canary the rebuilt images, because a base image bump can change library behaviour. Mentioning that distroless or minimal images reduce how often this happens is a plus.
Red Flags: "Each team should check their images"; patching running containers in place with apt-get upgrade via docker exec; no way to know what is running in production.