Top 20 Docker Interview FAQs
-
What is Docker, and how is it different from a virtual machine?
-
Explain the architecture of Docker (Client, Daemon, Images, Containers, Registries).
-
What is the difference between a Docker image and a Docker container?
-
How do you create a Docker image? What are best practices for writing a Dockerfile?
-
What is the difference between CMD and ENTRYPOINT in a Dockerfile?
-
What are Docker volumes, and how do they differ from bind mounts?
-
How do you persist data in Docker containers?
-
What is the difference between Docker Compose and Docker Swarm?
-
How does Docker handle networking? Explain different network drivers (bridge, host, overlay).
-
How do you share environment variables and secrets in Docker containers securely?
-
What are multi-stage builds in Docker, and why are they useful?
-
How do you optimize the size of a Docker image?
-
What happens when you run
docker runinternally? -
How do you troubleshoot a failing Docker container (logs, exec, inspect)?
-
What is the difference between COPY and ADD in Dockerfile?
-
How do you handle container security best practices in Docker?
-
How do you manage Docker images in production (clean-up, pruning, tagging, versioning)?
-
How do you scale containers in Docker? Difference between Docker Compose scale and Kubernetes?
-
What is the difference between Docker Swarm and Kubernetes? Which one is better?
-
Can you explain a real-world scenario where you used Docker in CI/CD pipelines?
1. What is Docker, and how is it different from a virtual machine?
Answer:
-
Docker is a containerization platform that packages applications and their dependencies in a lightweight, portable container.
-
Difference from VM:
-
VM: Needs full OS → Heavy, slower startup.
-
Docker: Shares host kernel → Lightweight, faster startup.
-
-
Scenario: Instead of running 5 separate VMs for microservices, you run 5 Docker containers on the same host, saving resources.
2. Explain the architecture of Docker.
Answer:
-
Components:
-
Docker Client: CLI (
docker run,docker build). -
Docker Daemon: Runs containers, manages images.
-
Docker Images: Read-only templates.
-
Docker Containers: Running instances of images.
-
Registry (Docker Hub / Private): Stores images.
-
-
Scenario: Developer pushes image to Docker Hub → Production server pulls image → Container runs.
3. Difference between a Docker image and container.
Answer:
-
Image: Blueprint (like a class in OOP).
-
Container: Running instance of an image (like an object).
-
Scenario: An
nginx:latestimage → when run → becomes a container serving HTTP traffic.
4. How do you create a Docker image? Best practices?
Answer:
-
Create using Dockerfile with instructions (
FROM,COPY,RUN, etc.). -
Best practices:
-
Use lightweight base image (e.g.,
alpine). -
Minimize layers.
-
Use
.dockerignore. -
Multi-stage builds.
-
-
Scenario: Instead of a 1GB Node.js image, using Alpine reduces it to 150MB → faster builds.
5. Difference between CMD and ENTRYPOINT.
Answer:
-
CMD: Provides default command, can be overridden.
-
ENTRYPOINT: Defines the executable, harder to override.
-
Scenario:
-
CMD ["npm", "start"]→ can be replaced. -
ENTRYPOINT ["python"]→ ensures container always runs Python.
-
6. What are Docker volumes vs bind mounts?
Answer:
-
Volumes: Managed by Docker, stored under
/var/lib/docker/volumes. -
Bind mounts: Map host directory → container path.
-
Scenario:
-
Volume for DB data persistence.
-
Bind mount for developers editing code locally.
-
7. How do you persist data in Docker?
Answer:
-
Using volumes (recommended) or bind mounts.
-
Scenario: MySQL container stores data in
/var/lib/mysql. If no volume used → data lost on restart. With volume → data persists.
8. Difference between Docker Compose and Docker Swarm.
Answer:
-
Compose: Defines multi-container apps (local dev/test).
-
Swarm: Docker’s native orchestration for scaling & clustering.
-
Scenario:
-
Compose: Run
docker-compose upfor a dev environment. -
Swarm: Deploy same app in production with scaling, load balancing.
-
9. How does Docker networking work?
Answer:
-
Drivers:
-
Bridge: Default, containers talk via IP.
-
Host: Shares host’s network stack.
-
Overlay: Used for multi-host networking (Swarm).
-
-
Scenario: Two microservices (
webanddb) connected viabridgenetwork →webusesdb:3306instead of IP.
10. How do you share environment variables and secrets securely?
Answer:
-
Use
--env-filein Docker run. -
Use Docker Secrets (in Swarm/K8s).
-
Avoid storing secrets in Dockerfile.
-
Scenario: Database password stored in
.envfile instead of hardcoding → secure CI/CD pipelines.
11. What are multi-stage builds in Docker?
Answer:
-
Feature to reduce image size by separating build & runtime.
-
Scenario:
-
Stage 1: Build app using
golang:latest. -
Stage 2: Copy binary to
alpine. -
Final image is only 20MB instead of 1GB.
-
12. How do you optimize Docker image size?
Answer:
-
Use lightweight base images.
-
Combine RUN commands.
-
Remove unnecessary files.
-
Multi-stage builds.
-
Scenario:
FROM ubuntu:20.04(200MB) → replace withFROM alpine(5MB).
13. What happens when you run docker run internally?
Answer:
-
Docker client sends request to daemon.
-
Daemon checks image locally → pulls from registry if missing.
-
Creates container from image.
-
Sets up filesystem, network, mounts.
-
Runs default command.
-
Scenario:
docker run nginx→ pullsnginx→ starts web server.
14. How do you troubleshoot a failing container?
Answer:
-
docker logs <container>→ check logs. -
docker exec -it <container> /bin/bash→ debug inside. -
docker inspect <container>→ metadata. -
Scenario: App container crashes → check logs → missing env variable → fix in Dockerfile.
15. Difference between COPY and ADD in Dockerfile.
Answer:
-
COPY: Copies files from host to container.
-
ADD: Does the same but also supports remote URLs & auto-extraction of tar files.
-
Best practice: Use
COPYunlessADDfeatures required.
16. How do you ensure Docker security?
Answer:
-
Use official images.
-
Scan images for vulnerabilities (
trivy,docker scan). -
Run containers as non-root.
-
Limit container capabilities.
-
Scenario: Running an Nginx container as
root→ security risk. Instead, set user in Dockerfile.
17. How do you manage Docker images in production?
Answer:
-
Tag images properly (
app:1.0, notlatest). -
Clean up unused images (
docker image prune). -
Use private registry (Harbor, ECR, ACR).
-
Automate builds in CI/CD.
18. How do you scale containers in Docker?
Answer:
-
Use
docker-compose scaleor Swarm mode. -
For large scale → Kubernetes.
-
Scenario: 1 container of Node.js can’t handle load → scale to 5 containers behind load balancer.
19. Docker Swarm vs Kubernetes.
Answer:
-
Swarm: Simple, Docker-native, easier setup.
-
K8s: Advanced orchestration, auto-healing, rich ecosystem.
-
Scenario:
-
Swarm → small apps with few nodes.
-
Kubernetes → enterprise-grade clusters with monitoring & scaling.
-
20. Real-world scenario: Using Docker in CI/CD.
Answer:
-
Build app → create Docker image → push to registry → deploy via pipeline.
-
Example:
-
Jenkins pipeline builds Spring Boot app.
-
Creates
myapp:1.0image. -
Pushes to Azure Container Registry.
-
Deploys to AKS using Helm.
-
Comments
Post a Comment