Reputation: 2127
I am trying to run a docker container, specifically openalpr
found here:
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:
import subprocess
app.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?
Upvotes: 0
Views: 1302
Reputation: 159732
When you call subprocess.run([...])
, you are responsible for breaking the command into "words". 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. 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. Since (space) pwd
isn't a valid volume name, you get this error.
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.)
Upvotes: 1
Reputation: 2127
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)
Upvotes: 0