mirror of https://github.com/docker/docker-py.git
Merge pull request #138 from aanand/container-output-without-logs
container_output() method for streaming the output without logs=1
This commit is contained in:
commit
77edd8b601
|
@ -142,6 +142,14 @@ Identical to the `docker logs` command. The `stream` parameter makes the
|
||||||
`logs` function return a blocking generator you can iterate over to
|
`logs` function return a blocking generator you can iterate over to
|
||||||
retrieve log output as it happens.
|
retrieve log output as it happens.
|
||||||
|
|
||||||
|
```python
|
||||||
|
c.attach(container, stdout=True, stderr=True, stream=False, logs=False)
|
||||||
|
```
|
||||||
|
|
||||||
|
The `logs` function is a wrapper around this one, which you can use
|
||||||
|
instead if you want to fetch/stream container output without first
|
||||||
|
retrieving the entire backlog.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
c.port(container, private_port)
|
c.port(container, private_port)
|
||||||
```
|
```
|
||||||
|
|
|
@ -285,15 +285,26 @@ class Client(requests.Session):
|
||||||
break
|
break
|
||||||
yield data
|
yield data
|
||||||
|
|
||||||
def attach(self, container):
|
def attach(self, container, stdout=True, stderr=True,
|
||||||
socket = self.attach_socket(container)
|
stream=False, logs=False):
|
||||||
|
if isinstance(container, dict):
|
||||||
|
container = container.get('Id')
|
||||||
|
params = {
|
||||||
|
'logs': logs and 1 or 0,
|
||||||
|
'stdout': stdout and 1 or 0,
|
||||||
|
'stderr': stderr and 1 or 0,
|
||||||
|
'stream': stream and 1 or 0,
|
||||||
|
}
|
||||||
|
u = self._url("/containers/{0}/attach".format(container))
|
||||||
|
response = self._post(u, params=params, stream=stream)
|
||||||
|
|
||||||
while True:
|
# Stream multi-plexing was introduced in API v1.6.
|
||||||
chunk = socket.recv(4096)
|
if utils.compare_version('1.6', self._version) < 0:
|
||||||
if chunk:
|
return stream and self._stream_result(response) or \
|
||||||
yield chunk
|
self._result(response, binary=True)
|
||||||
else:
|
|
||||||
break
|
return stream and self._multiplexed_socket_stream_helper(response) or \
|
||||||
|
''.join([x for x in self._multiplexed_buffer_helper(response)])
|
||||||
|
|
||||||
def attach_socket(self, container, params=None, ws=False):
|
def attach_socket(self, container, params=None, ws=False):
|
||||||
if params is None:
|
if params is None:
|
||||||
|
@ -551,24 +562,13 @@ class Client(requests.Session):
|
||||||
return self._result(response, json=True)
|
return self._result(response, json=True)
|
||||||
|
|
||||||
def logs(self, container, stdout=True, stderr=True, stream=False):
|
def logs(self, container, stdout=True, stderr=True, stream=False):
|
||||||
if isinstance(container, dict):
|
return self.attach(
|
||||||
container = container.get('Id')
|
container,
|
||||||
params = {
|
stdout=stdout,
|
||||||
'logs': 1,
|
stderr=stderr,
|
||||||
'stdout': stdout and 1 or 0,
|
stream=stream,
|
||||||
'stderr': stderr and 1 or 0,
|
logs=True
|
||||||
'stream': stream and 1 or 0,
|
)
|
||||||
}
|
|
||||||
u = self._url("/containers/{0}/attach".format(container))
|
|
||||||
response = self._post(u, params=params, stream=stream)
|
|
||||||
|
|
||||||
# Stream multi-plexing was introduced in API v1.6.
|
|
||||||
if utils.compare_version('1.6', self._version) < 0:
|
|
||||||
return stream and self._stream_result(response) or \
|
|
||||||
self._result(response, binary=True)
|
|
||||||
|
|
||||||
return stream and self._multiplexed_socket_stream_helper(response) or \
|
|
||||||
''.join([x for x in self._multiplexed_buffer_helper(response)])
|
|
||||||
|
|
||||||
def port(self, container, private_port):
|
def port(self, container, private_port):
|
||||||
if isinstance(container, dict):
|
if isinstance(container, dict):
|
||||||
|
|
Loading…
Reference in New Issue