Module 13 Lesson 2: Container Performance
Is your app slow or is it Docker? Learn how to monitor CPU, RAM, and I/O performance of your containers using the built-in 'stats' command and external tools.
Module 13 Lesson 2: Analyzing Container Performance
A "Slow" application can have many causes. In Docker, we need to know if the bottleneck is the CPU, the Memory, or the Disk/Network speed.
1. The Built-in Monitor: docker stats
This is the equivalent of "Task Manager" or "Top" for Docker.
docker stats
What to look for:
- CPU %: If this is near 100%, your code is doing too much work or is stuck in a loop.
- MEM USAGE / LIMIT: If usage is near the limit, your app will soon crash with an OOM (Out of Memory) error.
- NET I/O: High numbers here mean your app is sending/receiving massive amounts of data.
- BLOCK I/O: High numbers here mean your app is doing a lot of "Disk Reading/Writing" (Common for databases).
2. Advanced Performance: docker top and inspect
docker top <id>: Shows you exactly which Linux processes are running inside that specific container and their IDs.docker inspect <id> --format='{.State.Health.Status}': Programmatically check if the app is according to its healthcheck.
3. Detecting OOM Kills
If your container disappears without an error message, it was likely killed by the OS for taking too much RAM.
docker inspect <id> | grep OOMKilled
If it says "OOMKilled": true, you need to either give the container more memory or optimize your code's memory usage.
4. Benchmarking Your Container
You can use a "Stress Test" image to see how your computer handles heavy Docker loads.
docker run --rm -it progrium/stress --cpu 2 --io 1 --vm 1 --vm-bytes 128M --timeout 10s
- Open a second terminal and run
docker stats. You will see the metrics spike.
Exercise: The Stress Test
- Run
docker statsin your terminal. - In another terminal, start a heavy container (like a local database or a compile job).
- Watch the
MEM USAGE. How much did it jump? - Run
docker topon that container. Who is the "Parent" process? What is thePID(Process ID)? - Why is it better to use
docker statsrather than the host's standard "Activity Monitor" or "Task Manager"? (Hint: Think about Isolation).
Summary
Performance monitoring allows you to stop "Guessing" why your app is slow. By mastering the stats command, you can spot memory leaks and CPU bottlenecks before they crash your production environment.
Next Lesson: Keeping it clean: Docker system cleanup and maintenance.