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:

  • Add file to docker container to be used during startup
  • How to setup hostnames using docker-compose?
  • Scheduling Docker containers to Cluster: Swarm or CoreOS?
  • Dockerizing an app that uses mxnet package in R
  • Linux Docker container on Windows Server 2016
  • Specify bolt port for a neo4j docker container
  • 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?

  • Flannel and docker don't start
  • sensitive information required for docker build
  • docker pull lookup no such host
  • Case sensitive host volume mount in docker for windows
  • Docker selenium/node-chrome - Protractor can not find Chrome binary
  • docker invalid characters for local volume name
  • 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.