How to stream the logs in docker python API?

I am building an image from a Dockerfile using the docker python API.

import os
import sys
import os.path
import docker

client = docker.from_env()
    here = os.path.dirname(__file__)
    no_cache = False
    dockerfile = os.path.join(here, 'app', 'nextdir')
    image =, tag='app:v.2.4', nocache=no_cache, stream=True)

The operation finishes successfully, however I was not able to stream the logs. The API says:

  • AspnetPublishHandler with name “Custom” was not found when publishing ASP.NET 5 project to Docker
  • GCloud: Copying Files from Local Machine into a Docker Container
  • Jenkins inheritance plugin jobs are run in infinite loop
  • Spawn a new process in docker container which image is built from scratch
  • Bluemix container failing to network
  • Installing PDO driver in Docker
  • Return a blocking generator you can iterate over to retrieve build
    output as it happens

    when stream=True.

    How can I get these logs in python?

  • Why is the Node.js AWS-SDK returning the wrong SQS queue URL when creating a local queue
  • Docker & Amazon Beanstalk - Deploy an Angular application
  • Connect to Docker container running mysql on Windows 10
  • In docker run --ulimit cpu=10 does not work in some cases
  • docker dnsmasq wildcard sub domains not supported?
  • Accessing host different ports in production
  • 2 Solutions collect form web for “How to stream the logs in docker python API?”

    Streaming the docker build logs can be done using the low-level APIs given in docker-py as follows,

            here = os.path.dirname(__file__)
            dockerfile = os.path.join(here, 'app', 'nextdir')
            docker_client = docker.APIClient(base_url='unix://var/run/docker.sock')
            generator =, tag='app:v.2.4', rm=True)
            while True:
                    output =
                    output = output.strip('\r\n')
                    json_output = json.loads(output)
                    if 'stream' in json_output:
                except StopIteration:
                    click.echo("Docker image build complete.")
                except ValueError:
                    click.echo("Error parsing output from docker image build: %s" % output)

    The docs state…

    If you want to get the raw output of the build, use the build() method in the low-level API.

    Have you tried that?

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.