Let’s say you have a docker container that you run periodically, and it runs fine most of the time, but in some rare cases, it hangs. This can be very annoying, and it might take some time to find the root cause of the problem.
In the meantime, it would be great if the container would be terminated automatically after exceeding a user-defined maximum runtime.
To solve this problem, you can use the following Python script (don’t forget to change image_name and max_minutes to your environment):
#!/usr/bin/env python3
import docker
import dateutil.parser
from datetime import datetime
image_name = "myimage:latest"
max_minutes = 45
client = docker.from_env()
for container in client.containers.list():
if image_name not in container.image.tags:
continue
started_at = container.attrs["State"]["StartedAt"]
created = dateutil.parser.isoparse(started_at).replace(tzinfo=None)
now = datetime.utcnow()
diff = now - created
minutes_since_started = (int) (diff.total_seconds() / 60)
if minutes_since_started >= max_minutes:
container.stop()
Assuming that the user starting the container is called worker, you could save this script to /home/worker/scripts/check-containers.py and make it executable:
chmod u+x check-containers.py
The script uses the docker Python module. If you don’t have it installed already you can get it with:
pip install docker
Now you only need to add the following line to /etc/crontab and the script will be executed every minute:
*/1 * * * * worker /home/worker/scripts/check-containers.py
Since the check happens only once every minute, the container will be killed after 45 to 46 minutes. For most use cases, it should even be fine to have the check run less frequently, say only every 5 minutes, for example.