I'm trying to download a specific Docker image, where the user will input a version. However, if the version doesn't exist, Docker will throw an error.
I'm using subprocess.call
to pipe to the Terminal from Python 3.
Sample code:
from subprocess import call
containerName = input("Enter Docker container name: ")
swVersion = input("Enter software version: ")
call(["docker", "run", "--name", "{}".format(containerName),
"--detach", "--publish", "8080:8080", "user/software:{}".format(swVersion)])
If the version isn't found, docker will output in the Terminal:
docker: Error response from daemon: manifest for user/software:8712378 not found.
How do I catch this error within the Python script?
Something along the lines of:
try:
call(["docker", "run", "--name", "{}".format(containerName), "--detach", "--publish", "8080:8080", "user/software:{}".format(swVersion)])
except:
# How do I catch the piped response code here?`
check_call
instead ofcall
, if you want to get the string written tostderr
then you should do it a different waystderr
by the program you're calling or not?