Avoid hanging at end of docker run processes by allocating tty. Improved method for detecting the end of runs for exiting container