Advanced Interview Questions

What are multi-stage builds?

A Dockerfile with several FROM stages. Build tools and sources live in early stages; the final stage copies only the build output (COPY --from=build). Result: small images with a small attack surface.

How do you secure Docker containers?

  • Run as a non-root USER.
  • Use minimal, pinned base images and rebuild them regularly.
  • Scan images (Docker Scout, Trivy) in CI.
  • Never bake secrets into images; inject them at runtime.
  • Drop capabilities, use read-only file systems, avoid --privileged and mounting the Docker socket.
  • Set CPU and memory limits.

What happens when a container exceeds its memory limit?

The kernel's OOM killer stops the process; the container exits with code 137 and docker inspect shows OOMKilled: true. A restart policy or orchestrator may restart it.

How are images built from layers, and why does it matter?

Each instruction creates an immutable, content-addressed layer. Layers are cached between builds and shared between images, which saves disk and network. A change invalidates that layer and every layer after it, which is why instruction order affects build speed.

Docker Compose vs Kubernetes?

ComposeKubernetes
one hostcluster of many nodes
simple YAML, quick to learnmany objects, steeper learning curve
no self-healing across machinesself-healing, autoscaling, rolling updates
local dev, small deploymentsproduction at scale

How would you debug a container that keeps restarting?

  1. docker ps -a: check the exit code (1 = app error, 137 = killed/OOM, 139 = segfault).
  2. docker logs <container>: read the error.
  3. docker inspect: check env vars, mounts, health check and OOMKilled.
  4. Run the image with a shell (docker run -it --entrypoint sh image) to test manually.