Docker transforms how you deploy applications by providing lightweight, portable containers that run anywhere.
However, as your containerized applications scale in production, you need to monitor their performance, resource utilization, and health. Good container monitoring helps you find bottlenecks, prevent outages, and optimize resources across your infrastructure.
This guide shows you how to build robust monitoring solutions for your Docker containers.
Prerequisites
Before you start container monitoring, ensure that Docker is installed on your system. This guide assumes you're running Docker version 20.04 or later; however, most concepts are also applicable to earlier versions. You should also know basic Docker concepts like containers, images, and volumes.
Setting up your environment
To get the most out of this tutorial, create a practical environment with multiple containers that you can monitor throughout the guide. This hands-on approach helps you understand monitoring concepts while building real skills.
Start by creating a new directory for your monitoring project:
Create a simple web application that you can monitor. First, build a basic Node.js application:
Create a package.json file:
Now, create a Dockerfile for your application:
Build and run the application:
Verify the application works:
Or in the browser like this:
You should see JSON responses confirming the application works. You'll use this application throughout the tutorial to demonstrate various monitoring techniques.
Basic container monitoring with Docker commands
Now that you have a running container, explore Docker's built-in monitoring capabilities. These native tools give you immediate insights without requiring additional software.
Real-time container statistics
The docker stats command shows live performance data for your running containers:
You'll see output like this:
Generate some load to see the metrics change. In another terminal, generate some requests:
Watch how the CPU percentage and network I/O values increase in the docker stats output. This real-time feedback helps you understand your container's resource consumption patterns.
To capture a snapshot instead of continuous monitoring:
Examining container details
While docker stats shows real-time performance data, you often need more detailed information about your container's configuration and current state. The docker inspect command provides comprehensive details about how your container is configured and running.
Get complete container information in JSON format:
This produces extensive JSON output with hundreds of configuration details. For monitoring purposes, you'll typically want to extract specific information using formatting options.
Check if any memory limits are configured for your container:
A value of 0 indicates no memory limit is set. Verify your container's current running status:
You can also check the boolean running state:
Process monitoring inside containers
Beyond container-level statistics, you need visibility into the processes running inside your containers. This helps identify resource-intensive operations and understand what's happening within your application.
View processes running inside your container using Docker's top command:
For more detailed process information:
You'll see the Node.js process and any child processes:
This information helps you identify resource-intensive operations within your containers.
Check system resources from inside the container:
These commands show how the container sees system resources, which can differ from the host system's perspective.
Implementing container health checks
Building on your basic monitoring, implement health checks for your web application. Health checks provide automated container health assessment beyond simply checking if the process runs.
Adding health checks to your application
Health checks go beyond basic process monitoring by actively testing your application's functionality. Docker can automatically run health check commands at regular intervals to verify your application is responding correctly.
First, rebuild your container with a health check. Update the Dockerfile:
The HEALTHCHECK instruction configures Docker to test your /health endpoint every 30 seconds. If the health check fails 3 times in a row, Docker marks the container as unhealthy.
Stop the existing container to prepare for the rebuild:
Remove the container completely:
Build the updated image with health check capabilities:
Run the new container with integrated health monitoring:
Monitoring health check results
Now that your container includes health checks, Docker automatically monitors your application's health and reports the status. This provides more reliable monitoring than simply checking if the process is running.
Check the health status of your container:
You'll see a status like Up 25 seconds (healthy) once the health check passes. To see detailed health check information:
This command shows the last several health check results, including timestamps and exit codes.
Testing health check failure
Simulate a health check failure by stopping the application inside the container:
Monitor the health status in a second terminal:
You'll see the status change from healthy to unhealthy after the configured retries fail. This shows how health checks detect application failures even when the container still runs.
Restart the container to restore normal operation:
The status will show (health: starting) and then change to (healthy) once the health checks pass again:
Container log analysis and monitoring
Now explore container logging, which is crucial for monitoring application behavior and troubleshooting issues. You'll build on your existing setup to implement comprehensive log monitoring.
Accessing and analyzing container logs
Docker captures all output from your application's standard output and error streams. The docker logs command provides access to these logs for analysis and troubleshooting.
View logs from your web application:
For continuous monitoring, use the follow flag to watch logs in real-time:
Final thoughts
Throughout this tutorial, you've built a solid foundation for Docker container monitoring using basic commands, health checks, and log analysis. These techniques provide essential visibility into your containerized applications and help you detect issues quickly.
As your applications grow, you'll need to expand these techniques with more advanced tools like Prometheus and Grafana. The foundation you've built here will serve you well as you scale your monitoring infrastructure.
For more advanced monitoring techniques, explore the official Docker documentation and consider integrating with cloud-native monitoring solutions like Better Stack.
Thanks for following along with this Docker monitoring tutorial, and happy monitoring!