{jsonbody.com}

Top 40 Docker Interview Questions & Answers

Top 40 Docker interview questions and answers for developers and DevOps engineers: images, Dockerfile, layers, volumes, networks, Compose and security.

Loading…

Docker Basics

Q1. What is Docker?

A platform that packages an app and its dependencies into containers that run the same way on any machine with a container runtime.

Q2. What is the difference between a container and a virtual machine?

A VM runs a full guest operating system on a hypervisor. A container shares the host's kernel and isolates only the process, so it starts in seconds and uses far less memory.

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

An image is a read-only template with the app and its file system. A container is a running instance of an image with its own writable layer.

Q4. What is a Docker registry?

A service that stores and distributes images, such as Docker Hub, Amazon ECR or GitHub Container Registry. You docker push to it and docker pull from it.

Q5. What are the most used Docker commands?

docker build creates an image, docker run starts a container, docker ps lists containers, docker logs shows output, docker exec runs a command inside one, and docker stop and docker rm stop and remove it.

Q6. What does docker run -d -p 8080:80 nginx do?

It starts an nginx container in the background (-d) and maps port 8080 on the host to port 80 in the container.

Q7. What Linux features make containers possible?

Namespaces isolate what a process can see, such as processes, network and mounts. Control groups (cgroups) limit how much CPU and memory it can use.

Q8. What is the difference between docker stop and docker kill?

docker stop sends SIGTERM so the app can shut down cleanly, then SIGKILL after a timeout. docker kill sends SIGKILL immediately.

Q9. What are container restart policies?

no, on-failure, always and unless-stopped, set with --restart. They decide whether Docker restarts a container after it exits or the daemon restarts.

Q10. What is the Docker daemon?

The background service dockerd that builds images and manages containers, networks and volumes. The docker CLI talks to it through an API.

Dockerfile & Images

Q11. What is a Dockerfile?

A text file of instructions, such as FROM, COPY, RUN and CMD, that docker build follows to create an image.

Q12. What is the difference between CMD and ENTRYPOINT?

ENTRYPOINT sets the executable that always runs. CMD sets default arguments, or the default command, which docker run arguments replace. Together, CMD supplies default arguments to ENTRYPOINT.

Q13. What is the difference between COPY and ADD?

COPY copies local files into the image. ADD can also extract local tar archives and download URLs. Prefer COPY unless you need those extras.

Q14. What are image layers?

Each instruction that changes the file system creates a read-only layer. Layers are cached and shared between images, which speeds up builds and saves disk space.

Q15. How do you make Docker builds faster with caching?

Order instructions from least to most frequently changed: copy dependency files such as package.json and install packages before copying the source code, so code changes do not reinstall dependencies.

Q16. What is a multi-stage build?

A Dockerfile with several FROM stages, where you build in a full image and copy only the output into a small runtime image. It keeps compilers and build tools out of production.

Q17. How do you reduce image size?

Use small base images such as alpine, slim or distroless, use multi-stage builds, combine and clean up RUN steps, and exclude files with .dockerignore.

Q18. What is .dockerignore?

A file listing paths, such as node_modules and .git, to leave out of the build context. It speeds up builds and keeps secrets out of images.

Q19. What is the difference between ARG and ENV?

ARG defines a variable available only during the build. ENV sets an environment variable that stays in the image and is available to the running container.

Q20. Why should you not use the latest tag in production?

latest changes over time, so deployments become unpredictable and hard to roll back. Use specific version tags or image digests.

Volumes, Networking & Compose

Q21. What is the difference between volumes and bind mounts?

Volumes are managed by Docker, stored in its own area and suit persistent data such as databases. Bind mounts map a host folder into the container and suit development.

Q22. Why do containers lose data when removed?

Each container's writable layer is deleted with it. Store anything that must survive, such as database files, in a volume.

Q23. What network drivers does Docker have?

bridge is the default for single-host containers, host shares the host network, none disables networking, and overlay connects containers across several hosts.

Q24. How do containers talk to each other?

On a user-defined bridge network, containers reach each other by name through Docker's built-in DNS, such as http://api:3000.

Q25. What is Docker Compose?

A tool that defines multi-container apps in a compose.yaml file, with services, networks and volumes, and starts them together with docker compose up.

Q26. What does depends_on do in Compose?

It controls start order. Add condition: service_healthy with a health check to wait until a dependency such as a database is actually ready.

Q27. What is a health check?

A HEALTHCHECK instruction or Compose setting that runs a command to test whether the app works. Docker marks the container healthy or unhealthy.

Q28. How do you pass environment variables to a container?

Use -e KEY=value or --env-file with docker run, or environment and env_file in Compose. Use secrets for passwords instead of plain variables.

Q29. How do you limit a container's resources?

Use flags such as --memory=512m and --cpus=1.5, or deploy.resources.limits in Compose, so one container cannot starve the host.

Q30. How do you see why a container exited?

Run docker logs <container> for its output and docker inspect for the exit code and state. Exit code 137 usually means it was killed, often for running out of memory.

Security & Production

Q31. Why should containers not run as root?

If an attacker escapes the app, root inside the container makes breaking out to the host easier. Add a USER instruction to run as an unprivileged user.

Q32. How do you scan images for vulnerabilities?

Use tools such as Docker Scout, Trivy or Grype in CI to check base images and packages for known CVEs, and rebuild images regularly.

Q33. How do you handle secrets in Docker?

Never bake them into images or ENV. Use Docker or Compose secrets, BuildKit secret mounts at build time, or a secrets manager at runtime.

Q34. What is the difference between Docker and Kubernetes?

Docker builds and runs containers. Kubernetes orchestrates many containers across a cluster with scaling, self-healing, rollouts and service discovery.

Q35. What is BuildKit?

Docker's modern build engine, now the default. It builds stages in parallel, caches better and supports secret and SSH mounts during builds.

Q36. How do you build images for several CPU architectures?

Use docker buildx build --platform linux/amd64,linux/arm64 to build and push a multi-architecture image, so it runs on both Intel and ARM machines.

Q37. How should apps log in containers?

Write logs to standard output and standard error, and let Docker or the orchestrator collect and ship them, instead of writing log files inside the container.

Q38. What is the PID 1 problem?

The first process in a container must handle signals and reap child processes. Many apps do not, so use an exec-form CMD or a small init such as docker run --init.

Q39. What is distroless?

Base images that contain only the app and its runtime, with no shell or package manager. They are small and give attackers much less to work with.

Q40. How do you clean up disk space used by Docker?

docker system prune removes stopped containers, unused networks and dangling images. Add -a to remove all unused images and --volumes for unused volumes.