Unit 5: Performance, Monitoring & Troubleshooting
Part J: Performance, Monitoring & Optimization
1. Image Optimization
Use slim/alpine where appropriate, multi-stage builds, .dockerignore, cache-friendly ordering, remove package caches and avoid unnecessary runtime tools.
2. Resource Monitoring & Limits
CPU and memory limits can prevent one container from consuming uncontrolled resources. Monitor usage using:
docker stats
docker top CONTAINER
docker logs -f CONTAINER
3. Disk Usage & Cleanup
docker system df se check karo kitni space use ho rahi hai. Prune commands se un-used data clean karo, par carefully!docker system df
docker system prune
4. Image Reproducibility
Use deliberate image tags/versioning and deterministic dependency installation. Avoid blindly relying on latest for production.
Part K: Complete Troubleshooting
1. Master Debugging Method
Follow this exact flow when things go wrong:
docker ps -a(Check if it exited)docker logs CONTAINER(Read the error)docker inspect CONTAINER(Check configuration)docker exec -it CONTAINER sh(Go inside and debug)- Check ports / network / volume
- Reproduce
- Fix Dockerfile/Compose
- Rebuild and validate
2. Common Scenarios
-p 8081:80).Container Immediately Exits: Containers stay alive only while their main process is running. Run docker logs to see why the main process died.
Database Connection Failed:
- Check both services.
- Use the Compose service name (e.g.,
db), not localhost. - Verify credentials/port.
- Remember that
depends_ondoes not necessarily mean the database application is ready to accept connections.
Code Changes Not Visible:
Build Context Too Large: Improve .dockerignore; exclude .git, virtual environments, caches, local secrets and unnecessary datasets.