run_this = "print('hi')"
random_name = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(20))
command = 'docker run -i -t --name="%s" dockerfile/python /bin/bash' % random_name
subprocess.call([command],shell=True,stderr=subprocess.STDOUT)
command = 'cat <<'PYSTUFF' | timeout 0.5 python | head -n 500000
%s
PYSTUFF' % run_this
output = subprocess.check_output([command],shell=True,stderr=subprocess.STDOUT)
command = 'exit'
subprocess.call([command],shell=True,stderr=subprocess.STDOUT)
command = 'docker ps -a | grep "%s" | awk "{print $1}" | xargs --no-run-if-empty docker rm -f' % random_name
subprocess.call([command],shell=True,stderr=subprocess.STDOUT)
from subprocess import Popen, PIPE
p = Popen("docker run -i -t --name="%s" dockerfile/python /bin/bash", stdin=PIPE)
p.communicate("timeout 0.5 python | head -n 500000
" % run_this)
import pty
import sys
import select
import os
import subprocess
pty, tty = pty.openpty()
p = subprocess.Popen(['docker', 'run', '-it', '--rm', 'ubuntu', 'bash'], stdin=tty, stdout=tty, stderr=tty)
while p.poll() is None:
# Watch two files, STDIN of your Python process and the pseudo terminal
r, _, _ = select.select([sys.stdin, pty], [], [])
if sys.stdin in r:
input_from_your_terminal = os.read(sys.stdin.fileno(), 10240)
os.write(pty, input_from_your_terminal)
elif pty in r:
output_from_docker = os.read(pty, 10240)
os.write(sys.stdout.fileno(), output_from_docker)