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:

  • Set hostname of service container to host's hostname
  • Dockerizing Delayed Job
  • Docker build: is the content from a layer keep on the next one?
  • Connect many Jenkins servers with different branches of the same github repo
  • Jenkins Docker Set Admin Password from Environment Variable
  • Mount a volume while using a docker container in Azure App Service
  • #!/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?

  • Is Docker for Mac Open Source? [closed]
  • Running Angular2 application in Docker
  • How to create a docker cluster with MQ without Kubernetes
  • strange docker error when stop and starting a container
  • How to modify the `--registry-mirror` of a running docker machine?
  • stuck at command “sbt compile” in docker ubuntu
  • 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.