title: Docker timeout for container?
body: <p>For my dissertation at University, I'm working on a coding leaderboard system where users can compile / run untrusted code through temporary docker containers. The system seems to be working well so far, but one problem I'm facing is that when code for an infinite loop is submitted, E.g:</p>
  
  <pre><code>while True:
     print "infinite loop"
  </code></pre>
  
  <p>the system goes haywire. The problem is that when I'm creating a new docker container, the Python interpreter prevents docker from killing the child container as data is still being printed to STDOUT (forever). This leads to the huge vulnerability of docker eating up all available system resources until the machine using the system completely freezes (shown below):</p>
  
  <p><img src="https://i.stack.imgur.com/w43sd.gif" alt="enter image description here"></p>
  
  <p>So my question is, is there a better way of setting a timeout on a docker container than my current method that will actually <strong>kill the docker container and make my system secure</strong> (code originally taken from <a href="https://github.com/docker/docker/issues/1905#issuecomment-24795551" rel="noreferrer">here</a>)? </p>
  
  <pre><code>#!/bin/bash
  set -e
  
  to=$1
  shift
  
  cont=$(docker run --rm "$@")
  code=$(timeout "$to" docker wait "$cont" || true)
  docker kill $cont &amp;&gt; /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 &amp;&gt; /dev/null
  </code></pre>
  
  <p>Edit: The default timeout in my application (passed to the <code>$to</code> variable) is "10s" / 10 seconds.</p>
  
  <hr>
  
  <p>I've tried looking into adding a timer and <code>sys.exit()</code> to the python source directly, but this isn't really a viable option as it seems rather insecure because the user could submit code to prevent it from executing, meaning the problem would still persist. Oh the joys of being stuck on a dissertation... :(</p>