Reputation: 115
I just started building my docker images with python instead of sh-scripts, but am confused as I am getting no STDOUT with my script.
Is there a way to enable it? The parameter "quiet" seems to enable/disable if an object is returned.
Thank you,
Jakob
Minimal example:
# Dockerfile
FROM ubuntu
# main.py
import docker
client = docker.from_env()
client.images.build(path='.')
Where executin the build command in console yields:
jakob@jakob:~/foo$ docker build .
Sending build context to Docker daemon 20.99kB
Step 1/1 : FROM ubuntu
---> 7e0aa2d69a15
Successfully built 7e0aa2d69a15
executing the python file yields nothing. I would love for both to be similar in their STDOUT.
Upvotes: 1
Views: 1359
Reputation: 142641
I never use this before but I simply put build()
in print()
and I found it returns some values.
(<Image: 'ubuntu:latest'>, <itertools._tee object at 0x7f836bea2b00>)
So I start testing it with print(...)
, print(type(...))
, print(dir(...))
Because I saw word itertools
so I tried to use it with for
-loop.
This code
import docker
client = docker.from_env()
result = client.images.build(path='.', quiet=False)
#print(result)
print(result[0], type(result[0]))
print(dir(result[0])
for item in result[1]:
print(item)
gives me
<Image: 'ubuntu:latest'> <class 'docker.models.images.Image'>
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'attrs', 'client', 'collection', 'history', 'id', 'id_attribute', 'labels', 'reload', 'save', 'short_id', 'tag', 'tags']
{'stream': 'Step 1/1 : FROM ubuntu'}
{'stream': '\n'}
{'stream': ' ---> 7e0aa2d69a15\n'}
{'aux': {'ID': 'sha256:7e0aa2d69a153215c790488ed1fcec162015e973e49962d438e18249d16fa9bd'}}
{'stream': 'Successfully built 7e0aa2d69a15\n'}
It shows dictionares so using key,value
I can format it (and filter it)
import docker
client = docker.from_env()
result = client.images.build(path='.')#, quiet=False)
#print(result)
#print(result[0], type(result[0]))
#print(dir(result[0]))
for item in result[1]:
#print(item)
for key, value in item.items():
#print(key, ':', value)
if key == 'stream':
text = value.strip()
if text:
print(text)
and this gives me
Step 1/1 : FROM ubuntu
---> 7e0aa2d69a15
Successfully built 7e0aa2d69a15
Upvotes: 1