Docker is a powerful tool that enables developers to package, deploy, and run applications in a containerized environment. Docker containers are portable and lightweight, making them an attractive choice for developers to build, test, and deploy applications.
Docker containers also provide an isolated environment for applications to run, which can help ensure that apps don’t interfere with each other and allow developers to run multiple versions of an application on the same host. Docker containers help applications’ easy distribution and scalability across different machines and environments.
However, running applications in Docker containers also introduces new challenges, such as monitoring your containers’ health and performance. In this article, we will take a deep dive into Docker container monitoring. We’ll cover the importance of monitoring Docker containers, the key metrics to track, and the various tools and techniques available for monitoring them. Lastly, we’ll discuss the best practices for Docker container monitoring.
- What is Docker container monitoring?
- Why is it important to monitor Docker containers?
- Monitoring Docker containers in real time?
- Open source tools for monitoring Docker containers
What is Docker container monitoring?
Docker container monitoring is the process of tracking the performance, health, and resource utilization of applications running in Docker containers. Some of the standard metrics monitored in the Docker container are:
- Resource utilization metrics: Metrics that refer to the resources allocated to and used by the Docker containers. Usage can indicate if the container is experiencing a workload too heavy, optimal, or light and helps troubleshoot issues with the application running inside the container. Some examples of these metrics include:
- CPU usage: The percentage of CPU time being used by the container
- Memory usage: The amount of memory being used
- Disk usage: The amount of disk space being used
- Network usage: The amount of network bandwidth being used
- Container status metrics: These metrics refer to the status of the Docker containers to help track availability, overall health, and identify issues related to the container. Some examples of these metrics are:
- Container uptime: The amount of time that the container has been running. High uptime can be an indicator of the stability and reliability of the container
- Container restarts: The number of times that the container has been restarted. A high number of restarts can be an indicator of issues with the container or the application running inside it
- Container exits: The number of times that the container has exited. High numbers of container exits can be an indicator of issues
- Status: This metric refers to the status of the container itself (e.g. running, stopped, restarting)
- Application performance metrics: Metrics that refer to the performance of the applications running inside Docker containers. Some examples are:
- Response time: The time it takes for the containerized application to respond to a request. Tracking response time can help identify performance issues and ensure that the application is responsive to end users
- Request rate: The number of requests being handled by the containerized application per second
- Error rate: The number of errors encountered by the containerized application
- Host machine metrics: Metrics that refer to the performance of the physical or virtual machine on which Docker is running. These metrics are essential to monitor because they can help identify issues related to the overall health and performance of the host machine, which can impact the performance and availability of the Docker containers running on the host. Some examples are:
- CPU usage
- Memory usage
- Disk usage
- Network usage
- Logs: Logs provide a detailed record of the activities and events inside the container. It refers to tracking and analyzing the log output generated by a Docker container
- Processes: Programs or tasks that are running inside the container
Why is it important to monitor Docker containers?
Containers are a vital component of modern application development and deployment, and being able to monitor them effectively is crucial to the success of any containerized application.
Docker container monitoring is important because it allows you to ensure the health and performance of your containers, which are critical to the overall operation of your applications. It ensures that containers run efficiently and helps identify and address any issues in a timely manner. There are several reasons why it is important to monitor Docker containers:
- Resource utilization: Monitoring the resource utilization of Docker containers can help ensure that containers are running optimally and have sufficient resources to perform their tasks. This visibility into the resource usage of your containers can also help plan for future capacity needs
- Security: Monitoring your Docker containers can help you detect and prevent security breaches, such as unauthorized access or malicious code injection
- Cost optimization: Optimizing the usage of your resources leads to cost savings.
- Compliance: In some cases, monitoring your Docker containers may require compliance with industry regulations or standards
- Health and availability: Monitoring the health and availability of your Docker containers can help you detect and resolve issues quickly, ensuring that your applications are always available to your users
- Monitoring: Can help identify bottlenecks, optimize performance, alert to issues, and provide detailed logs and metrics for debugging.
Monitoring Docker containers in real time
There are several ways to monitor Docker containers in real time:
docker logs: View the logs of a running container
docker attach: Attach to a running container and view its output
docker top: View the running processes of a container
docker events: View real-time events from the Docker daemon, such as when a container is created or destroyed
- Third-party tools: Several third-party tools can monitor Docker containers in real time. These tools typically provide more advanced monitoring and visualization features, and integration with other IT systems and tools
Open source tools for monitoring Docker containers
docker stats command
docker stats is a simple built-in command used in the Docker command-line interface (CLI) to view real-time performance and resource usage statistics for all running containers. To use the command, Docker must first be installed on the host system.
The output of the
docker stats command includes the following columns:
- CONTAINER ID: The unique identifier for the container
- NAME: The name of the container
- CPU %: The percentage of CPU usage
- MEM USAGE / LIMIT: The amount of memory the container uses and its memory limit
- MEM %: The percentage of memory usage
- NET I/O: The network input and output
- BLOCK I/O: The block input and output
- PIDS: The number of processes running in the container
docker stats command will default display statistics for all running containers. However, a specific container can be specified by passing its name or ID as an argument like so:
docker stats container-id.
docker stats command has several options that allow output customization, such as the
--all option to show all containers, even those that have exited, and the
--no-stream option to disable the live stream and show a snapshot of the current resource usage instead. There is also the
--format option to specify a custom format for the output. This option uses the Go template syntax, which allows you to define the layout and contents of the output by using variables and control structures.
docker stats can be an effective way to monitor containers’ performance and resource usage in real time. It can be handy for identifying and troubleshooting issues with your applications. However, it is important to note that
docker stats only provides a high-level overview of resource usage, and more is needed for more detailed and comprehensive monitoring and analysis.
Prometheus is an open source monitoring and alerting system well-suited for monitoring the resource usage and health of Docker containers. To use Prometheus for monitoring Docker containers, the Prometheus server must be set up and configured to scrape metrics from your containers.
The Prometheus query language, PromQL, can be used to create alerts and graphs that allow you to monitor the performance of your containers. There are several ways to integrate Prometheus with Docker. Still, one standard method is to use the Prometheus Docker Hub exporter, a container that collects metrics from the Docker daemon and exports them to Prometheus. You can then configure the Prometheus server to scrape metrics from the exporter container.
There are several key features of Prometheus that make it well-suited for monitoring Docker containers:
- Scalability: Prometheus is designed to be horizontally scalable, which means it can handle a large number of Docker containers without performance degradation
- Alerting: Prometheus includes a built-in alert manager that can send notifications based on defined rules. Notifications are conditional on specific rules, for example, when a container’s CPU usage exceeds a threshold
- Query language: Prometheus provides a powerful query language called PromQL that allows you to filter, aggregate, and analyze metrics in real time. This is useful for creating alerts and generating graphs that help you understand the performance of your containers
- Grafana integration: Prometheus integrates seamlessly with Grafana, a popular open source tool for creating dashboards and visualizing data. This allows you to create custom dashboards that display the metrics you care about in a way that is easy to understand.
- Flexibility: Prometheus is not tied to any specific technology or platform, which means it can be used to monitor Docker containers running on various systems
- Customizability: Prometheus allows the definition of custom metrics and configurations on collection and processing operations. This feature enables the monitoring of specific aspects of containers relevant to the respective environment.
Overall, Prometheus is a powerful tool for monitoring Docker containers, and it is a popular choice among DevOps professionals.
cAdvisor, also known as Container Advisor, is an open source tool developed by Google for monitoring and managing containerized applications. It provides detailed information about the resource usage of individual containers and helps administrators optimize their containerized applications’ resource utilization.
cAdvisor runs as a standalone service within a Docker container and collects resource usage data from all the containers running on the same host. It then provides this information through graphs and tables through a simple web interface. cAdvisor requires that Docker is installed on your host. With a working installation of Docker on the host, pull the cAdvisor image from the Docker Hub and start the cAdvisor container as follows:
docker pull google/cadvisor:latest.
Next, start the cAdvisor container:
docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=8080:8080 --detach=true --name=cadvisor google/cadvisor:latest
This command will start the cAdvisor container in detached mode, running in the background. The
--publish flag maps the container’s port 8080 to the host’s port 8080, so you can access the cAdvisor web interface at http://localhost:8080.
By default, cAdvisor will collect resource usage data for all containers on the host. You can customize the behavior of cAdvisor by using command line flags or setting environment variables when starting the container. For a complete list of options, you can consult the cAdvisor documentation.
Once cAdvisor is up and running, you can access the cAdvisor web interface to view resource usage data for individual containers. The interface provides several real-time graphs and tables that display resource usage data. From the web interface, containers can be labeled with custom metadata, grouped by various criteria, and have alerts set for when the resource usage of a container exceeds certain thresholds.
There are several key features of cAdvisor that make it well-suited for monitoring Docker containers:
- Real-time resource usage data: cAdvisor provides detailed resource usage data for individual containers, including CPU, memory, and network usage. This data is updated in real time, allowing administrators to identify and troubleshoot resource contention issues quickly
- Historical resource usage data: cAdvisor stores historical resource usage data for each container, allowing administrators to see how the resource usage of a container has changed over time. This feature is helpful for capacity planning and performance analysis
- Container labeling: cAdvisor allows administrators to label containers with custom metadata, which can be useful for grouping and filtering containers in the cAdvisor web interface
- Container grouping: cAdvisor allows grouping containers by various criteria, such as the host they are running on or the Docker image they are based on. This feature can help compare the resource usage of different groups of containers
- Container alerts: cAdvisor includes the ability to set alerts for when the resource usage of a container exceeds certain thresholds. This feature can be helpful for proactively identifying and addressing resource contention issues
- Integration with other tools: cAdvisor integrates with several different tools, such as Google Cloud Monitoring, Grafana, and Prometheus, allowing administrators to view and analyze resource usage data within the context of their overall monitoring and management strategy.
Docker container monitoring is a critical part of managing and operating containerized environments. By monitoring your containers, you can ensure that your applications are running smoothly, identify and troubleshoot issues as they arise, and optimize the performance of your containers.
Several open source tools are available for monitoring Docker containers in real time. These tools provide a range of features, including multidimensional data models, powerful query languages, efficient time series databases, and flexible alerting systems.
Some popular open source options include Prometheus, Grafana, and cAdvisor. Ultimately, the right tool for monitoring your Docker containers will depend on your specific needs and requirements. However, by leveraging one of these open source tools, you can gain valuable insights into the performance and behavior of your containers and ensure that your applications are running smoothly.
Get setup with LogRocket's modern error tracking in minutes:
- Visit https://logrocket.com/signup/ to get an app ID.
- Install LogRocket via NPM or script tag.
LogRocket.init()must be called client-side, not server-side.
- (Optional) Install plugins for deeper integrations with your stack:
- Redux middleware
- ngrx middleware
- Vuex plugin
$ npm i --save logrocket
import LogRocket from 'logrocket';
Add to your HTML:
<script>window.LogRocket && window.LogRocket.init('app/id');</script>