Technology

Docker Interview Questions and Answers

Docker is a core DevOps and backend skill. These Docker interview questions cover images, containers and Dockerfiles for freshers, then layers and caching, multi-stage builds, volumes, networking, Docker Compose, security and production best practices for experienced engineers.

30 questions · Updated

Reading answers is not the same as saying them.Practise Docker questions out loud and get a score, what you missed and a model answer for each one.
Practise free with AI
Docker Interview Questions with answers. Examples: What is Docker, and how is a container different from a virtual machine? What is the difference between an image and a container? What are the common Dockerfile instructions? What is the difference between CMD and ENTRYPOINT?
Docker Interview Questions: 30 questions with model answers on this page.

Basic Docker interview questions

Fundamentals, definitions and simple scenarios. Good for freshers and warm-ups.

1. What is Docker, and how is a container different from a virtual machine?

Answer flow · 3 steps
  1. Docker packages an application with its dependencies into a container that runs the same way everywhere.

  2. A virtual machine includes a full guest operating system on a hypervisor, so it is heavy and slow to start.

  3. Containers share the host's kernel and isolate processes using namespaces and cgroups, so they are lightweight, start in seconds and use far fewer resources.

2. What is the difference between an image and a container?

Answer flow · 3 steps
  1. An image is a read-only template with the application, libraries and configuration, built from a Dockerfile and stored in a registry such as Docker Hub.

  2. A container is a running instance of an image with a thin writable layer on top.

  3. You can start many containers from one image.

3. What are the common Dockerfile instructions?

FROM sets the base image, WORKDIR sets the working directory, COPY copies files into the image, RUN executes commands at build time, ENV sets environment variables, EXPOSE documents the port, USER sets the user to run as, and CMD or ENTRYPOINT define the command run when the container starts.

4. Which Docker commands do you use most?

docker build -t name . builds an image, docker run starts a container, docker ps lists running containers (-a for all), docker logs shows output, docker exec -it name sh opens a shell inside, docker stop and docker rm stop and remove containers, docker images and docker rmi manage images, docker pull and docker push transfer images, and docker system prune cleans up.

5. What do the common docker run options do?

-d runs in the background, -p 8080:80 publishes container port 80 on host port 8080, -e sets environment variables (or --env-file), -v mounts volumes, --name names the container, --rm removes it when it stops, -it gives an interactive terminal, --network attaches it to a network, and --restart sets the restart policy.

6. What is a container registry?

Answer flow · 3 steps
  1. A registry stores and distributes images.

  2. Docker Hub is the default public registry; others include GitHub Container Registry, Amazon ECR, Google Artifact Registry and Azure Container Registry, often private.

  3. Images are named registry/repository:tag, pushed after building in CI and pulled by servers and Kubernetes clusters.

7. What is the difference between COPY and ADD?

Answer flow · 3 steps
  1. COPY copies files and folders from the build context into the image.

  2. ADD does the same but can also extract local tar archives and download files from URLs.

  3. Because ADD's extra behaviour can be surprising, COPY is recommended for normal file copying, and RUN with curl or wget is clearer for downloads.

8. What is the difference between ARG and ENV?

Answer flow · 3 steps
  1. ARG defines a variable available only while the image is being built, passed with docker build --build-arg, such as a version number.

  2. ENV sets an environment variable that is stored in the image and available to running containers.

  3. Neither should hold secrets, because both can be seen in the image history; use build secrets or runtime secrets instead.

9. What is .dockerignore for?

It lists files the Docker client should not send to the build context, such as node_modules, .git, logs, local .env files and build output. This makes builds faster, avoids invalidating the layer cache unnecessarily, keeps images smaller and prevents secrets from accidentally being copied into images.

10. How do you debug a running container?

Check output with docker logs -f, open a shell with docker exec -it container sh (or bash), check settings and state with docker inspect, watch resource use with docker stats and see processes with docker top. For minimal images without a shell, docker debug or a temporary debugging container sharing the same namespaces helps.

11. What is the difference between alpine, slim and distroless base images?

Answer flow · 4 steps
  1. Alpine images are very small and use musl libc, which can cause compatibility issues with some compiled libraries.

  2. Slim images are trimmed Debian images using glibc, a safe default.

  3. Distroless images contain only the application runtime, with no shell or package manager, which gives a smaller attack surface but makes debugging harder.

  4. Choose based on size, compatibility and security needs.

Intermediate Docker interview questions

Applied problems, trade-offs and questions about your own projects.

12. What is the difference between CMD and ENTRYPOINT?

Answer flow · 4 steps
  1. ENTRYPOINT defines the main executable that always runs, and CMD provides default arguments that can be overridden at docker run.

  2. If only CMD is used, the whole command can be replaced.

  3. A common pattern is ENTRYPOINT ["python", "app.py"] with CMD ["--port", "8000"].

  4. The exec (JSON array) form is preferred so signals reach the process.

13. How does layer caching work, and how do you speed up builds?

Answer flow · 3 steps
  1. Each instruction creates a layer, and Docker reuses cached layers until one changes; every layer after a change is rebuilt.

  2. So I copy dependency files first and install dependencies, such as COPY package*.json then RUN npm ci, before copying the rest of the source code.

  3. I also use a .dockerignore file and combine related RUN commands.

14. What are volumes and bind mounts?

Answer flow · 3 steps
  1. Container file systems are temporary, so data is lost when a container is removed.

  2. Volumes are managed by Docker and are the preferred way to persist data such as databases.

  3. Bind mounts map a specific host directory into the container, which is useful in development for live code reloading. tmpfs mounts keep data in memory only.

15. What is Docker Compose?

Docker Compose defines and runs multi-container applications from a YAML file (compose.yaml), with services, networks, volumes, environment variables and dependencies. docker compose up starts the whole stack, such as an API, a database and Redis, with one command. It is mostly used for local development and testing.

16. What are restart policies and health checks?

Answer flow · 3 steps
  1. Restart policies decide what happens when a container exits: no (default), on-failure, always, or unless-stopped.

  2. A HEALTHCHECK instruction or Compose setting runs a command periodically, such as calling a /health endpoint, so Docker marks the container healthy or unhealthy, and Compose can wait for a database to be healthy before starting the app.

  3. Kubernetes uses its own probes instead.

17. How do you limit container resources?

Answer flow · 3 steps
  1. Use --memory to cap memory, which kills the container with an out-of-memory error if exceeded, and --cpus to limit CPU, for example --cpus=1.5.

  2. Without limits, one container can starve others on the same host.

  3. Runtimes such as the JVM and Node.js should be configured to respect container limits, which modern versions detect automatically.

18. How should you tag images?

Answer flow · 3 steps
  1. Avoid relying on latest, because it changes and makes deployments unpredictable.

  2. Tag images with the version and the Git commit SHA, such as myapp:1.4.2 and myapp:sha-3f2a1c9, so every deployment is traceable and easy to roll back.

  3. For maximum safety, deploy by image digest (sha256:...), which never changes, and pin base images too.

19. Why does PID 1 matter in containers?

Answer flow · 3 steps
  1. The main process in a container runs as PID 1, which receives stop signals such as SIGTERM.

  2. If CMD uses the shell form, a shell becomes PID 1 and may not forward signals, so the app is killed abruptly after a timeout.

  3. Use the exec (JSON array) form, make the app handle SIGTERM gracefully, and use --init or tini to reap zombie processes when needed.

20. What is BuildKit, and what do cache mounts and build secrets do?

Answer flow · 3 steps
  1. BuildKit is Docker's modern build engine, with parallel stages and better caching.

  2. Cache mounts (RUN --mount=type=cache,target=/root/.npm npm ci) keep package manager caches between builds without storing them in the image.

  3. Secret mounts (RUN --mount=type=secret,id=npmrc) provide credentials during a build without leaving them in any layer.

21. How do you handle configuration and secrets in Docker Compose?

Answer flow · 3 steps
  1. Non-sensitive settings go in environment variables or an env_file, and variables can be substituted from a .env file.

  2. Secrets such as passwords should use Compose secrets, mounted as files in /run/secrets, or come from a secrets manager in production.

  3. Never bake secrets into images or commit .env files with real credentials.

22. A container exits immediately after starting. How do you debug it?

Answer flow · 3 steps
  1. Check docker ps -a for the exit code and docker logs for errors.

  2. Common causes are the main command finishing immediately (containers stop when their main process ends, so services must run in the foreground), a missing file or wrong path, a missing environment variable, or an exit code 137 meaning it was killed for using too much memory.

  3. Overriding the entrypoint with a shell lets you look inside.

High level Docker interview questions

System design, deep internals, leadership and tough follow-ups.

23. What is a multi-stage build?

Answer flow · 3 steps
  1. A multi-stage build uses several FROM stages in one Dockerfile.

  2. Early stages contain compilers and build tools to build the application; the final stage copies only the built output into a small runtime image such as alpine, distroless or a slim base.

  3. This makes images much smaller and more secure because build tools and source code are not shipped.

24. How do you secure Docker containers?

Use small official or distroless base images and keep them updated, scan images for vulnerabilities with tools such as Trivy or Docker Scout, run as a non-root user, never put secrets in images or build arguments, use a read-only root filesystem where possible, drop Linux capabilities, set resource limits, and sign and pin images by digest.

25. How does Docker networking work?

Answer flow · 3 steps
  1. The default bridge network connects containers on one host; user-defined bridge networks add DNS so containers reach each other by name, which is what Compose sets up.

  2. The host network shares the host's network stack, none disables networking, and overlay networks connect containers across hosts in Swarm.

  3. Ports are published to the host with -p 8080:80.

26. How does Docker work under the hood?

The Docker CLI talks to the Docker daemon, which uses containerd to manage containers and runc to create them according to the OCI runtime specification. Isolation comes from Linux namespaces (PID, network, mount, user and others), resource limits from cgroups, and images use a layered union file system such as overlayfs, with a thin writable layer per container.

27. How does Docker Swarm compare with Kubernetes?

Answer flow · 3 steps
  1. Swarm is built into Docker and is simple to set up, using familiar Compose files for services, scaling and rolling updates, which suits small clusters.

  2. Kubernetes is far more powerful and extensible, with autoscaling, a huge ecosystem, custom resources and support from every cloud provider, but it is more complex.

  3. Most organisations today choose Kubernetes or a managed container service.

28. How do you make Docker images as small as possible?

Use multi-stage builds so only the built output reaches the final image, start from slim or distroless bases, install only production dependencies, clean package manager caches in the same RUN step, use .dockerignore, combine related commands to reduce layers, and for compiled languages such as Go, copy a static binary into a scratch or distroless image. docker history and dive show what takes space.

29. How do you build images for multiple CPU architectures?

Use docker buildx with --platform linux/amd64,linux/arm64 to build a multi-platform image, which is published as a manifest list so each machine pulls the right variant, such as ARM-based Apple Silicon laptops and AWS Graviton servers. Builds use QEMU emulation or, faster, native builders for each architecture, or cross-compilation in the Dockerfile.

30. How do you run Docker securely, including in CI?

Mounting the Docker socket into a container gives it root-level control of the host, and Docker-in-Docker needs privileged mode, so both are risky in shared CI. Safer options are rootless Docker, daemonless builders such as BuildKit in rootless mode, Kaniko or Buildah, and running containers as non-root users with dropped capabilities, read-only file systems, seccomp profiles and scanned images.

Ready to test yourself?Pick your topics and level, answer by voice or text, and get instant feedback. Free.
Start a mock interview

Preparing for a devops engineer interview?

See all DevOps Engineer interview questions, covering Linux, Git, Docker, Kubernetes, CI/CD (GitHub Actions, Jenkins) and more.

Related skill interview questions

More technology interview questions

HR round, company and bank interview questions

Bank and government exams

Interview questions for other fields