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 import path to php interpreter from docker container to $PATH Linux?
  • Access JMX inside docker container with mapped ports
  • Docker root access to host system
  • ASP.NET Core 1.0 RC1 Docker with dnu publish
  • unable to create docker container on windows VM, GCP
  • Update packages policy on RHEL6
  • #!/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
    

    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?

  • docker daemon restarting containers when not required to
  • Get docker run command for container
  • Docker newbie, is this image created successfully ?
  • How to reduce memory consumption of docker containers?
  • Connect to Docker container running mysql on Windows 10
  • How to use ipaddreses instead of container names in docker compse networking
  • 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 :

    mypid=$!
    sleep 20 && kill $mypid

    Regards

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