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

Hinglish: 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:

  1. docker ps -a (Check if it exited)
  2. docker logs CONTAINER (Read the error)
  3. docker inspect CONTAINER (Check configuration)
  4. docker exec -it CONTAINER sh (Go inside and debug)
  5. Check ports / network / volume
  6. Reproduce
  7. Fix Dockerfile/Compose
  8. Rebuild and validate

2. Common Scenarios

Port Already Allocated: Choose another host port (e.g., -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:

Code Changes Not Visible:

Hinglish: Agar code image ke andar COPY kiya tha, toh rebuild karna padega. Development ke time 'bind mounts' use karo taaki host folder sync rahe.

Build Context Too Large: Improve .dockerignore; exclude .git, virtual environments, caches, local secrets and unnecessary datasets.