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:

  • How to access to Google AppEngine Managed VM logs?
  • How to debug Golang application in Docker with VSCode?
  • How to perform code analysis in sonarqube in docker in a ASP.NET web application
  • How can I expose a Docker container port only to localhost so that it is also accessible via an ssh tunnel?
  • How Docker is better than Vagrant+LXC+Chef [closed]
  • Write to a file on docker container gives error: No such file or directory
  • #!/bin/bash
    set -e
    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
        echo exited: $code
    echo output:
    # pipe to sed simply for pretty nice indentation
    docker logs $cont | sed 's/^/\t/'
    docker rm $cont &> /dev/null

    Which is almost perfect however if you run an infinite process (for example this python infinite loop):

    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?

  • How to use docker images filter
  • Xdebug with PHPStorm and a Docker container
  • how to implement Sensu Check for checking if Docker Container is running or not
  • Pycharm run “Debugger speedups using cython” on docker hosted app
  • RabbitMQ inside docker won't end gracefully and cannot reconnect
  • Can I automate a Docker image build for a public repo I'm not a member of?
  • 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


    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.