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:

  • Using sudo inside non-priviledged docker container not working
  • Run a Docker image as a command line utility in Windows?
  • Kubernetes run on AWS
  • Kubernetes PetSet - FailedCreate of persistent volume
  • Filebeats doesn't foward Docker compose logs, why?
  • Meteor run server getting error: “Could not locate the bindings file. […]bcrypt[…]”
  • #!/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 automate Grunt on a Docker image
  • Kubernetes service dns resolution returning wrong IP
  • Simple docker command does not work: x509: cannot validate certificate
  • Run docker from console
  • tutum node always at full memory consumption
  • Docker container running but can't view in browser
  • 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.