View the Resource Usage of Your Docker Containers

What happens when you have a number of Docker containers running and something goes awry? Do you panic and stop all containers? No. You investigate… right? That’s what system administrators have done for years: they locate the problem and then find a solution. The same thing should hold true with containers.
One place you might start your investigation is resource usage. After all, when you have a number of deployed containers, they’ll each use resources differently and you might have to track down the one that’s causing problems. It’s the same as standard applications on a server. When something goes wrong, you might first check the resource usage of those apps to see which one is the problem.
So, why not do the same with your Docker containers? After all, what container developer or admin doesn’t want to know how their deployments are using resources?
Fortunately, there are a few ways to handle this task and I’m going to show you two of them… one from the command line and one from the Docker Desktop GUI. I’ll even show you how to do a quick WordPress deployment so you’ll have at least two containers to monitor.
First, let’s deploy WordPress.
What You’ll Need
To follow along with this, you’ll need the following things:
- Docker and docker-compose are installed on your platform of choice.
- Docker Desktop installed (if you want to do this the GUI way).
Deploying WordPress with Docker
The first thing we’re going to do is deploy a basic WordPress container. To do that, we’re going to use the docker-compose command. First, create the docker-compose file with the command (or use whatever text editor you prefer):
1 |
nano docker-compose.yml |
In that file, paste the following content:
Make sure to change the username and password as needed. Remember, this is only for testing purposes, so you can opt to leave the above compose file as is.
Save and close the file.
Deploy the containers with the command:
1 |
docker-compose up -d |
Both MySQL and WordPress containers will deploy. You can verify that with the command:
1 |
docker ps |
Check the Resource Usage from the Command Line
With your new containers running, issue the command:
docker stats
You should see output similar to this:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
c6ba085f1adc jack-wordpress-1 0.01% 51.72MiB / 3.58GiB 1.41% 75.5kB / 150kB 4.65MB / 58MB 1
c0308bfc83c1 jack-mysql-1 0.09% 192.1MiB / 3.58GiB 5.24% 44.6kB / 39.1kB 1.16MB / 291MB 28
005cb27fc012 vigorous_morse 0.00% 7.398MiB / 3.58GiB 0.20% 1.67kB / 0B 344kB / 12.3kB 9
The above command will display the Container ID, Name, CPU percentage, memory usage and limit, memory percentage, and NET I/O for each running container. One thing to note is that you don’t receive your prompt back, because the stats command displays real-time information about the running containers. Because of this, you can watch the stats of those containers as they are used.
If you have a large number of containers running, you might want to view the output one container at a time. To do that, you must have the container ID. To find the ID of a container, issue the command:
1 |
docker ps |
Let’s say you have a container with ID c6ba085f1adc and you want to view its stats. For that, the command would be:
1 |
docker stats c6ba085f1adc |
The output of the above command will only display the real-time stats for that one container.
Check the Resource Usage with Docker Desktop
If Docker Desktop is your tool of choice, you can view the resources of your running containers with the help of a handy extension. To add the extension, open Docker Desktop, click Add Extensions in the sidebar, type Resource Usage in the search field, and then click Install associated with the app (Figure 1).
-
Figure 1: Installing the Resource Usage for Docker Desktop.
After the extension has been installed, you’ll see Resource usage listed in the sidebar. Click that entry to view a real-time listing of each deployed container (Figure 2).
-
Figure 2: Container resource usage as viewed from the Docker Desktop GUI.
One of the nice things about the Docker Desktop Resource Usage extension is that you can also stop and start containers from that listing. You also get an at-a-glance view of overall resource usage and even change the refresh rate for the extension (from every 1 second to 5 minutes).
For those who prefer their statistics displayed in charts, click the Chart View tab to see charts for CPU, Memory, Disk R/W, and Net I/O (Figure 3).
-
Figure 3: A graphical representation of resource usage is available in Docker Desktop.
And that’s all there is to view the resource usage of your Docker containers. Both of these methods will give you plenty of information to start troubleshooting your containers. This may not be the be-all-end-all collection of information, but it’ll allow you to get a peak into the efficiency of your deployments.