docker-py: how can I check if the build was successful?
<p>I'm trying to build myself a neat pipeline in Python 3. My problem, everything seems to be working nice, Jenkins always gives me a green bubble, but sometimes docker build fails to executed.</p>

<p>So <code>client.build</code> doesn't raise an error if the build breaks for some reason:</p>

<pre><code>try:
    self.client.build(path=self.arguments.projectPath, pull=True, rm=False, tag=self.arguments.dockerImageTag)
except:
     print("Error: Docker did not build)
     raise
</code></pre>

<p>does not raise an error if the build failed. </p>

<p>Can someone please help me finding the right method, how I can be sure my build was done, and if not getting a valid message? I am totally lost.</p>

<p>Best regards
Mirco</p>

<p><code>client.build</code> returns the messages outputted by <code>docker</code> when issuing the <code>docker build</code> command. Your initial mistake is in not capturing the response:</p>

<pre><code>response = cl.build(fileobj=f, rm = True, tag='myv/v')
</code></pre>

<p>and if successful the contents look as they do if executed through the command line:</p>

<pre><code>list(response)
b'{"stream":" ---\\u003e bb44cb7f2d89\\n"}\r\n',
# snipped for brevity
b'{"stream":"Successfully built 6bc18019ddb6\\n"}\r\n']
</code></pre>

<p>On error cases, for example, using a silly dockerfile:</p>

<pre><code># a dockerfile with a type in 'MAINTAINER'
f = BytesIO('''
# Shared Volume
FROM busybox:buildroot-2014.02
MAINTAIER first last, first.last@yourdomain.com
VOLUME /data
CMD ["/bin/sh"]
'''.encode('utf-8'))
</code></pre>

<p>The output contained in <code>response</code> looks like:</p>

<pre><code>[b'{"stream":"Step 1 : FROM busybox:buildroot-2014.02\\n"}\r\n',
 b'{"stream":" ---\\u003e 9875fb006e07\\n"}\r\n',
 b'{"stream":"Step 2 : MAINTAIER \\n"}\r\n',
 b'{"errorDetail":{"message":"Unknown instruction: MAINTAIER"},"error":"Unknown instruction: MAINTAIER"}\r\n']
</code></pre>

<p>As you see, a <code>key</code> of <code>errorDetail</code> is contained containing the error message. </p>

<p>So, you can check for that, either with byte string search:</p>

<pre><code>class DockerBuildException(BaseException): pass 


try:
    responce = self.client.build(path=self.arguments.projectPath, pull=True, rm=False, tag=self.arguments.dockerImageTag)
    for i in respone:
        if b'errorDetail' in i:
             raise DockerBuildException("Build Failed")
except DockerBuildException as e:
     print("Error: " + e.args[0])
     raise
</code></pre>

<p>Or, even better, by transforming each entry to a <code>dict</code> with <code>ast.literal_eval</code> and also using the message supplied to inform the user:</p>

<pre><code>from ast import literal_eval

try:
    responce = self.client.build(path=self.arguments.projectPath, pull=True, rm=False, tag=self.arguments.dockerImageTag)
    for i in respone:
        if b'errorDetail' in i:
            d = literal_eval(i.decode('ascii')
            raise DockerBuildException(d['errorDetail']['message'])
except DockerBuildException as e:
     print("Error: " + e.args[0])
     raise
</code></pre>

python python-3.x docker dockerpy