Home > Enterprise >  How to filter Docker containers by running time?
How to filter Docker containers by running time?

Time:04-10

For example, display only those containers that have been running for more than 10 minutes.

docker container ls -q --format '{{.Names}}' --filter status=running

CodePudding user response:

First you need to get container creation date time then compare it with current date

#!/bin/bash    
# Date 1
container_date=$(docker inspect --format='{{.State.StartedAt}}' CONTAINER_NAME)
# Compute the seconds since epoch for date 1
t1=$(date --date="$container_date"  %s)

# Date 2 : Current date
current_date=$(date  %Y-%m-%d\ %H:%M:%S)
# Compute the seconds since epoch for date 2
t2=$(date --date="$current_date"  %s)

# Compute the difference in dates in seconds
let "tDiff=$t2-$t1"
# Compute the approximate hour difference
let "hDiff=$tDiff/3600"

echo "Approx hour diff b/w $container_date & $current_date= $hDiff"

reference link

  • Related