Opscraft
Docker · Lesson 11 of 11 · 5 min read

Interview questions and model answers

"How would you make a Docker image smaller, faster to build and more secure?"

Smaller: multi-stage builds so compilers and sources stay in the build stage; slim or distroless runtime base; clean package caches in the same RUN; a .dockerignore. Faster: order layers from least to most frequently changing so dependency layers stay cached, BuildKit cache mounts for package managers, and a registry cache (--cache-from/--cache-to) because CI agents start cold. More secure: non-root USER, pinned base images rebuilt regularly, no secrets in layers (BuildKit secret mounts), scanning in CI with a failing threshold, signing and verifying images, and read-only root filesystem at runtime.

"What's the difference between CMD and ENTRYPOINT, and why does exec form matter?"

ENTRYPOINT defines the executable; CMD supplies default arguments that docker run arguments replace. In Kubernetes, command overrides ENTRYPOINT and args overrides CMD. Exec form makes the app PID 1 so it receives SIGTERM directly; shell form makes /bin/sh PID 1, which doesn't forward signals, so the app gets SIGKILLed after the grace period and can't drain connections. For wrapper scripts I end with exec "$@".

"Explain how a container is isolated."

It's a normal process with namespaces — its own PID tree, network stack, mounts, hostname and optionally user mapping — and cgroups limiting CPU, memory, I/O and PIDs. Docker also drops most capabilities, applies a seccomp profile and AppArmor/SELinux. It shares the host kernel, so isolation is weaker than a VM; that's why we run non-root, avoid privileged mode and use sandboxed runtimes or Fargate/Firecracker for untrusted workloads.

"Your container works on your laptop but fails on an EKS node with exec format error. Why?"

Architecture mismatch: I likely built an arm64 image on an Apple Silicon Mac and the nodes are amd64 (or the reverse with Graviton nodes). Fix by building multi-platform images with docker buildx build --platform linux/amd64,linux/arm64 and pushing a manifest list.

"How does layer caching work and how would you speed up a slow CI build?"

Each instruction is cached if it and all previous layers are unchanged; for COPY the file checksums matter. One miss invalidates everything after it. I'd check the build output for where caching breaks, move dependency installs before source copies, add .dockerignore, use cache mounts, and since CI agents are ephemeral, use a registry or GitHub Actions cache backend. I'd also parallelise independent stages, which BuildKit does automatically.

"Where are secrets safe in a Docker workflow?"

Not in ARG, ENV, or copied files — they end up in image layers or config. At build time use RUN --mount=type=secret. At runtime inject from a secret manager: ECS secrets from Secrets Manager/SSM, Kubernetes Secrets synced by External Secrets Operator, or the Secrets Store CSI driver. And scan images for leaked secrets (Trivy secret scanning).

"A host's disk keeps filling up with Docker data. What do you do?"

docker system df to see whether it's images, containers, volumes, build cache or logs. Container logs with the json-file driver are unbounded by default — I'd set max-size/max-file in daemon.json. Prune unused images and build cache on a schedule, move /var/lib/docker to its own volume, and alert on disk usage. If containers write to their writable layer, move that data to volumes.

"Why tag with the git SHA instead of latest?"

Tags are mutable pointers; latest can change under you, so two nodes might run different code, and rollbacks are ambiguous. A git SHA tag ties the image to exactly one commit, makes deployments reproducible and auditable, and lets you roll back by redeploying a previous SHA. I'd also enable tag immutability in ECR and ideally deploy by digest.

"Docker vs containerd vs runc?"

runc is the low-level OCI runtime that creates the namespaces and cgroups and starts the process. containerd is the daemon managing images, snapshots and container lifecycles, calling runc through a shim. Docker (dockerd) adds the developer experience — builds, networks, volumes, the CLI and API — on top of containerd. Kubernetes talks to containerd directly through CRI, which is why dockershim was removed in 1.24 without breaking Docker-built images.