简体   繁体   English

如何将 docker 卷挂载作为 python 子进程运行?

[英]How to run a docker volume mount as a python subprocess?

I am trying to run a docker container, specifically openalpr found here:我正在尝试运行 docker 容器,特别是在此处找到的openalpr

https://hub.docker.com/r/openalpr/openalpr/ https://hub.docker.com/r/openalpr/openalpr/

docker run -it --rm -v $(pwd):/data:ro openalpr -c us plateTest1.jpg

I am running into an error in python when trying to run this command as a subprocess:尝试将此命令作为子进程运行时,我在 python 中遇到错误:

import subprocess导入子流程

app.py应用程序.py

result = subprocess.run(['docker', 'run', '-it', '--rm', '-v pwd:/data:ro', 'openalpr', '-c us', 'plateTest1.jpg'], capture_output=True)
print(result.stdout)
print(result.stderr)

Here is the error I am getting:这是我得到的错误:

(base) mac@macs-MBP lpr % python openalpr_test.py
b''
b'docker: Error response from daemon: invalid volume specification: \' pwd:data:ro\': invalid mount config for type "volume": invalid mount path: \'data\' mount path must be absolute.\nSee \'docker run --help\'.\n'

I am assuming this has to do with escaping slashes?我假设这与 escaping 斜杠有关?

When you call subprocess.run([...]) , you are responsible for breaking the command into "words".当您调用subprocess.run([...])时,您有责任将命令分解为“单词”。 There is no further processing here;这里没有进一步处理; everything gets passed on exactly as you have it.一切都会按照您的方式传递。

In particular, when you特别是,当您

subprocess.run([..., '-v pwd:/data:ro', ...])

The argument passed is exactly -v pwd... including the space in the single argument.传递的参数正是-v pwd...包括单个参数中的空格。 Docker sees a -v argument and parses the rest of this "word" as a volume specification, breaking it on colons, so mounting a named volume (with a leading space) pwd on container path /data in read-only mode. Docker 看到-v参数,并将此“单词”的 rest 解析为卷规范,将其分解为冒号,因此以只读模式在容器路径/data上安装命名卷(带有前导空格) pwd Since (space) pwd isn't a valid volume name, you get this error.由于 (space) pwd不是有效的卷名,您会收到此错误。

You can break this into two separate words to disambiguate this, or remove the intermediate space.您可以将其分成两个单独的词以消除歧义,或删除中间空格。

subprocess.run([..., '-v', 'pwd:/data:ro', ...])
subprocess.run([..., '-vpwd:/data:ro', ...])

(In general it's better to avoid launching things as subprocesses if there is a native library or SDK that has the same functionality. You might try to run this process using the Docker Python SDK instead. Remember that it's very easy to use a docker run command to root the entire host: be very careful about what you're launching and how command lines are processed.) (In general it's better to avoid launching things as subprocesses if there is a native library or SDK that has the same functionality. You might try to run this process using the Docker Python SDK instead. Remember that it's very easy to use a docker run command根整个主机:要非常小心你正在启动什么以及如何处理命令行。)

Not exactly the way I laid it out but this seems to work:不完全是我布置的方式,但这似乎有效:

from subprocess import Popen
import subprocess

command='''
docker run -it --rm -v $(pwd):/data:ro openalpr -c us plateTest1.jpg
'''
process=Popen(command,shell=True,stdout=subprocess.PIPE)
result=process.communicate()
print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM