Monitoring Docker Container Resource Usage: A Comprehensive Guide

how to monitor docker container resource usage

Docker is a containerization platform that allows users to separate their applications from their infrastructure, facilitating rapid software delivery. Monitoring Docker involves gathering performance-related metrics from various system components, including containers, hosts, and databases. This is crucial for maintaining optimal performance and troubleshooting issues.

There are several methods to monitor Docker container resource usage, including the Docker stats command, pseudo-files in sysfs, and the REST API exposed by the Docker daemon. The Docker stats command is a built-in feature that provides real-time resource consumption statistics for containers, such as CPU and memory utilisation. It supports CPU, memory usage, memory limit, and network IO metrics.

Additionally, Docker Desktop users can utilise the Resource Usage extension to view container resource usage, offering a user-friendly interface and the ability to stop and start containers.

Characteristics Values
Command to monitor resource usage docker stats
Display options --all, --format, --no-stream, --no-trunc
Resource usage metrics CPU %, MEM USAGE/LIMIT, MEM %, NET I/O, BLOCK I/O, PIDS
Resource usage monitoring tools Docker stats command, Pseudo-files in sysfs, REST API exposed by Docker daemon

shundigital

Using the docker stats command

Docker has a built-in stats command that allows you to monitor the resource usage of your containers. By simply typing $ docker stats in your CLI, you can view the CPU, memory, network, and disk usage of all your running containers. The docker stats command returns a live data stream for running containers, providing valuable insights into their resource utilisation.

The command supports various metrics, including CPU, memory usage, memory limit, and network I/O. The data is presented in columns, displaying the container ID, name, CPU and memory usage percentages, memory usage and limit, network I/O, block I/O, and the number of processes or threads created.

To limit the data to specific containers, you can specify a list of container names or IDs separated by spaces. Additionally, you can use the /containers/(id)/stats API endpoint to obtain more detailed information about a container's resource usage.

The docker stats command is a powerful tool for monitoring container resource usage, providing a live view of the resources utilised by your containers. However, it only provides a snapshot of a particular moment in time. For more advanced analysis and insights, you may consider using additional tools or extensions, such as the Resource Usage extension offered by Docker.

shundigital

Viewing the Resource Usage with Docker Desktop

Docker Desktop provides a built-in stats command that allows you to monitor the resource usage of your Docker containers in real time. By simply using the command $ docker stats in your CLI, you can view the resource consumption statistics for all your running containers. This includes CPU, memory, network, and disk usage.

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.

The docker stats command also offers several display options to customise the output:

  • --all: Shows all containers, whether stopped or running.
  • --format: Uses the Go Template syntax to print images out.
  • --no-stream: Disables streaming stats and only shows a snapshot of the current resource usage.
  • --no-trunc: Prevents Docker from truncating or shortening the output.

While the docker stats command is useful for monitoring resource usage, it only provides a current snapshot. If you want to track the evolution of resource usage over time, you can use the Resource Usage extension. This extension compiles data from the Docker stats command into a user interface, allowing you to analyse resource-intensive containers, observe changes in resource usage over time, and view CPU, memory, network, and disk space usage.

shundigital

Using the Resource Usage extension

The Resource Usage extension is a powerful tool that provides insights into how containers are using resources, making it easier to optimise them. It is available on Docker Hub and under Extensions in Docker Desktop.

The extension shows which containers consume the most resources on their development machines by running the Docker stats command and then compiling the data into a user interface. With this extension, you can quickly analyse the most resource-intensive containers or Docker Compose projects and observe how resource usage changes over time.

The cards at the top give a global overview of the resources in use, including the number of running containers. The Table view shows the details for your containers, and you can customise the view by selecting which columns to display and which values to filter. The available columns are:

  • NAME – The container name
  • STATUS – The container status (running or not running)
  • CPU (%) – The percentage of the host’s CPU and memory used by the container
  • MEM (%) – The amount of memory used out of the total limit
  • DISK READ/WRITE – The amount of data the container has read to and written from block devices on the host
  • NETWORK I/O – The amount of data sent and received over its network interface
  • PIDs – The number of processes or threads the container has created

The Chart view provides a visual representation of usage over time.

To install the Resource Usage extension, open Docker Desktop, click the EXT option on the left sidebar, type "Resource usage" in the search field, and then click "Install". Once installed, the Resource usage tab will be available on the sidebar, providing a real-time listing of each container. You can also view a Chart view of the containers and modify the refresh rate for the extension (from 1 second to 5 minutes).

shundigital

Monitoring with an observability tool

Observability tools are critical for modern cloud-native applications, and there are several options available for monitoring Docker container resource usage. These tools provide engineering teams with more confidence in their production environment and make troubleshooting performance issues more accessible.

One such observability tool is SigNoz, an open-source solution that helps make containerized applications observable. It uses OpenTelemetry to collect metrics from containers for monitoring. OpenTelemetry is becoming the world standard for cloud-native application instrumentation and is backed by the CNCF (Cloud Native Computing Foundation).

Another popular option is Prometheus, an open-source monitoring system created by SoundCloud in 2012. Prometheus has a comprehensive, multidimensional data model and a powerful query language called PromQL. It also has a push gateway for pushing metrics to the server instead of polling them. Prometheus is known for its simple service discovery, ease of use, powerful alerting capabilities, and strong integration with Kubernetes.

Google's Container Advisor (cAdvisor) is another open-source tool specifically designed for monitoring Docker containers. It is a runtime daemon that collects, aggregates, and exports resource usage and performance data for target containers. cAdvisor provides native support for Docker containers and can be used with additional container environments like Kubernetes. It collects data on CPU, memory, file system utilization, network interfaces, and more.

Grafana is another open-source option that comes with a built-in monitoring, logging, and alerting suite for Docker hosts and containers. It allows users to create custom dashboards with data from multiple sources and has its alert system. Grafana is known for its ability to visualise metrics stored in Prometheus effectively.

Other observability tools mentioned include Lumigo, Elasticsearch & Kibana, Jaeger, AppOptics, Sematext, Datadog, and SolarWinds.

shundigital

Using the CLI to check resource usage

Docker has a built-in stats command that makes it simple to see the amount of resources your containers are using. To use the command, open a terminal and run the docker stats command:

Bash

$ docker stats

This command will display a live feed of your containers' critical metrics, including CPU consumption, memory usage, and a measure of each container's network and storage use. The output will also include the number of processes started by each container.

By default, the `docker stats` command only shows running containers. To include stopped containers in the output, you can use the --all flag:

Bash

$ docker stats --all

If you want to see the stats for a specific container, you can provide the container ID:

Bash

$ docker stats

You can also view the stats for multiple containers by passing a list of space-separated container IDs or names:

Bash

$ docker stats

The `docker stats` command also supports custom formatting, allowing you to select only the columns you need. The --format flag accepts a Go placeholder string that lets you create custom data visualizations. For example, to show container names with CPU and memory usage metrics, you can use:

Bash

$ docker stats --format "table {{.Name}}t{{.CPUPerc}}t{{.MemUsage}}"

The `table` formatting type prepends column headers to the output. If you want the raw data without tabulation, simply omit the `table` keyword.

In addition to the `docker stats` command, Docker also provides other methods for monitoring resource usage, such as using pseudo-files in `sysfs` and the REST API exposed by the Docker daemon.

Frequently asked questions

You can use the docker stats command to monitor Docker container resource usage from the command line. This command provides a live data stream of resource usage statistics for running containers, including CPU, memory, network, and disk usage. To monitor a specific container, include its ID or name after the command.

You can monitor Docker container resource usage with Docker Desktop by installing the Resource Usage extension. This extension provides a user interface that displays real-time resource usage statistics for all deployed containers, including CPU, memory, disk, and network usage. It also allows you to stop and start containers directly from the interface.

If you need more detailed information about a container's resource usage, you can use the /containers/(id)/stats API endpoint provided by Docker. This will give you access to additional metrics and data that can help you better understand how your containers are utilising system resources.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment