test
Here are comprehensive Docker interview questions organized by level:
🟢 Beginner Level
Concepts
Q1: What is Docker and why is it used?
Docker is an open-source containerization platform that packages applications and their dependencies into lightweight, portable containers — ensuring they run consistently across any environment (dev, staging, production).
Q2: What is the difference between a container and a virtual machine?
| Container | Virtual Machine | |
| OS | Shares host OS kernel | Has its own OS |
| Size | Lightweight (MBs) | Heavy (GBs) |
| Startup | Seconds | Minutes |
| Isolation | Process-level | Full hardware-level |
| Performance | Near-native | Overhead |
Q3: What is a Docker image vs a Docker container?
- Image — A read-only blueprint/template used to create containers
- Container — A running instance of an image
Q4: What is a Dockerfile?
A text file containing step-by-step instructions to build a Docker image automatically.
Q5: What is Docker Hub?
A public cloud-based registry where Docker images are stored, shared, and distributed.
Basic Commands
Q6: What are the most common Docker commands?
docker build -t myapp . # Build image
docker run -d -p 8080:80 myapp # Run container
docker ps # List running containers
docker ps -a # List all containers
docker stop <container_id> # Stop container
docker rm <container_id> # Remove container
docker images # List images
docker rmi <image_id> # Remove image
docker logs <container_id> # View logs
docker exec -it <id> /bin/bash # Enter container shell
Q7: What is the difference between CMD and ENTRYPOINT?
| CMD | ENTRYPOINT | |
| Purpose | Default command, easily overridden | Fixed command, always executes |
| Override | Yes, at runtime | Only with –entrypoint flag |
| Use case | Flexible defaults | Enforced commands |
ENTRYPOINT [“python”] # always runs python
CMD [“app.py”] # default arg, can be overridden
Q8: What is the difference between COPY and ADD?
- COPY — Simply copies files from host to container (preferred)
- ADD — Same as COPY but also supports URLs and auto-extracts tar files
🟡 Intermediate Level
Networking
Q9: What are Docker network types?
| Network | Description | Use Case |
| bridge | Default, isolated network | Single host containers |
| host | Shares host network stack | High performance needs |
| none | No networking | Fully isolated containers |
| overlay | Multi-host networking | Docker Swarm / distributed apps |
docker network create my-network
docker run –network my-network myapp
Q10: How do containers communicate with each other?
Containers on the same custom bridge network can communicate using their container name as hostname.
# Both containers on same network can reach each other by name
docker run –network my-net –name db postgres
docker run –network my-net –name app myapp # app can reach “db”
Volumes & Storage
Q11: What is the difference between volumes, bind mounts, and tmpfs?
| Type | Description | Use Case |
| Volume | Managed by Docker | Persistent data (databases) |
| Bind Mount | Maps host directory to container | Development, live code reload |
| tmpfs | Stored in memory only | Sensitive/temporary data |
docker run -v myvolume:/data myapp # volume
docker run -v /host/path:/container myapp # bind mount
Q12: How do you persist data in Docker?
Use named volumes — data persists even after the container is removed.
docker volume create mydata
docker run -v mydata:/app/data myapp
Docker Compose
Q13: What is Docker Compose and when do you use it?
Docker Compose defines and runs multi-container applications using a single docker-compose.yml file.
version: “3.8”
services:
app:
build: .
ports:
– “8080:80”
depends_on:
– db
environment:
– DB_HOST=db
db:
image: postgres:15
volumes:
– pgdata:/var/lib/postgresql/data
environment:
– POSTGRES_PASSWORD=secret
volumes:
pgdata:
docker-compose up -d # Start all services
docker-compose down # Stop and remove
docker-compose logs -f # Follow logs
Q14: What is the difference between docker-compose up and docker-compose start?
- up — Creates and starts containers (builds if needed)
- start — Starts existing stopped containers only
Images & Optimization
Q15: How do you reduce Docker image size?
- Use minimal base images like alpine
- Use multi-stage builds
- Combine RUN commands to reduce layers
- Use .dockerignore to exclude unnecessary files
# Multi-stage build example
FROM node:18 AS builder
WORKDIR /app
COPY . .
RUN npm run build
FROM nginx:alpine
COPY –from=builder /app/dist /usr/share/nginx/html
Q16: What is a .dockerignore file?
Similar to .gitignore — tells Docker which files to exclude from the build context.
node_modules
.git
*.log
.env
dist
🔴 Advanced Level
Security
Q17: How do you secure Docker containers?
- Run containers as non-root user
- Use read-only filesystems where possible
- Scan images for vulnerabilities (docker scout)
- Limit container capabilities with –cap-drop
- Never store secrets in Dockerfiles — use Docker Secrets or environment variables
# Run as non-root
RUN adduser –disabled-password appuser
USER appuser
Q18: What is the difference between docker save and docker export?
| docker save | docker export | |
| Target | Image | Container |
| Includes | All layers & history | Flattened filesystem only |
| Use case | Backup/transfer images | Snapshot a running container |
Performance & Production
Q19: How do you limit container resources?
docker run \
–memory=”512m” \
–cpus=”1.0″ \
–memory-swap=”1g” \
Myapp
Q20: What is the difference between Docker Swarm and Kubernetes?
| Docker Swarm | Kubernetes | |
| Complexity | Simple | Complex but powerful |
| Setup | Easy | Steeper learning curve |
| Scaling | Basic auto-scaling | Advanced auto-scaling |
| Community | Smaller | Very large |
| Best for | Small–medium workloads | Large enterprise workloads |
Q21: What happens when a Docker container crashes?
Use restart policies to handle crashes automatically:
docker run –restart=always myapp # Always restart
docker run –restart=on-failure:3 myapp # Restart up to 3 times on failure
docker run –restart=unless-stopped myapp # Restart unless manually stopped
Dockerfile Best Practices
Q22: What are Dockerfile best practices?
# ✅ Good Dockerfile
FROM node:18-alpine # Use minimal base image
WORKDIR /app
COPY package*.json ./ # Copy dependency files first
RUN npm ci –only=production # Install dependencies
COPY . . # Copy source code
RUN adduser –disabled-password app # Create non-root user
USER app # Switch to non-root
EXPOSE 3000
CMD [“node”, “server.js”]
⚡ Quick-Fire Questions
| Question | Answer |
| Default Docker network? | bridge |
| Docker config file location? | /etc/docker/daemon.json |
| How to see container resource usage? | docker stats |
| How to copy files into a container? | docker cp file.txt container:/path |
| Difference between stop and kill? | stop = graceful (SIGTERM), kill = forceful (SIGKILL) |
| What is a dangling image? | An image with no tag, created by rebuilds |
| How to clean up unused resources? | docker system prune |