Reputation: 734
I'm trying to execute some long processes with docker-py My problem is that I'm not able to see the output of what I'm doing...
How can I stream the output from the docker exec directly to stdout ? or better to logging ?
I've seen there is a "stream" parameter, but I don't undestand how to use the generator returned...
exec_c=self.docker.exec_create(myContainer,e["cmd"])
output=self.docker.exec_start(exec_c)
self.logger.info(output)
http://docker-py.readthedocs.org/en/latest/api/?highlight=exec#exec_create
Upvotes: 2
Views: 6195
Reputation: 61
Example of how you can get stdout output. Stdout for default is true o complet stdout=true
Here's python code:
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock')
id_container = '14484253f0a8'
cli.start(id_container)
cmds='ls' #comand to execute
exe = cli.exec_create(container=id_container, cmd=cmds)
exe_start= cli.exec_start(exec_id=exe, stream=True)
for val in exe_start:
print (val)
I hope it helps.
Upvotes: 6
Reputation: 634
Here is the code I use in order to get responses from build commands. You should be able to adapt this code to exec_start needs.
Create a StreamLineBuilder generator:
import json
class StreamLineBuildGenerator(object):
def __init__(self, json_data):
self.__dict__ = json.loads(json_data)
And then use this generator in order to parse your stream:
import docker
docker_client = docker.Client(version="1.18", base_url="unix:///var/run/docker.sock")
generator = docker_client.build(nocache=False, rm=True, stream=True, tag="my_image_tag", path="my_path")
for line in generator:
try:
stream_line = StreamLineBuildGenerator(line)
# Do something with your stream line
# ...
except ValueError:
# If we are not able to deserialize the received line as JSON object, just print it out
print(line)
continue
Upvotes: 0