Docker kill an infinite process in a container after X amount of time
I am using the code found in this docker issue to basically start a container run a process within 20 seconds and if the process completes / does not complete / fails to execute / times out the container is killed regardless.
The code I am using currently is this:
#!/bin/bash set -e to=$1 shift cont=$(docker run -d "$@") code=$(timeout "$to" docker wait "$cont" || true) docker kill $cont &> /dev/null echo -n 'status: ' if [ -z "$code" ]; then echo timeout else echo exited: $code fi echo output: # pipe to sed simply for pretty nice indentation docker logs $cont | sed 's/^/\t/' docker rm $cont &> /dev/null
while True: print "inifinte loop"
The whole system jams up and the app crashes, after reading around a bit I think it has something to do with the STDOUT Buffer but I have absolutely no idea what that means?
2 Solutions collect form web for “Docker kill an infinite process in a container after X amount of time”
The problem you have is with a process that is writing massive amounts of data to stdout.
These messages get logged into a file which grows infinitely.
Have a look at (depending on your system’s location for log files):
sudo find /var/lib/docker/containers/ -name '*.log' -ls
You can remove old log files if they are of no interest.
One possibility is to start your
docker run -d daemon
under a ulimit restriction on the max size a file can be.
Add to the start of your script, for example:
ulimit -f 20000 -c 0
This limits file sizes to 20000*1024 bytes, and disables core file dumps, which you expect
to get from infinite loops where writes are forced to fail.
Please add & at the end of
cont=$(docker run -d “$@”)&
It will run the process in background.
I don’t know dockers but if it still fail to stop you may also add just after this line the following :
sleep 20 && kill $mypid