How to display running Docker containers in Stat panels, filtered by compose file?

  • What Grafana version and what operating system are you using?
    Ver 10.2.3 (Docker), DietPi. Metrics provided by Telegraf.

  • What are you trying to achieve?
    Multiple panels that count my currently running Docker containers, but each filtered to those running from a specific Compose file. Similar to how Portainer lists Stacks.

  • How are you trying to achieve it?
    Using the Data Explorer, I’ve found that the all the docker_container_… metrics have a com.docker.compose tag, which I can use to filter them separately as I desire. Unfortunately though, only the “docker” metric has an option to print just the currently running containers, and it lacks the tag. Docker_container ones only provide metrics about the containers, such as uptime or resource usage.

Using docker_container_status with its started_at field seems to give me some useful data for this purpose. A table of containers, with their compose file listed, and the time they were started at. If a container isn’t running, it isn’t included entirely. Perhaps I could query a count of how many times a compose file name appears in the data, and that’d provide the exact value I need?

I have absolutely minimal knowledge of Grafana, having just started out. So I’m undoubtedly using incorrect terminology here. Apologies if this is a simple query to write manually, but I have no idea where to start!

If you want to count the running Docker containers created from a specific Docker Compose file, you can use the docker-compose command along with Docker commands. Here’s a general approach:

  1. List Containers: Use the docker-compose ps command to list containers related to a specific Docker Compose project. This command provides information about the status of the containers, including whether they are running.
    Replace /path/to/your/docker-compose-file.yml with the actual path to your Docker Compose file.
  2. Filter and Count Running Containers: You can use grep and wc to filter and count the running containers.

This command will output the number of running containers.

  • --services: Lists only the service names.
  • grep -E '\b(up)\b': Filters only the lines containing the word “up” (indicating running containers).
  • wc -l: Counts the number of lines, giving you the count of running containers.

This approach assumes that the containers you want to count are defined in the specified Docker Compose file. Adjust the path to your Docker Compose file and modify the commands accordingly based on your specific setup.

Remember that Docker Compose must be installed, and you should have the necessary permissions to execute Docker commands. Additionally, the Docker Compose file should be properly configured to define the services you want to manage.

Welcome @jacksaur

So how is this related to grafana?

I have the data I need and a way it could feasibly be read in the way I want. I just wanted some direction on how to write such a Query to format the data that way in a panel. Or, if there’s an easier method than the one I’m thinking of (Counting how many times each compose file name appears in the recieved data).
I’ve looked at some beginner guides on the SQL language used, but it’s a lot to take in for just a simple panel I want currently.

It sounds like a simple idea, but with zero existing knowledge, it’s much harder to put together than I expected.

where is the data from docker containers being currently saved to?

InfluxDB via Telegraf.

please share some sample data as csv inline


Again, complete beginner to all this, so apologies if this isn’t the data you needed.
As far as I could tell, I needed to make a panel to export from. So I threw something together that used Distinct as the aggregator (Which somewhat works if I set the group by Time interval to match telegraf’s flush interval, but without perfect sync, it bunches up values incorrectly at times)

2024-01-24 19:16:40,aggregate,1705337796792591400
2024-01-24 19:16:40,aggregate,1705338155643984000
2024-01-24 19:16:45,aggregate,1705337796792591400
2024-01-24 19:16:45,aggregate,1705338155643984000
2024-01-24 19:16:40,management,1703160334364439300
2024-01-24 19:16:45,management,1703160334364439300

My ideal result from that would be, of course, a value of 2 for Aggregate and 1 for Management in a panel.

1 Like

no need to apologize.

from the data sample provided which ones qualify to be shown and based on what criteria

The criteria I’d want is to display the amount of values with the same compose.project key, but only from the latest batch of data sent at the time.
So for instance, in the data above I’d want a value of 2 for Aggregate and 1 for Management. Since there are two unique Aggregate results at 19:16:45 and one Management. The results at 19:16:40 and earlier should all be ignored.

It seems the ‘distinct’ aggregator counts the values as I desire, but it also counts through all data recieved within the current interval range. Meaning if my telegraf export doesn’t match up perfectly with the query interval, the results end up either doubled, or empty. Is there a way to have it iterate over only the latest batch of data recieved?

1 Like

I’ve found a workaround of sorts by setting the panel’s Relative Time to a much shorter value in the Query Options section. It works, but I would prefer if there was some way I could limit the data to the latest batch by the query itself.
For anyone trying to accomplish the same: Here’s my query.

Check this out