Loading...

How to use docker stats

Jonas Scholz
3 min read 08. Jan. 2024

Let's talk about the docker stats command. This is a handy tool if you want to keep an eye on how your Docker containers are using resources like CPU, memory, and network. It shows you a live stream of stats, which means the information updates in real-time.

To use docker stats, you can simply type docker stats into your terminal. This will show you the stats for all your running containers. If you want to see stats for specific containers, you can list their names or IDs after the command, like this: docker stats container1 container2.

If you try to check the stats of a stopped container, it won't show any data because, well, it's stopped! But you can still include stopped containers in your command if you want.

For a deeper dive into a container's resource usage, you can use the /containers/(id)/stats API endpoint. This gives you more detailed information than the basic docker stats command.

Now, let's go over some options you can use with docker stats:

  1. -a, --all: By default, docker stats only shows running containers. If you want to see all containers, including stopped ones, use this option. For example: docker stats --all.
  2. --format: This lets you customize how the stats are displayed. You can choose from formats like 'table', 'json', or even create your own template. For instance, to see the stats in JSON format, you would use docker stats --format json.
  3. --no-stream: Normally, docker stats keeps updating the stats in real-time. If you just want to see the stats once and then stop, use this option. Like this: docker stats --no-stream.
  4. --no-trunc: If you're seeing truncated output and want to see the full details, use this option. It makes sure nothing is cut off. For example: docker stats --no-trunc.

When you run docker stats without specifying a format, it shows you several columns of information:

  • CONTAINER ID and Name: This tells you which container the stats are for.
  • CPU % and MEM %: These show how much of the host's CPU and memory the container is using, in percentages.
  • MEM USAGE / LIMIT: This tells you how much memory the container is using and how much it's allowed to use.
  • NET I/O: This shows how much data the container has sent and received over the network.
  • BLOCK I/O: This tells you how much data the container has read from and written to block devices on the host.
  • PIDs: This is the number of processes or threads the container has created.

If you're using docker stats on a Linux system, there's something to keep in mind about memory usage. The command subtracts cache usage from the total memory usage to give you a clearer picture. However, if you're using the API, it gives you the total memory usage and the cache usage separately, so you can do your own calculations.

Also, on Linux, the PIDS column shows the number of processes and kernel threads. If you see a high number here but a low number of processes when you use commands like ps or top, it might mean something in the container is creating a lot of threads.

For Windows users, the stats command works a bit differently, but you can still use it to monitor your containers in a similar way.

To format the output of docker stats, you can use the --format option with a Go template. This lets you choose exactly what information you want to see and how you want it displayed. For example, if you just want to see the container name and its CPU usage, you could use a template like {{.Name}}:{{.CPUPerc}}.

Remember, the docker stats command is a powerful tool for keeping track of your containers' performance. Whether you're just starting out with Docker or you're a seasoned pro, it's a command you'll find yourself using a lot.

Welcome to the container cloud

Sliplane makes it simple to deploy containers in the cloud and scale up as you grow. Try it now and get started in minutes!