Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting list of running containers from a cluster

I am running docker containers on an AWS via ECS. I have setup a cluster with some basic configuration.

Now I am looking forward to get no. of all running containers on this cluster (spread across 2-3 EC2 instances)? Does AWS / Docker provide any API for this?

I know on a container instance, doing docker ps -a will return me list of all running / stopped containers, while docker ps will return running containers on one system. But I want an API (which can be called from outside) or command (which I can run on any of the instance part of the cluster) and it shows me total number of running containers across cluster?

like image 557
Meraj Rasool Avatar asked Feb 01 '26 21:02

Meraj Rasool


1 Answers

In ECS you're looking at tasks. You can list all running tasks with the ECS API. With the aws cli, for example, you can do:

aws ecs list-tasks --cluster <cluster_name>

If you use one of the SDKs, for example boto3, you can obtain the count easily:

import boto3

client = boto3.client('ecs')

tasks = client.list_tasks(cluster='YouCluster')

print(len(tasks['taskArns'])
like image 119
Jose Haro Peralta Avatar answered Feb 03 '26 10:02

Jose Haro Peralta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!