Search code examples
pythondockerdockerpy

How to get logs when build fails in docker-py?


If I build an image using the high-level docker-py sdk, I get a BuildError on failure., eg

try:
    client.images.build(...)
except:
    print("Hey something wrong with image build!")

I know I can use the low level client API to directly hook in and stream logs, see How can I detect when docker-py client.build() fails.

Is there a way to get some useful debug output from the image build script without going down to the lower level api?


Solution

  • As of Docker 3.x, the BuildError contains a new build_log variable which is a generator of output:

    try:
        return client.images.build(...)
    except BuildError as e:
        print("Hey something went wrong with image build!")
        for line in e.build_log:
            if 'stream' in line:
                logger.error(line['stream'].strip())
        raise