Docker Community Forums

Share and learn in the Docker community.

Tip: Cleaning up old containers


(Michael Brown) #1

Over time, you can accumulate a LOT of containers sitting around.

If you want to clean those up after a while (say 24 hours) you can use this to do it nice and cleanly:

docker inspect -f '{{.Id}},{{.State.Running}},{{.State.FinishedAt}}' $(docker ps -qa) | \
  awk -F, 'BEGIN { TIME=strftime("%FT%T.000000000Z",systime()-60*60*24); } $2=="false" && $3 < TIME {print $1;}' | \
  xargs --no-run-if-empty docker rm

Schedule in cron and it’ll automatically happen, keeping your filesystem clean!