0
0
Dockerdevops~30 mins

Resource monitoring per container in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Resource monitoring per container
📖 Scenario: You are managing multiple Docker containers running different applications on your server. To keep your system healthy, you want to monitor how much CPU and memory each container is using.
🎯 Goal: Build a simple setup to list all running Docker containers and display their CPU and memory usage in a clear way.
📋 What You'll Learn
Use the docker ps command to list running containers.
Use the docker stats --no-stream command to get current CPU and memory usage per container.
Extract container IDs and names to show alongside resource usage.
Display the information in a readable format.
💡 Why This Matters
🌍 Real World
Monitoring resource usage per container helps keep servers stable and prevents any container from using too many resources.
💼 Career
DevOps engineers often monitor container resources to optimize performance and troubleshoot issues in production environments.
Progress0 / 4 steps
1
List running Docker containers
Run the command docker ps --format "{{.ID}} {{.Names}}" to list all running containers with their IDs and names.
Docker
Need a hint?

This command shows container IDs and names in a simple list.

2
Get resource usage snapshot
Run the command docker stats --no-stream --format "{{.Container}} {{.CPUPerc}} {{.MemUsage}}" to get a one-time snapshot of CPU and memory usage for each container.
Docker
Need a hint?

This command shows CPU and memory usage once, without continuous updates.

3
Match container names with resource usage
Use the command docker ps --format "{{.ID}} {{.Names}}" and docker stats --no-stream --format "{{.Container}} {{.CPUPerc}} {{.MemUsage}}" together to match container names with their CPU and memory usage. You can use a shell loop or script to combine this information.
Docker
Need a hint?

This loop gets each container's name and resource usage, then prints them together.

4
Display resource usage per container
Print the container name followed by its CPU and memory usage in the format: container_name: CPU% MemoryUsage. Use the loop from Step 3 to display this information.
Docker
Need a hint?

The output should show each container's name followed by its CPU and memory usage.