7

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?`
6
  • Are you saying you want the error message when it fails? Also, to format code blocks, preface the line(s) with 4 spaces instead of using backticks. Sep 7, 2017 at 13:56
  • Well yes, I want to catch the error so I can re-prompt the user to input a valid version, if that makes sense :)
    – cbll
    Sep 7, 2017 at 13:58
  • 1
    That depends on what "catch the error" is, if you just want to have an exception raised if the program doesn't work, use check_call instead of call, if you want to get the string written to stderr then you should do it a different way Sep 7, 2017 at 13:59
  • I haven't had a look at check_call, will read about it. By catch the error, I mean just printing an error message to the user, and re-prompt for a valid software number. The error message is a hand-crafted string.
    – cbll
    Sep 7, 2017 at 14:01
  • 1
    So the question still remains, do you want to get the string written to stderr by the program you're calling or not? Sep 7, 2017 at 14:02

3 Answers 3

12

If you are fine with the program writing its output to stderr and you not directly interacting with it, the easiest way to do what you are asking is to use check_call instead of call. check_call will raise an exception if the command it's running exits with anything other than 0 as its status.

try:
    check_call(["docker", "run", "--name", "{}".format(containerName), "--detach", "--publish", "8080:8080", "user/software:{}".format(swVersion)])
except CalledProcessError:
    print("That command didn't work, try again")
8

You can use the Popen function of subprocess to grab the stderr and print in python console, as Documentation says for subprocess.call

Note Do not use stdout=PIPE or stderr=PIPE with this function as that can deadlock based on the child process output volume. Use Popen with the communicate() method when you need pipes.

proc = subprocess.Popen(["docker", "run", "--name", "{}".format(containerName), "--detach", "--publish", "8080:8080", "user/software:{}".format(swVersion)],stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess_flags)
proc.wait()
(stdout, stderr) = proc.communicate()

if proc.returncode != 0:
    print(stderr)
else:
    print("success")
1

The answer above with checking the return code works but the more pythonic way would be to catch an exception like:

try:
    proc = subprocess.Popen(["docker", "run", "--name", "{}".format(containerName), "--detach", "--publish", "8080:8080", "user/software:{}".format(swVersion)],stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess_flags)
    proc.wait()
    (stdout, stderr) = proc.communicate()

except calledProcessError as err:
    print("Error ocurred: " + err.stderr)

calledProcessError is the error caught by the Popen class.

If you want to catch the frequent errors on OS or system level (e.g. file/directory doesn't exist), add the following exceptions:

except OSError as e:
    print ("OSError > ",e.errno)
    print ("OSError > ",e.strerror)
    print ("OSError > ",e.filename)

except:
    print ("Error > ",sys.exc_info()[0])

If you want to return a return code, you should do that explicitly instead of print()ing it. Write after the last except statement and on the same indentation as the try statement:

return True

If you want to return a message, you can use the Response object:

return Response ("Success Message")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.