Cron job to kill all hanging docker containers

636 views Asked by At

I am new to docker containers but we have containers being deployed and due to some internal application network bugs the process running in the container hangs and the docker container is not terminated. While we debug this issue I would like a way to find all those containers and setup a cron job to periodically check and kill those relevant containers.

So how would I determine from "docker ps -a" which containers should be dropped and how would I go about it? Any ideas? We are eventually moving to kubernetes which will help with these issues.

1

There are 1 answers

3
ROOT On BEST ANSWER

Docker already have a command to cleanup the docker environment, you can use it manually or maybe setup a job to run the following command:

$ docker system prune

Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

refer to the documentation for more details on advanced usage.