Careeroza
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
--privilegedand 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?
| Compose | Kubernetes |
|---|---|
| one host | cluster of many nodes |
| simple YAML, quick to learn | many objects, steeper learning curve |
| no self-healing across machines | self-healing, autoscaling, rolling updates |
| local dev, small deployments | production at scale |
How would you debug a container that keeps restarting?
docker ps -a: check the exit code (1 = app error, 137 = killed/OOM, 139 = segfault).docker logs <container>: read the error.docker inspect: check env vars, mounts, health check and OOMKilled.- Run the image with a shell (
docker run -it --entrypoint sh image) to test manually.