Docker Interview Questions
Distinguish what Docker decides at build time from what a running container receives. Start with images and isolation, then diagnose realistic build and deployment failures.
Try answering aloud before opening each explanation.
Try the Docker practice demoFoundations
How does an image differ from a container?
An image contains the packaged filesystem layers and runtime defaults. A container runs from that image with its own process environment and writable layer unless configured otherwise.
Do Linux containers normally boot their own kernel?
No. They isolate processes through facilities provided by the host kernel, such as namespaces and cgroups. Docker Desktop may put Linux containers inside a Linux VM on a non-Linux host.
How does publishing a port differ from EXPOSE?
EXPOSE documents intended ports; it does not bind a host port by itself. Publishing creates a host-to-container mapping, and the bind address controls the host interfaces that expose it.
What persists when a container is removed and recreated?
Mounted volumes or bind mounts can persist independently. Data written only to the container's writable layer is removed with that container.
How does an image digest differ from a tag?
A digest identifies specific content, while a tag is a movable name. Pin a digest when an exact deployment must be reproducible and update the pin intentionally.
Applied
Why copy dependency manifests before copying source code?
The dependency install layer can be reused while manifests stay the same. Copying changing application files earlier invalidates that layer on otherwise unrelated edits.
How can a multi-stage build reduce image size?
Compile in a stage containing the toolchain, then copy only runtime artifacts into a clean final stage. Check that the copied binary still has the libraries it needs.
How do ENTRYPOINT and CMD work together in exec form?
ENTRYPOINT specifies the executable and CMD can supply its default arguments. Arguments supplied when starting the container replace CMD by default without replacing the entrypoint.
When would you use a bind mount instead of a named volume?
Use a bind mount when the container needs a specific host path, such as checked-out source during development. A named volume is managed by Docker and avoids coupling the container to a chosen host directory.
How should a build receive a short-lived package registry credential?
Use a build secret mount for the single step that needs it. Avoid putting the value in ARG, ENV or a layer, and verify that the build step does not copy it into its output.
Production scenarios
A process is healthy inside the container but unreachable through a published port. What do you check?
Confirm the mapping uses the actual container port. Check that the process listens on a reachable container interface rather than only on loopback, then inspect host routing and firewall policy.
Why can two containers not reach each other through localhost?
Each container generally has its own loopback interface. Put them on the intended shared network and connect using the peer's service name and container port.
Why does a container fail to shut down gracefully?
Check what runs as PID 1 and whether it receives and forwards the stop signal. Use exec form or a wrapper that handles signals, and test that the process drains before forced termination.
Why does deleting build files in a later layer not reclaim their image size?
Earlier layers still contain the bytes. Avoid copying unnecessary files, remove transient data within the same layer, or keep those files in a separate build stage.
An image works on a laptop but fails with an exec format error on a server. Where do you look?
Check the image platform and the architecture of copied executables. Build and test for the target platform; a multi-platform manifest does not fix a wrong binary inside one variant.
Further reading
Official documentation
Related question sets
- Node.js interview questions
- TypeScript interview questions
- REST API interview questions
- Microservices interview questions
- SQL interview questions
- Python interview questions
- JavaScript interview questions
- React interview questions
- Kubernetes interview questions
- System Design interview questions
- Java interview questions
- MongoDB interview questions
- PostgreSQL interview questions
- Redis interview questions
- NestJS interview questions
- RabbitMQ interview questions