Docker Interview Questions & Answers (With Examples)

Docker Interview Questions & Answers (With Examples)

Docker interview answers: images vs containers, layers, Dockerfile best practices, multi-stage builds, volumes, networking and Docker Compose.

Docker questions are common in interviews for backend, full stack, DevOps, data engineering and QA automation roles. Freshers are asked the basics; experienced candidates get questions about optimising images, debugging containers and running them in production. Interviewers want to see that you have actually used Docker, not just memorised definitions. Here are fifteen common questions with concise answers and examples.

1. What is Docker, and why use it?

Docker packages an application with its dependencies into a container image that runs the same way on any machine with a container runtime. It solves "works on my machine" problems, makes environments reproducible, speeds up onboarding and CI, and is the standard packaging format for deploying to Kubernetes and many cloud services.

2. Containers vs virtual machines

Virtual machines include a full guest operating system on top of a hypervisor. Containers share the host's kernel and isolate processes using kernel features (namespaces and cgroups on Linux). Containers are lighter, start faster and use less memory; VMs provide stronger isolation and can run different operating systems.

3. Image vs container

An image is a read-only template made of layers. A container is a running (or stopped) instance of an image with its own writable layer. You can run many containers from one image.

4. What are image layers?

Each instruction in a Dockerfile that changes the filesystem creates a layer. Layers are cached and shared between images. Ordering instructions well — rarely changing steps first — makes rebuilds much faster.

5. Write a good Dockerfile for a Node.js app

Explain: copying `package*.json` first means dependencies are cached unless they change; `npm ci` gives reproducible installs; running as a non-root user improves security.

6. What is a multi-stage build?

A Dockerfile with several `FROM` stages, where later stages copy only what they need from earlier ones. It keeps build tools out of the final image.

The final image contains only the JRE and the JAR, not Maven or source code.

7. CMD vs ENTRYPOINT

`ENTRYPOINT` defines the main executable; `CMD` provides default arguments (or a default command if there is no entrypoint). Arguments passed to `docker run` replace `CMD`. Use the exec (JSON array) form so signals reach your process correctly.

8. COPY vs ADD

Both copy files into the image. `ADD` can also extract local tar archives and fetch remote URLs. Prefer `COPY` for clarity unless you specifically need `ADD`'s extra behaviour.

9. How do you persist data?

Container filesystems are ephemeral. Use volumes (managed by Docker) or bind mounts (a host directory mounted into the container). Volumes are preferred for databases and production data; bind mounts are convenient in development for live code reloading.

10. How does container networking work?

By default, containers connect to a bridge network. Containers on the same user-defined network can reach each other by container name through Docker's DNS. Use `-p host:container` to publish ports to the host.

11. What is Docker Compose?

A tool to define and run multi-container applications with a YAML file: services, networks, volumes and environment variables.

Note that `depends_on` controls start order, not readiness; use health checks or retry logic for databases.

12. How do you reduce image size?

  • Use slim or Alpine base images where compatible.
  • Use multi-stage builds.
  • Combine related `RUN` steps and clean package caches in the same layer.
  • Use a `.dockerignore` to exclude `node_modules`, `.git`, logs and build outputs.
  • Install only production dependencies.

13. How do you debug a container?

  • `docker logs <container>` to see output.
  • `docker exec -it <container> sh` to open a shell inside.
  • `docker inspect <container>` for configuration, network and mounts.
  • `docker stats` for CPU and memory usage.
  • Check exit codes, for example 137 often indicates the process was killed, commonly due to memory limits.

14. How do you handle secrets?

Do not bake secrets into images or commit them in Dockerfiles. Pass them at runtime through environment variables from a secure source, Docker or Kubernetes secrets, or a secrets manager. Use build secrets for credentials needed only during builds.

15. Docker vs Kubernetes

Docker builds and runs containers; Kubernetes orchestrates containers across a cluster: scheduling, scaling, self-healing, service discovery and rolling updates. Kubernetes runs images built with Docker or other OCI-compatible tools.

Prepare one real story: how you containerised an application, cut an image size, or debugged a container that kept restarting. A specific example convinces interviewers that you have used Docker beyond tutorials.

Getting to the interview

List Docker and related tools precisely on your resume, in context: "Containerised five Spring Boot services with multi-stage builds and deployed them to EKS." The Algnetic ATS resume builder checks your resume against target postings, and auto-apply keeps applications moving while you prepare.

Key takeaways

  • Know images, containers, layers and how containers differ from VMs.
  • Write efficient Dockerfiles with good layer ordering, non-root users and multi-stage builds.
  • Understand volumes, networking and Docker Compose.
  • Be able to debug containers and handle secrets safely.
  • Back your answers with a real example from your work or projects.

Let Algnetic apply to matching jobs for you · More career guides

Related guides

https://algnetic.com/blog/docker-interview-questions-and-answers