Interview › Kubernetes, Docker, Helm & Podman
How does Docker layer caching work, and how do you order a Dockerfile to exploit it?
Kubernetes, Docker, Helm & Podman · Intermediate level
Answer
Docker caches each build step as a layer. To exploit caching, I put stable dependency installation steps before frequently changing source code, copy lock files before the rest of the app, and keep the build context small.
Technical explanation
Changing an early Dockerfile instruction invalidates the cache for following steps.
Copy dependency manifests first, install dependencies, then copy frequently changing application source.
Container image quality affects supply chain, startup time, vulnerability surface, rollout reliability, and debugging workflows.
Prefer reproducible builds: pinned dependencies, small build context, deterministic Dockerfile order, non-root runtime, and immutable image references.
Understand the runtime boundary: an image is not a VM, and container isolation depends on kernel, namespaces, cgroups, capabilities, seccomp, and mounts.
Hands-on example
1. Create a tiny sample app and Dockerfile for this exercise: reorder a Dockerfile and measure cache hits and misses.
2. Build and inspect it with docker build or podman build, docker history, image inspect, and a vulnerability or size scan if available.
3. Run it locally with explicit env vars, ports, user, volumes, and signal tests depending on the question.
4. Convert the final runtime assumptions into Kubernetes fields such as image, command, args, ports, securityContext, probes, and volumeMounts.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Kubernetes, Docker, Helm & Podman interview questions
- What are Kubernetes QoS classes (Guaranteed, Burstable, BestEffort)?
- What is graceful termination, and how do preStop hooks and terminationGracePeriodSeconds work?
- What is a PodDisruptionBudget, and how does it protect availability during maintenance?
- How do you view logs from a crashed (previous) container instance?
- How do you troubleshoot an ImagePullBackOff error?
- What is etcd, and why is it critical to back it up?
- What is a sidecar container, and give a common use case?
- What is a static Pod, and how does it differ from a normally scheduled Pod?