Introduction
Docker is an essential tool for modern DevOps and cloud-native development. Below is a collection of frequently asked Docker interview questions with detailed answers and examples.
What is Docker?
Docker is an open-source platform that enables developers to build, ship, and run applications in lightweight, portable containers. It abstracts the underlying OS and allows applications to run consistently across different environments.
What are Docker Images and Containers?
Docker Image
A Docker Image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and dependencies.
Docker Container
A Docker Container is a running instance of a Docker Image. Containers are isolated environments that ensure consistency across different stages of development.
Docker Interview Questions and Answers
1. What is Docker?
Docker is a containerization platform that allows developers to package applications with their dependencies into lightweight containers.
2. What are the benefits of using Docker?
- Consistency across environments
- Faster development and deployment
- Scalability
- Resource efficiency
3. How do you create a Docker container?
docker run -d --name my_container nginx4. What is a Dockerfile?
A Dockerfile is a script containing instructions to build a Docker image.
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "server.js"]5. What is Docker Compose?
Docker Compose is a tool for defining multi-container applications using a YAML file.
version: '3'
services:
web:
image: nginx
ports:
- '80:80'
db:
image: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password6. How do you list all running Docker containers?
docker ps7. How do you stop a running container?
docker stop <container_id>8. How do you remove a Docker container?
docker rm <container_id>9. How do you remove a Docker image?
docker rmi <image_id>10. How do you build a Docker image from a Dockerfile?
docker build -t my_image .11. What is the difference between Docker COPY and ADD?
COPYcopies files from host to container.ADDdoes the same but can also extract compressed files and download from URLs.
12. What is the difference between ENTRYPOINT and CMD?
CMDprovides default execution parameters.ENTRYPOINTmakes the container act like an executable.
13. How do you persist data in Docker?
Use volumes:
docker volume create my_volume14. What is the difference between Docker volumes and bind mounts?
- Volumes are managed by Docker.
- Bind mounts map host directories to containers.
15. How do you inspect a Docker container?
docker inspect <container_id>16. How do you check Docker logs?
docker logs <container_id>17. What is a multi-stage build in Docker?
It helps optimize image size by using multiple FROM statements in a Dockerfile.
18. How do you restart a stopped container?
docker start <container_id>19. How do you run a command inside a running container?
docker exec -it <container_id> bash20. How do you limit resource usage of a container?
Use flags like --memory and --cpus.
docker run --memory=500m --cpus=1 nginx21. What is Docker Swarm?
Docker Swarm is a container orchestration tool that allows managing multiple containers across multiple hosts.
22. What is Kubernetes, and how does it compare to Docker Swarm?
Kubernetes is a more advanced orchestration platform than Docker Swarm, offering features like auto-scaling and self-healing.
23. How do you push an image to Docker Hub?
docker push <username>/<image_name>:<tag>24. How do you use environment variables in Docker?
docker run -e VAR_NAME=value <image_name>25. How do you create a Docker network?
docker network create my_network26. How do you connect a container to a network?
docker network connect my_network my_container27. How do you list Docker networks?
docker network ls28. How do you inspect a Docker network?
docker network inspect my_network29. How do you run a detached container?
docker run -d <image_name>30. How do you enable Docker BuildKit?
export DOCKER_BUILDKIT=131. How do you secure Docker containers?
By using practices such as image scanning, limiting privileges, enabling seccomp proles, and using network segmentation.
32. How do you monitor Docker containers?
By using tools like Prometheus, Grafana, and Docker Stats.
33. What is the dierence between Docker and Kubernetes?
Docker is a containerization platform, while Kubernetes is a container orchestration platform that manages containerized applications.
34. How can you debug a failed Docker build?
By using the --no-cache flag to rebuild the image without cache, and inspecting the build logs for errors.
35. What are Docker storage drivers?
Storage drivers manage how Docker writes and reads images and containers, such as overlay2, aufs, etc.
36. What is BuildKit in Docker?
BuildKit is a new build subsystem for Docker that provides improved performance and security.
37. How does Docker ensure isolation between containers?
Through technologies such as cgroups, namespaces, and kernel features.
38. How do you clean up Docker resources?
By using commands like docker system prune to remove unused data, containers, and images.
39. What are the best practices for writing a Dockerle?
- Use minimal base images
- Leverage caching eectively
- Avoid unnecessary layers
- Use multi-stage builds
- Keep the image size small
40. How do you scale Docker containers?
By using Docker Swarm or Kubernetes to manage multiple containers across multiple hosts.
41. How do you secure Docker images?
By scanning images for vulnerabilities, using signed images, and following security best practices.
42. How do you manage secrets in Docker?
By using Docker secrets, Docker Configs, or third-party tools like HashiCorp Vault.
43. How do you update a Docker container?
By pulling the latest image and recreating the container with the new image.
44. How do you share data between containers?
By using Docker volumes, bind mounts, or shared networks.
45. How do you debug a running Docker container?
By using the docker exec command to run commands inside the container, or by inspecting logs.
46. How do you create a custom Docker network?
By using the docker network create command with custom options.
47. How do you manage Docker images?
By using commands like docker build, docker push, docker pull, and docker rmi.
docker build -t my_image .
docker push my_image
docker pull my_image
docker rmi my_image48. How do you create a Docker volume?
docker volume create my_volume49. How do you remove unused Docker volumes?
docker volume prune50. How do you run a command in a Docker container?
docker exec -it <container_id> bashConclusion
Docker is a must-know tool for DevOps engineers and software developers. Understanding its core concepts and commands will help you in real-world applications and interviews.
If you found this guide useful, feel free to share it with others preparing for Docker interviews. 🚀