How to Tell How Much Memory a Docker Container Uses?

Hey there, fellow Docker enthusiasts! Today, I wanted to share with you my journey in figuring out how to tell how much memory a Docker container uses. When I first started playing with Docker, I had no clue how to monitor the resources my containers were consuming. I hope this simple and friendly guide will help you understand this important aspect of working with Docker containers.

To learn more about Docker, please check Docker Tutorials page.

Let’s dive right in!

Step 1: Install Docker

First things first, we need to have Docker installed on our system. If you haven’t done that yet, don’t worry! Just head over to the official Docker website and follow the instructions for your operating system. Or read this tutorial to learn how to install Docker on MacOS.

After installing Docker, make sure it’s running by opening your terminal (or command prompt) and typing:

docker --version

If you see the Docker version displayed, you’re good to go!

Step 2: Run a Docker container

Before we can check the memory usage, we need to have a container running. For this example, let’s use the official nginx image from Docker Hub. To run the container, type the following command in your terminal:

docker run -d --name my-nginx nginx

This command will download the nginx image (if you don’t have it already) and run it in the background as a container named “my-nginx”.

Step 3: Check the memory usage

Now that we have a running container let’s figure out how much memory it’s using. The easiest way to do this is by using the docker stats command. In your terminal, type:

docker stats --no-stream my-nginx

This command will show you real-time stats for the “my-nginx” container, including memory usage. The --no-stream flag is used to display the stats only once instead of continuously updating them.

Here’s a sample output you might see:

CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O          BLOCK I/O       PIDS
a1b2c3d4e5f6   my-nginx  0.00%     3.082MiB / 1.943GiB   0.16%     1.16kB / 648B    3.07MB / 8.19kB  2

The important column to look at is “MEM USAGE / LIMIT”. In this example, the container is using 3.082 MiB of memory out of a total available limit of 1.943 GiB. The “MEM %” column shows the percentage of the total available memory used by the container.

And that’s it! You now know how to tell how much memory a Docker container is using. Remember, monitoring the resources consumed by your containers is crucial for optimizing performance and avoiding issues when running multiple containers.

I hope you found this guide helpful, and I wish you the best of luck in your Docker adventures! Don’t hesitate to share your thoughts and questions in the comments below.

Happy containerizing!