Docker and Kubernetes course menu
Intermediate Docker Interview Questions and Answers
Intermediate Docker interview questions with answers: layers and caching, multi-stage builds, image size, networking, Compose, health checks, secrets and debugging.
On this page
- Building images
- 1. What are image layers?
- 2. How does the build cache work?
- 3. How do you reduce an image's size?
- 4. What is a multi-stage build?
- 5. What is .dockerignore for?
- 6. Why does deleting a file in a later RUN not shrink an image?
- 7. What is the difference between ARG and ENV?
- 8. Why prefer the exec form of CMD?
- Running containers
- 9. What are restart policies?
- 10. What is a health check?
- 11. What does exit code 137 mean?
- 12. A container exits immediately. How do you debug it?
- 13. How do you limit a container's resources?
- Networking and Compose
- 14. Explain Docker's network drivers.
- 15. How does name resolution work between containers?
- 16. What is the difference between depends_on with and without a condition?
- 17. What does docker compose down -v do?
- 18. How do you give an app a secret?
- 19. How do you develop with live reload in Docker?
- 20. How would you tag images in CI?
These questions check whether you can build good images, and run real apps.
Building images#
1. What are image layers?#
Show solution
Each Dockerfile instruction creates a read-only layer of file changes. Layers are stacked, cached, and shared between images. A running container adds a thin writable layer on top.
2. How does the build cache work?#
Show solution
Docker reuses a layer if the instruction and its inputs are unchanged. Once one step changes, all later steps rebuild. So copy dependency files and install them before copying code.
3. How do you reduce an image's size?#
Show solution
Use a slim or Alpine base, multi-stage builds, and a .dockerignore. Combine install and clean-up in one RUN, and do not install what you do not need.
4. What is a multi-stage build?#
Show solution
A Dockerfile with several FROM stages. You build in one stage, then COPY --from only the result into a small final image, which leaves compilers and source code behind.
5. What is .dockerignore for?#
Show solution
It excludes files from the build context, such as node_modules, .git and .env. This makes builds faster, and keeps secrets out of images.
6. Why does deleting a file in a later RUN not shrink an image?#
Show solution
Layers only add. The file remains in the earlier layer. Delete it in the same RUN that created it.
7. What is the difference between ARG and ENV?#
Show solution
ARG exists only during the build, and is set with --build-arg. ENV is available during the build and in the running container. Neither is safe for secrets.
8. Why prefer the exec form of CMD?#
Show solution
CMD ["node", "app.js"] makes your app PID 1, so it receives signals such as SIGTERM. The shell form runs through sh, which may not pass signals on, so docker stop waits and then kills it.
Running containers#
9. What are restart policies?#
Show solution
no, on-failure, always and unless-stopped. They tell Docker when to restart a container after it exits or the daemon restarts.
10. What is a health check?#
Show solution
A command that Docker runs regularly to test whether the app works. The status is starting, healthy or unhealthy. Plain Docker only reports it, but Compose and orchestrators can act on it.
11. What does exit code 137 mean?#
Show solution
The process was killed by SIGKILL (128 + 9), usually by the out-of-memory killer, or by docker kill. Check OOMKilled in docker inspect.
12. A container exits immediately. How do you debug it?#
Show solution
Check docker ps -a for the exit code, and docker logs for messages. Run the image with a shell, such as docker run -it --rm image sh, or override the entrypoint to look inside.
13. How do you limit a container's resources?#
Show solution
With --memory and --cpus on docker run, or mem_limit and cpus in Compose.
Networking and Compose#
14. Explain Docker's network drivers.#
Show solution
bridge is a private network on one host, and is the default. host shares the host's network. none has no networking. overlay connects containers across hosts.
15. How does name resolution work between containers?#
Show solution
User-defined networks have an embedded DNS server, at 127.0.0.11, which resolves container and service names. The default bridge network does not offer this.
16. What is the difference between depends_on with and without a condition?#
Show solution
Plain depends_on only controls start order. condition: service_healthy waits until the dependency passes its health check, which matters for databases.
17. What does docker compose down -v do?#
Show solution
It removes the containers and networks, and also the named volumes, which deletes the data.
18. How do you give an app a secret?#
Show solution
Pass it at run time, not in the image. Environment variables work, but they can be seen with docker inspect. Mounted secret files are better, using Compose secrets, Kubernetes Secrets or a cloud secrets manager.
19. How do you develop with live reload in Docker?#
Show solution
Bind-mount the source into the container and run a watching server, or use docker compose watch. Use an extra volume so that the mount does not hide node_modules.
20. How would you tag images in CI?#
Show solution
With the commit ID and a release version, so that each image can be traced and rolled back. Avoid deploying latest.
Next: Advanced Questions.
Finished this lesson?
Related lessons
Recommended Docker and Kubernetes resources
- Docker Deep Dive by Nigel Poulton
A practical guide to how Docker works, for readers who have finished the basics.
Some links are affiliate links. If you buy through them, Laviyx Learn may earn a commission at no extra cost to you. As an Amazon Associate, we earn from qualifying purchases.