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?
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.
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.
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.
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.
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.
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?
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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With