Kritim Yantra
Apr 12, 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.
Problem:
A container running a Python Flask app crashes randomly with exit code 137
.
Check Logs:
docker logs <container_id>
Out of Memory (OOM)
errors.Inspect Resource Limits:
docker inspect <container_id> | grep -i "memory"
Fix:
docker run -d --memory="512m" --name flask_app flask:latest
Problem:
Building a Docker image takes 15 minutes due to large dependencies.
Optimize Dockerfile
:
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 . .
Leverage Build Cache:
COPY requirements.txt . # Rarely changes → cached
RUN pip install -r requirements.txt
COPY . . # Frequently changes → last
Use .dockerignore
:
node_modules/
.git/
Problem:
A containerized Node.js app fails to connect to a PostgreSQL DB with ECONNREFUSED
.
Verify Network:
docker network ls
docker inspect <network_id> | grep -A 5 "Containers"
Test Connectivity:
docker exec -it node_app ping db
ping
fails, check DNS resolution.Fix:
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
Problem:
A MySQL container loses data when restarted.
Check Volume Usage:
docker inspect mysql_container | grep -A 10 "Mounts"
Fix:
docker volume create mysql_data
docker run -d -v mysql_data:/var/lib/mysql --name mysql mysql:latest
docker run --rm -v mysql_data:/data -v $(pwd):/backup busybox tar cvf /backup/mysql_backup.tar /data
Problem:
The Docker host throws No space left on device
errors.
Identify Large Files:
docker system df # Show disk usage
docker images --filter "dangling=true" -q | xargs docker rmi # Remove dangling images
Prune Unused Objects:
docker system prune -a --volumes
Limit Log Size:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
(Configure in /etc/docker/daemon.json
)
Problem:
A container runs as root
, posing security risks.
Run as Non-Root:
FROM alpine
RUN adduser -D appuser
USER appuser
Drop Capabilities:
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE nginx
Read-Only Filesystem:
docker run --read-only nginx
Problem:
Need to deploy a web app (Nginx), API (Node.js), and DB (PostgreSQL) with dependencies.
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
Problem:
Update a service without downtime.
Rolling Update:
docker service update \
--image myapp:v2 \
--update-parallelism 2 \
--update-delay 10s \
myapp
Rollback on Failure:
docker service update --rollback myapp
Problem:
A container takes 2 minutes to start.
Profile Startup:
time docker run myapp
Check Entrypoint:
docker inspect myapp | grep -A 5 "Entrypoint"
Use --init
for Zombie Processes:
docker run --init myapp
Problem:
Two containers need access to the same configuration file.
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 alpine
Alternative: Bind Mount:
docker run -d -v /host/path:/config --name container1 alpine
docker logs
, then docker inspect
..."Need more scenarios? Drop a comment! 🚀
Practice Challenge:
docker stats
and exec
. Good luck with your interview! 🐳
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 7, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google