Table of contents
- Docker Interview Questions for Freshers
- 1. What is the difference between an Image, Container, and Engine?
- 2. What is the difference between COPY and ADD in Docker?
- 3. What is the difference between CMD and RUN in Docker?
- 4. How can you reduce the size of a Docker image?
- 5. Why and when should you use Docker?
- 6. Explain Docker components and their interaction.
- 7. Explain Docker Compose, Dockerfile, Docker Image, and Docker Container.
- 8. Docker vs Hypervisor
- 9. Advantages and Disadvantages of Docker
- 10. What is a Docker namespace?
- 11. What is a Docker registry?
- 12. What is an entry point in Docker?
- 13. How to implement CI/CD in Docker?
- 14. Will data in a container be lost when it exits?
- 15. What is a Docker Swarm?
- 16. Key Docker Commands
- 17. Common practices to reduce Docker image size
- 18. Troubleshooting a Docker container not starting
- 19. What is the Docker networking model?
- 20. Managing persistent storage in Docker
- 21. How to secure Docker containers?
- 22. What is Docker overlay networking?
- 23. Handling environment variables in Docker
- 24. Real scenarios where Docker is used
Docker Interview Questions for Freshers
Docker is an essential tool for DevOps professionals, and its prominence makes it a frequent topic in interviews. This blog presents 24 vital Docker interview questions with concise answers to help you prepare effectively.
1. What is the difference between an Image, Container, and Engine?
Image: A read-only template containing application code and dependencies. Used to create containers.
Container: A lightweight, executable package running an application in isolation, based on an image.
Engine: The core Docker technology responsible for building and running containers.
2. What is the difference between COPY and ADD in Docker?
COPY: Copies files or directories from the local filesystem to a specified location in the container. Syntax:
COPY <source> <destination>
.ADD: Similar to
COPY
, but also supports remote URLs and automatic extraction of compressed files. Syntax:ADD <source> <destination>
.
3. What is the difference between CMD and RUN in Docker?
CMD: Sets the default command to execute when a container starts. Only one CMD instruction is allowed per Dockerfile. Example:
CMD ["python", "app.py"]
.RUN: Executes commands during the image-building process, creating a new image layer. Example:
RUN apt-get update && apt-get install -y nginx
.
4. How can you reduce the size of a Docker image?
Use multi-stage builds to include only necessary files in the final image.
Choose lightweight base images (e.g.,
alpine
).Combine multiple commands into a single
RUN
statement.Exclude unnecessary files using
.dockerignore
.
5. Why and when should you use Docker?
Why: Docker ensures consistent application behavior across environments, simplifies deployments, and supports microservices architectures.
When: When deploying portable applications or adopting microservices, and when requiring isolated, lightweight environments for development and testing.
6. Explain Docker components and their interaction.
Docker Engine: The runtime for managing containers.
Images: Serve as templates for creating containers.
Containers: Run-time instances of images.
Registry: Stores and distributes Docker images (e.g., Docker Hub).
Interaction: The engine pulls images from the registry, creates containers, and manages their lifecycle.
7. Explain Docker Compose, Dockerfile, Docker Image, and Docker Container.
Docker Compose: Orchestrates multi-container applications using a YAML configuration file.
Dockerfile: A script defining how to build a Docker image.
Docker Image: A packaged application and its dependencies.
Docker Container: A running instance of a Docker image.
8. Docker vs Hypervisor
Docker: Uses OS-level virtualization. Containers share the host OS kernel, making them faster and more resource-efficient.
Hypervisor: Manages virtual machines with separate OS instances, offering stronger isolation but higher overhead.
9. Advantages and Disadvantages of Docker
Advantages:
Lightweight and portable.
Faster deployments.
Supports microservices and CI/CD.
Disadvantages:
Limited to containerized applications.
Shared kernel poses security challenges.
Requires orchestration tools for large-scale management.
10. What is a Docker namespace?
Namespaces provide isolation for containers, separating resources like processes, network, and file systems. They ensure containers operate independently.
11. What is a Docker registry?
A centralized repository for storing and managing Docker images. Examples:
Public: Docker Hub.
Private: AWS Elastic Container Registry (ECR), GitHub Container Registry.
12. What is an entry point in Docker?
An ENTRYPOINT specifies the main process to run in a container. It can accept additional arguments. Example:
ENTRYPOINT ["python", "app.py"]
13. How to implement CI/CD in Docker?
Build the application using a Dockerfile.
Push the image to a Docker registry.
Deploy the image using CI/CD tools like Jenkins, GitLab CI, or GitHub Actions.
Automate tests and monitor deployments.
14. Will data in a container be lost when it exits?
Yes, unless data is stored in a volume or bind mount, which ensures persistence beyond the container lifecycle.
15. What is a Docker Swarm?
A native orchestration tool that manages a cluster of Docker nodes, enabling container deployment, scaling, and management across multiple hosts.
16. Key Docker Commands
View running containers:
docker ps
Run a container with a name:
docker run --name <name> <image>
Export an image:
docker save -o <file>.tar <image>
Import an image:
docker load < <file>.tar
Delete a container:
docker rm <container_id>
Clean unused resources:
docker system prune
17. Common practices to reduce Docker image size
Use minimal base images (e.g.,
alpine
).Implement multi-stage builds.
Exclude files using
.dockerignore
.
18. Troubleshooting a Docker container not starting
Check logs:
docker logs <container_id>
.Inspect configuration:
docker inspect <container_id>
.Verify resource limits and dependencies.
19. What is the Docker networking model?
Bridge: Default network for standalone containers.
Host: Shares the host’s network stack.
Overlay: Enables multi-host communication in Swarm clusters.
Custom: User-defined networks for tailored setups.
20. Managing persistent storage in Docker
Use volumes: Managed by Docker and portable across containers.
Use bind mounts: Maps host directories to container directories.
21. How to secure Docker containers?
Use trusted base images.
Scan images for vulnerabilities.
Set resource limits to prevent overuse.
Run containers with non-root users.
22. What is Docker overlay networking?
A network driver enabling secure communication between containers running on different hosts in a Swarm cluster.
23. Handling environment variables in Docker
Use the
-e
flag:docker run -e VAR_NAME=value <image>
.Define variables in the Dockerfile using
ENV
.Use
.env
files with Docker Compose.
24. Real scenarios where Docker is used
Containerizing MySQL and Nginx during training.
Building multi-container setups using Docker Compose.
Deploying Java and Python applications with Dockerfiles.
I hope this guide helps you ace your Docker interview! Let me know if you have more questions or need further clarifications.
Feel free to connect with me on LinkedIn.