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:

  • docker-compose: specify which interface will connect to each network (when having multiple networks)
  • Error with running simple Elixir app in Docker container
  • EC2 User Data runs script but does not boot up application
  • Caching a single file on Travis CI
  • Deploy group in marathon
  • How to speed up running docker-container with node.js?
  • 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?

  • Is it possible to use a Dockerfile not named “Dockerfile” with CircleCI?
  • Dockerized nginx is not starting
  • Cannot connect to postgres server in docker
  • docker: why are images per machine?
  • Convert HTML to PDF using wkhtmltopdf, docker in ASP.NET Core
  • How start docker container with specific veth pair 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.