[英]Properly automate a docker script in Python
Based on this tutorial to build a TF image classifier, I have a bash shell in which I run a Docker image with the following command: 基于构建TF图像分类器的本教程 ,我有一个bash shell,可在其中使用以下命令运行Docker图像:
docker run --name fooldocker -it -v $HOME/tf_files:/tf_files/ gcr.io/tensorflow/tensorflow:latest-devel
And then in this docker image I run my Python script: 然后在此docker映像中,运行我的Python脚本:
python /tf_files/label_image.py /tf_files/myimages
exit
It works. 有用。
But now, I need to automate these commands in a Python script . 但是现在, 我需要在Python脚本中自动执行这些命令 。 I tried :
我试过了 :
p = Popen(['docker', 'run', '--rm', '--name', 'fooldocker','-it', '-v', '$HOME/tf_files:/tf_files/', 'gcr.io/tensorflow/tensorflow:latest-devel'], stdout=PIPE)
p = Popen(['docker', 'exec', 'fooldocker', 'python', '/tf_files/label_NES.py', '/tf_files/NES/WIP'])
p = Popen(['docker', 'kill', 'fooldocker'], shell=True, stdout=PIPE, stderr=PIPE)
p = Popen(['docker', 'rm', 'fooldocker'], shell=True, stdout=PIPE, stderr=PIPE)
Leading to this error after Popen #2 is run : 在运行Popen#2之后导致此错误:
docker: Error response from daemon: create $HOME/tf_files: "$HOME/tf_files" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
The problem is that $HOME
cannot be evaluated in this single quotes string. 问题在于
$HOME
不能在此单引号字符串中求值。 Either try doublequotes, or evaluate the variable beforehand and put it into the command string. 请尝试使用双引号,或者预先评估变量并将其放入命令字符串中。
Also: If you set shell=True, you don't split your command into a list: 另外:如果设置shell = True,则不会将命令拆分为一个列表:
p = Popen('docker kill fooldocker', shell=True, stdout=PIPE, stderr=PIPE)
it is because Popen didn't interpret $HOME
to your home path. 这是因为Popen不会将
$HOME
解释$HOME
您的主路径。 and it is the string $HOME
and pass to docker command which not allow $
in a volume name. 它是字符串
$HOME
并传递给docker命令,该命令不允许在卷名中使用$
。
maybe you can use subprocess
module for convenience, for example: 也许为了方便起见,可以使用
subprocess
模块,例如:
import subprocess
subprocess.call("echo $HOME", shell=True)
it interpreted $HOME
if shell=True
specified. 如果
shell=True
指定,它将解释$HOME
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.