Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stream the logs in docker python API?

Tags:

python

docker

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()
try:
    here = os.path.dirname(__file__)
    no_cache = False
    dockerfile = os.path.join(here, 'app', 'nextdir')
    image = client.images.build(path=dockerfile, 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:

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?

like image 599
cateof Avatar asked Apr 21 '17 10:04

cateof


People also ask

How do I capture docker logs?

First of all, to list all running containers, use the docker ps command. Then, with the docker logs command you can list the logs for a particular container. Most of the time you'll end up tailing these logs in real time, or checking the last few logs lines.

Can I access the logs of the docker container?

The docker logs command shows information logged by a running container. The docker service logs command shows information logged by all containers participating in a service. The information that is logged and the format of the log depends almost entirely on the container's endpoint command.

How do I monitor logs from a docker container?

Viewing Container Logs You can use docker ps -a to get the IDs and names of your containers. The logs command prints the container's entire log output to your terminal. The output will not be continuous. If you'd like to keep streaming new logs, add the --follow flag to the command.

How to check Docker container logs?

Docker has a dedicated command which lists container logs. The docker logs command. The flow will usually involve you checking your running containers with docker ps, then check the logs by using a container’s ID. This command will list all logs for the specified container. You can add a timestamp flag and list logs for particular dates.

What does the stream parameter do in Docker logs?

Similar to the docker logs command. The stream parameter makes the logs function return a blocking generator you can iterate over to retrieve log output as it happens. stdout ( bool) – Get STDOUT.

How to send Docker container logs to Sematext?

If you were to use Sematext Logagent there are a few simple steps to follow in order to start sending logs to Sematext. After creating a Logs App, run these commands in a terminal. This will start sending all container logs to Sematext. How to Work With Docker Container Logs Using the docker logs Command?

What are the best Docker logging driver alternatives?

But there are a few Docker logging driver alternatives that can help make your job easier, one of them being Sematext Logagent. Logagent is an all-in-one general-purpose solution for container log processing that allows you to monitor container logs, as well as your whole infrastructure and applications if paired with the Sematext Agent container.


2 Answers

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 = docker_client.build(path=dockerfile, tag='app:v.2.4', rm=True)
        while True:
            try:
                output = generator.__next__
                output = output.strip('\r\n')
                json_output = json.loads(output)
                if 'stream' in json_output:
                    click.echo(json_output['stream'].strip('\n'))
            except StopIteration:
                click.echo("Docker image build complete.")
                break
            except ValueError:
                click.echo("Error parsing output from docker image build: %s" % output)
like image 142
Shabaz Patel Avatar answered Oct 21 '22 04:10

Shabaz Patel


According to the docs, the image build now returns a tuple with both the image and the build logs

The first item is the Image object for the image that was build. The second item is a generator of the build logs as JSON-decoded objects.

And modifying @havock solution accordingly:

import docker

client = docker.from_env()
image, build_logs = client.images.build(**your_build_kwargs)

for chunk in build_logs:
    if 'stream' in chunk:
        for line in chunk['stream'].splitlines():
            log.debug(line)
like image 31
EffePi Avatar answered Oct 21 '22 03:10

EffePi