from io import BytesIO
import docker
dockerfile = '''
FROM ubuntu
RUN apt-get update
'''
f = BytesIO(dockerfile.encode('utf-8'))
client = docker.from_env()
for line in client.images.build(fileobj=f, rm=True, tag='test', nocache=True, stream=True):
print(line)
From the documentation, it seems like I client.images.build should return a generator. I get the following traceback:
Traceback (most recent call last):
File "issue_no_logs.py", line 9, in <module>
for line in client.images.build(fileobj=f, rm=True, tag='test', nocache=True, stream=True):
TypeError: 'Image' object is not iterable
pip freeze | grep docker && python --version && docker version
docker==2.0.1
docker-pycreds==0.2.1
Python 2.7.12
Client:
Version: 1.12.3
API version: 1.24
Go version: go1.6.2
Git commit: 6b644ec
Built: Mon, 19 Dec 2016 09:20:48 +1300
OS/Arch: linux/amd64
Server:
Version: 1.12.3
API version: 1.24
Go version: go1.6.2
Git commit: 6b644ec
Built: Mon, 19 Dec 2016 09:20:48 +1300
OS/Arch: linux/amd64
cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.1 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.1 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
The returned type Image matches the documentation, as far as I can tell.
Where did you see that client.images.build returns a generator? Might be an overlook on our part in another section of the docs.
@shin- I appreciate the quick response. From the documentation, I saw stream as a kwarg.
stream (bool) – Deprecated for API version > 1.8 (always True). Return a blocking generator you can iterate over to retrieve build output as it happens
Although deprecated, it seems like an always True value would always return a generator that I can iterate over. I ultimately want to stream the logs over a socket as the image is building. Is there a better way to do this than my sample code?
Ah, this is a mistake - the stream parameter doesn't do anything (it's
deprecated as noted).
For streaming build logs, you'll want the low-level build method on
APIClient (client.api.build).
http://docker-py.readthedocs.io/en/stable/api.html#module-docker.api.build
My mistake, the low-level build method does exactly what I want. Thanks for the clarification.
Glad I could help! The documentation is definitely misleading.
I recently ran into this issue, however the reason was not because the docs were wrong, but because I had previously installed _docker-py_, I'm assuming as a pre-requisite of _docker-compose_ and it was conflicting with the newer _docker_ pip package. I discovered this after I ran into further issues when pushing the image (see this issue)
Prior to removing _docker-py_, the following code would throw the error TypeError: 'Image' object is not iterable. Now that it has been removed and replaced with the docker pip package, all is well.
import docker
client = docker.from_env()
i,log = client.images.build(path=cwd)
for line in log:
print line
Most helpful comment
Ah, this is a mistake - the stream parameter doesn't do anything (it's
deprecated as noted).
For streaming build logs, you'll want the low-level build method on
APIClient (
client.api.build).http://docker-py.readthedocs.io/en/stable/api.html#module-docker.api.build