Docker Scenario-Based Interview Questions and Detailed Answers (2025)
Preparing for a Docker interview in 2025? Scenario-based questions test your practical problem-solving skills and real-world experience with Docker. This blog covers 10 complex scenarios with detailed solutions, from debugging to production deployments.
1. Scenario: Debugging a Crashing Container
Problem:
A container running a Python Flask app crashes randomly with exit code 137.
Solution Steps:
Check Logs:
docker logs <container_id>- Look for
Out of Memory (OOM)errors.
- Look for
Inspect Resource Limits:
docker inspect <container_id> | grep -i "memory"- If no limits are set, the host might kill the container under memory pressure.
Fix:
- Limit Memory:
docker run -d --memory="512m" --name flask_app flask:latest - Optimize App: Reduce memory leaks (e.g., unbounded caches).
- Limit Memory:
2. Scenario: Docker Build is Too Slow
Problem:
Building a Docker image takes 15 minutes due to large dependencies.
Solution Steps:
Optimize
Dockerfile:- Use multi-stage builds to discard build tools:
FROM python:3.9 as builder COPY requirements.txt . RUN pip install --user -r requirements.txt FROM python:3.9-slim COPY --from=builder /root/.local /root/.local COPY . .
- Use multi-stage builds to discard build tools:
Leverage Build Cache:
- Order commands from least to most frequently changed:
COPY requirements.txt . # Rarely changes → cached RUN pip install -r requirements.txt COPY . . # Frequently changes → last
- Order commands from least to most frequently changed:
Use
.dockerignore:node_modules/ .git/
3. Scenario: Container Can’t Connect to Database
Problem:
A containerized Node.js app fails to connect to a PostgreSQL DB with ECONNREFUSED.
Solution Steps:
Verify Network:
docker network ls docker inspect <network_id> | grep -A 5 "Containers"- Ensure both containers are on the same network.
Test Connectivity:
docker exec -it node_app ping db- If
pingfails, check DNS resolution.
- If
Fix:
- Create a shared network:
docker network create app_net docker run -d --net=app_net --name db postgres docker run -d --net=app_net --name node_app node:latest
- Create a shared network:
4. Scenario: Persistent Data Loss After Container Restart
Problem:
A MySQL container loses data when restarted.
Solution Steps:
Check Volume Usage:
docker inspect mysql_container | grep -A 10 "Mounts"- If no volume is mounted, data is stored in the container’s writable layer (ephemeral).
Fix:
- Use a Named Volume:
docker volume create mysql_data docker run -d -v mysql_data:/var/lib/mysql --name mysql mysql:latest - Backup Volume:
docker run --rm -v mysql_data:/data -v $(pwd):/backup busybox tar cvf /backup/mysql_backup.tar /data
- Use a Named Volume:
5. Scenario: Docker Host Running Out of Disk Space
Problem:
The Docker host throws No space left on device errors.
Solution Steps:
Identify Large Files:
docker system df # Show disk usage docker images --filter "dangling=true" -q | xargs docker rmi # Remove dangling imagesPrune Unused Objects:
docker system prune -a --volumesLimit Log Size:
{ "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } }(Configure in
/etc/docker/daemon.json)
6. Scenario: Securing a Container Running as Root
Problem:
A container runs as root, posing security risks.
Solution Steps:
Run as Non-Root:
FROM alpine RUN adduser -D appuser USER appuserDrop Capabilities:
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE nginxRead-Only Filesystem:
docker run --read-only nginx
7. Scenario: Deploying a Multi-Service App with Docker Compose
Problem:
Need to deploy a web app (Nginx), API (Node.js), and DB (PostgreSQL) with dependencies.
Solution Steps:
Write
docker-compose.yml:version: "3.8" services: web: image: nginx ports: ["80:80"] api: build: ./api depends_on: ["db"] db: image: postgres volumes: ["pg_data:/var/lib/postgresql/data"] volumes: pg_data:Deploy:
docker-compose up -d
8. Scenario: Zero-Downtime Deployment with Docker Swarm
Problem:
Update a service without downtime.
Solution Steps:
Rolling Update:
docker service update \ --image myapp:v2 \ --update-parallelism 2 \ --update-delay 10s \ myappRollback on Failure:
docker service update --rollback myapp
9. Scenario: Debugging Slow Container Startup
Problem:
A container takes 2 minutes to start.
Solution Steps:
Profile Startup:
time docker run myappCheck Entrypoint:
docker inspect myapp | grep -A 5 "Entrypoint"- Optimize slow init scripts.
Use
--initfor Zombie Processes:docker run --init myapp
10. Scenario: Sharing Data Between Containers
Problem:
Two containers need access to the same configuration file.
Solution Steps:
Use a Shared Volume:
docker volume create config_vol docker run -d -v config_vol:/config --name container1 alpine docker run -d -v config_vol:/config --name container2 alpineAlternative: Bind Mount:
docker run -d -v /host/path:/config --name container1 alpine
Final Tips for Scenario-Based Interviews
- Ask Clarifying Questions:
- "Is the container running in Swarm/Kubernetes?"
- "What’s the error message?"
- Explain Trade-offs:
- "Bind mounts are easier for dev, but volumes are better for prod."
- Show Debugging Steps:
- "First, I’d check
docker logs, thendocker inspect..."
- "First, I’d check
Need more scenarios? Drop a comment! 🚀
Practice Challenge:
- Simulate a memory leak in a container and debug it using
docker statsandexec. - Deploy a 3-tier app (frontend, backend, DB) using Compose.
Good luck with your interview! 🐳
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.