Docker Interview Questions and Answers
Anand Mohan is a Software Developer turned DevOps Engineer and a Public speaker. Anand has worked with multiple startups and MNCs and helped them scale their Development and Deployment and community. Anand currently working with HeaTrec Solutions. In his leisure time, Anand writes about Web Development, Cloud technologies, and DevOps . Anand also creates educational content on multiple platforms like Twitter and LinkedIn and Own Blogging Websites.
1. Basic Docker Questions
1.1 What is Docker?
Answer:
Docker is a containerization platform that allows developers to package applications and dependencies into lightweight, portable containers. It ensures that applications run consistently across different environments.
1.2 What are Docker’s key components?
Answer:
Docker Engine: The core service that runs containers.
Docker CLI: Command-line tool to interact with Docker.
Docker Images: Read-only templates for containers.
Docker Containers: Running instances of images.
Docker Hub: Public registry for storing images.
Docker Compose: Tool for managing multi-container applications.
1.3 How is Docker different from Virtual Machines (VMs)?
| Feature | Docker (Containers) | Virtual Machines |
| Size | Lightweight (MBs) | Heavy (GBs) |
| Boot Time | Seconds | Minutes |
| Performance | Faster | Slower |
| Isolation | Process-level | Full OS |
| Resource Usage | Shares host kernel | Requires full OS |
2. Docker Architecture & Workflow
2.1 Explain the Docker architecture.
Answer:
Docker follows a client-server architecture with:
Docker Client: Runs
dockercommands.Docker Daemon: Manages images, containers, and networks.
Docker Registry (Docker Hub/Private Registry): Stores and distributes images.
Docker Containers: Run applications using containerized environments.
2.2 How does Docker work?
Answer:
Developer writes a Dockerfile.
Builds an image using
docker build.Runs a container using
docker run.Pushes image to Docker Hub or a private registry.
Deploys the container on any environment.
3. Docker Commands
3.1 What are the most commonly used Docker commands?
Answer:
| Command | Description |
docker build -t myapp . | Build an image from a Dockerfile |
docker run -d -p 8080:80 myapp | Run a container in detached mode |
docker ps -a | List all containers |
docker stop <container_id> | Stop a running container |
docker rm <container_id> | Remove a container |
docker rmi <image_id> | Remove an image |
docker logs <container_id> | View container logs |
docker exec -it <container_id> /bin/sh | Access running container shell |
docker-compose up -d | Start services in docker-compose.yml |
3.2 How do you check running containers?
docker ps
3.3 How do you restart a stopped container?
docker start <container_id>
4. Dockerfile & Image Management
4.1 What is a Dockerfile?
Answer:
A Dockerfile is a text file with instructions to automate the creation of a Docker image.
4.2 What are key Dockerfile instructions?
| Instruction | Purpose |
FROM | Specifies the base image |
LABEL | Adds metadata |
COPY / ADD | Copies files into the container |
WORKDIR | Sets the working directory |
RUN | Executes commands at build time |
CMD / ENTRYPOINT | Defines the startup command |
EXPOSE | Declares the container's port |
ENV | Sets environment variables |
4.3 Example of a Dockerfile
FROM node:16
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["node", "server.js"]
4.4 Difference between COPY and ADD?
| Feature | COPY | ADD |
| Basic File Copy | ✅ | ✅ |
| Extract Tar Archives | ❌ | ✅ |
| Supports URLs | ❌ | ❌ |
5. Docker Networking
5.1 What are different Docker network types?
| Network | Description |
| Bridge (Default) | Containers communicate on a private network |
| Host | Container shares the host network |
| Overlay | For multi-host networking (Swarm) |
| None | No networking, isolated container |
5.2 How to connect two containers?
docker network create mynetwork
docker run --network=mynetwork myapp
6. Docker Volumes & Data Persistence
6.1 What is a Docker volume?
Answer:
Docker volumes persist data beyond the container's lifecycle.
6.2 How to create and mount a volume?
docker volume create mydata
docker run -v mydata:/app/data myapp
7. Docker Compose & Multi-Container Apps
7.1 What is Docker Compose?
Answer:
Docker Compose manages multi-container applications using a YAML file.
7.2 Example docker-compose.yml file
version: "3"
services:
web:
image: nginx
ports:
- "8080:80"
app:
build: .
depends_on:
- db
db:
image: postgres
environment:
POSTGRES_PASSWORD: mypassword
7.3 Commands for Docker Compose
docker-compose up -d # Start all services
docker-compose down # Stop all services
docker-compose ps # Check running services
8. Docker Security & Best Practices
8.1 How to secure Docker containers?
✅ Run as non-root user (USER appuser)
✅ Use minimal base images (e.g., alpine)
✅ Limit container resources (--memory=512m --cpu=0.5)
✅ Use multi-stage builds to reduce attack surface
✅ Enable container scanning (docker scan myimage)
9. Docker Swarm & Kubernetes
9.1 What is Docker Swarm?
Answer:
Docker Swarm is Docker’s native orchestration tool for managing multiple containers across nodes.
9.2 What is Kubernetes?
Answer:
Kubernetes (K8s) is an orchestration platform for deploying, managing, and scaling containerized applications.
9.3 Docker Swarm vs Kubernetes
| Feature | Docker Swarm | Kubernetes |
| Complexity | Simple | Complex |
| Scaling | Manual or automatic | Advanced scaling |
| Networking | Built-in | Requires setup |
| Storage | Limited | Advanced options |
10. Advanced Docker Questions
10.1 What is a multi-stage build?
Multi-stage builds reduce final image size by separating build & runtime environments.
# Build stage
FROM node:16 AS builder
WORKDIR /app
COPY . .
RUN npm install && npm run build
# Final stage
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
10.2 How to optimize Docker images?
✅ Use .dockerignore to exclude unnecessary files
✅ Use multi-stage builds
✅ Minimize RUN layers
✅ Use alpine base images
Final Thoughts
These Docker interview questions cover everything from basics to advanced topics like security, networking, volumes, Kubernetes, and Swarm.
Would you like mock interview questions or real-world Docker scenario-based problems? 🚀