0
0
Dockerdevops~30 mins

Container metrics collection in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Container Metrics Collection
📖 Scenario: You are managing a small set of Docker containers running different services on your local machine. You want to collect basic metrics like CPU and memory usage for each container to monitor their performance.
🎯 Goal: Build a simple Docker setup that collects and stores container metrics in a structured way for easy querying and monitoring.
📋 What You'll Learn
Create a Docker Compose file with two containers running simple services
Add a configuration to enable metrics collection for these containers
Write a command or script to query and display the metrics
Ensure the metrics data is stored or accessible for later analysis
💡 Why This Matters
🌍 Real World
Monitoring container resource usage is essential for maintaining healthy and efficient applications in production environments.
💼 Career
DevOps engineers and system administrators often need to collect and analyze container metrics to optimize performance and troubleshoot issues.
Progress0 / 4 steps
1
Create Docker Compose with two containers
Create a file named docker-compose.yml with two services: webapp running the image nginx:latest and db running the image mysql:5.7. Both should restart always.
Docker
Need a hint?

Use the services key to define two containers with the exact names and images.

2
Enable metrics collection configuration
In the docker-compose.yml, add labels under both webapp and db services with the key com.example.metrics set to enabled.
Docker
Need a hint?

Use the labels key under each service to add the metrics label.

3
Write a command to collect container metrics
Write a shell command that uses docker stats --no-stream --format to display the container name, CPU percentage, and memory usage for containers with the label com.example.metrics=enabled. Store the output in a variable called metrics.
Docker
Need a hint?

Use docker stats with --no-stream and --filter to get metrics only once for labeled containers.

4
Store metrics output to a file
Add a command to save the metrics variable content into a file named container_metrics.txt.
Docker
Need a hint?

Use echo with double quotes and redirect output to the file.