Docker interview questions and answers guide
š± Beginner-Level Docker Questions
1. What is Docker?
Docker is a platform that allows you to package applications and their dependencies into lightweight, portable containers that can run consistently across environments.
2. Difference between Docker and Virtual Machines (VMs)?
FeatureDockerVMResource OverheadLightweight, shares OS kernelHeavy, full OS per VMStartup TimeSecondsMinutesIsolationOS-levelHardware-levelPortabilityHighModerate
3. What is a Docker Image?
A Docker image is a read-only template with instructions to create a container. Images include the application code, libraries, dependencies, and OS packages.
4. What is a Docker Container?
A container is a runtime instance of a Docker image. It includes the application and its environment and runs isolated from other containers.
5. What is Docker Hub?
Docker Hub is a cloud registry for sharing and managing Docker images.
š§© Intermediate-Level Docker Questions
6. What is Dockerfile?
A Dockerfile is a text file containing instructions to build a Docker image. It defines the base image, environment variables, commands, and more.
7. What is the difference between COPY and ADD in Dockerfile?
COPY: Copies files/folders from local system to container.ADD: Copies files/folders and supports extracting tar archives and downloading remote URLs.
8. What is the difference between docker run and docker start?
docker run: Creates and starts a new container.docker start: Starts an existing stopped container.
9. What is a Docker Volume?
Volumes are persistent storage managed by Docker and used to persist data beyond container lifecycle.
10. Difference between EXPOSE and port mapping (-p) in Docker?
EXPOSE: Documents the port the container will listen on.-p: Maps container port to host port for access.
11. What is Docker Compose?
Docker Compose allows defining and running multi-container applications using a docker-compose.yml file.
āļø Advanced-Level Docker Questions
12. How do you optimize Docker images?
Use lightweight base images (Alpine).
Minimize layers by combining commands.
Avoid unnecessary files.
Use
.dockerignoreto exclude files.
13. How does Docker networking work?
Docker supports several network types:
bridge: Default, isolated network for containers.
host: Shares host network stack.
overlay: Multi-host container communication (used in Swarm/Kubernetes).
none: No networking.
14. How does Docker handle security?
Namespaces for isolation
Control groups (cgroups) for resource limits
Seccomp, AppArmor, SELinux for system call restrictions
User namespaces to avoid root inside containers
15. What is Docker Swarm?
Docker Swarm is Dockerās native clustering and orchestration tool, allowing multiple Docker nodes to work together for scaling and high availability.
16. Difference between Docker Swarm and Kubernetes?
FeatureDocker SwarmKubernetesComplexitySimpleComplexScalingEasyDeclarative, automatedEcosystemLimitedLarge, supports many toolsNetworkingBuilt-inCNI plugins
17. How do you persist data in Docker?
Volumes: Preferred method, managed by Docker.
Bind mounts: Maps host directory to container.
tmpfs: In-memory storage for ephemeral data.
18. How do you update a running container?
Containers are immutable. Typically, you:
Build a new image with updates.
Stop the old container.
Run a new container from the updated image.
19. How do you monitor Docker containers?
docker statsfor basic metrics.Use monitoring tools like Prometheus, Grafana, ELK stack, or cAdvisor.
20. What are multi-stage builds in Docker?
Multi-stage builds allow building images in stages, keeping the final image small by copying only necessary artifacts.
ā” Real-Time Docker Scenarios
1. Scenario: Your container crashes on startup. How do you debug it?
Check logs:
docker logs <container_id>Start container interactively:
docker run -it <image> /bin/bashInspect image configuration:
docker inspect <container_id>
2. Scenario: You need to run multiple containers that depend on each other.
Use Docker Compose to define services, networks, and dependencies in
docker-compose.yml.
3. Scenario: You want to persist MySQL data across container restarts.
Use Docker volume:
docker run -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql
4. Scenario: Deploy an updated version of a container without downtime.
Run new container with updated image.
Redirect traffic (via reverse proxy/load balancer).
Stop the old container.
5. Scenario: A container uses too much CPU and memory.
Limit resources during container run:
docker run -it --cpus=ā1ā --memory=ā512mā <image>