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:

  • How to “add” the proxy section to configure a registry as a pull-through cache?
  • getting error /bin/sh: 1: source: not found
  • How to change the hostname of a container before cretion?
  • Dockerfile - removing a file in one RUN command, it is still present in the next RUN command
  • Can not delete a docker image because repository is missing
  • How I can run docker containers on the remote host?
  • 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?

  • GELF logging driver with Windows Containers
  • Installing pure-ftpd in docker (Debian wheezy), error 421
  • Docker intercontainer communication
  • Connect Docker Container port to server's apache port (odoo Container)
  • Docker Plugin for Jenkins error: Scripts not permitted to use method
  • Docker: Error pulling image (latest) from imageStack, operation not supported
  • 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.