當我在交互模式下使用docker run時,我能夠運行我想測試一些python東西的命令。在Python子進程中在docker中運行交互命令
[email protected]:~# docker run -i -t dockerfile/python /bin/bash
[ [email protected]:/data ]$ python -c "print 'hi there'"
hi there
[ [email protected]:/data ]$ exit
exit
[email protected]:~#
我想用子模塊,所以我寫了蟒蛇自動化此此:
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 \n%s\nPYSTUFF' % 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)
據說這是爲了創建容器,運行容器和出口蟒命令和刪除容器。它執行所有這些操作,除了命令在主機上運行而不是在Docker容器上運行。我猜Docker正在換殼或類似的東西。如何從新的shell運行python子進程?