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?
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'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With