Skip to content

Commit 16c2809

Browse files
committed
Move exec_run example to user guides section of docs
Signed-off-by: Joffrey F <[email protected]>
1 parent b2ad302 commit 16c2809

File tree

5 files changed

+79
-64
lines changed

5 files changed

+79
-64
lines changed

docker/models/containers.py

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -181,70 +181,6 @@ def exec_run(self, cmd, stdout=True, stderr=True, stdin=False, tty=False,
181181
Raises:
182182
:py:class:`docker.errors.APIError`
183183
If the server returns an error.
184-
185-
Example:
186-
187-
Create a container that runs in the background
188-
189-
>>> client = docker.from_env()
190-
>>> container = client.containers.run(
191-
... 'bfirsh/reticulate-splines', detach=True)
192-
193-
Prepare the command we are going to use. It prints "hello stdout"
194-
in `stdout`, followed by "hello stderr" in `stderr`:
195-
196-
>>> cmd = '/bin/sh -c "echo hello stdout ; echo hello stderr >&2"'
197-
198-
We'll run this command with all four the combinations of ``stream``
199-
and ``demux``.
200-
201-
With ``stream=False`` and ``demux=False``, the output is a string
202-
that contains both the `stdout` and the `stderr` output:
203-
204-
>>> res = container.exec_run(cmd, stream=False, demux=False)
205-
>>> res.output
206-
b'hello stderr\nhello stdout\n'
207-
208-
With ``stream=True``, and ``demux=False``, the output is a
209-
generator that yields strings containing the output of both
210-
`stdout` and `stderr`:
211-
212-
>>> res = container.exec_run(cmd, stream=True, demux=False)
213-
>>> next(res.output)
214-
b'hello stdout\n'
215-
>>> next(res.output)
216-
b'hello stderr\n'
217-
>>> next(res.output)
218-
Traceback (most recent call last):
219-
File "<stdin>", line 1, in <module>
220-
StopIteration
221-
222-
With ``stream=True`` and ``demux=True``, the generator now
223-
separates the streams, and yield tuples
224-
``(stdout, stderr)``:
225-
226-
>>> res = container.exec_run(cmd, stream=True, demux=True)
227-
>>> next(res.output)
228-
(b'hello stdout\n', None)
229-
>>> next(res.output)
230-
(None, b'hello stderr\n')
231-
>>> next(res.output)
232-
Traceback (most recent call last):
233-
File "<stdin>", line 1, in <module>
234-
StopIteration
235-
236-
Finally, with ``stream=False`` and ``demux=True``, the whole output
237-
is returned, but the streams are still separated:
238-
239-
>>> res = container.exec_run(cmd, stream=True, demux=True)
240-
>>> next(res.output)
241-
(b'hello stdout\n', None)
242-
>>> next(res.output)
243-
(None, b'hello stderr\n')
244-
>>> next(res.output)
245-
Traceback (most recent call last):
246-
File "<stdin>", line 1, in <module>
247-
StopIteration
248184
"""
249185
resp = self.client.api.exec_create(
250186
self.id, cmd, stdout=stdout, stderr=stderr, stdin=stdin, tty=tty,

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,5 @@ That's just a taste of what you can do with the Docker SDK for Python. For more,
9292
volumes
9393
api
9494
tls
95+
user_guides/index
9596
change-log

docs/user_guides/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
User guides and tutorials
2+
=========================
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
multiplex
8+
swarm_services

docs/user_guides/multiplex.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Handling multiplexed streams
2+
============================
3+
4+
.. note::
5+
The following instruction assume you're interested in getting output from
6+
an ``exec`` command. These instruction are similarly applicable to the
7+
output of ``attach``.
8+
9+
First create a container that runs in the background:
10+
11+
>>> client = docker.from_env()
12+
>>> container = client.containers.run(
13+
... 'bfirsh/reticulate-splines', detach=True)
14+
15+
Prepare the command we are going to use. It prints "hello stdout"
16+
in `stdout`, followed by "hello stderr" in `stderr`:
17+
18+
>>> cmd = '/bin/sh -c "echo hello stdout ; echo hello stderr >&2"'
19+
We'll run this command with all four the combinations of ``stream``
20+
and ``demux``.
21+
With ``stream=False`` and ``demux=False``, the output is a string
22+
that contains both the `stdout` and the `stderr` output:
23+
>>> res = container.exec_run(cmd, stream=False, demux=False)
24+
>>> res.output
25+
b'hello stderr\nhello stdout\n'
26+
27+
With ``stream=True``, and ``demux=False``, the output is a
28+
generator that yields strings containing the output of both
29+
`stdout` and `stderr`:
30+
31+
>>> res = container.exec_run(cmd, stream=True, demux=False)
32+
>>> next(res.output)
33+
b'hello stdout\n'
34+
>>> next(res.output)
35+
b'hello stderr\n'
36+
>>> next(res.output)
37+
Traceback (most recent call last):
38+
File "<stdin>", line 1, in <module>
39+
StopIteration
40+
41+
With ``stream=True`` and ``demux=True``, the generator now
42+
separates the streams, and yield tuples
43+
``(stdout, stderr)``:
44+
45+
>>> res = container.exec_run(cmd, stream=True, demux=True)
46+
>>> next(res.output)
47+
(b'hello stdout\n', None)
48+
>>> next(res.output)
49+
(None, b'hello stderr\n')
50+
>>> next(res.output)
51+
Traceback (most recent call last):
52+
File "<stdin>", line 1, in <module>
53+
StopIteration
54+
55+
Finally, with ``stream=False`` and ``demux=True``, the whole output
56+
is returned, but the streams are still separated:
57+
58+
>>> res = container.exec_run(cmd, stream=True, demux=True)
59+
>>> next(res.output)
60+
(b'hello stdout\n', None)
61+
>>> next(res.output)
62+
(None, b'hello stderr\n')
63+
>>> next(res.output)
64+
Traceback (most recent call last):
65+
File "<stdin>", line 1, in <module>
66+
StopIteration

docs/user_guides/swarm_services.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Swarm services
22

3+
> Warning:
4+
> This is a stale document and may contain outdated information.
5+
> Refer to the API docs for updated classes and method signatures.
6+
37
Starting with Engine version 1.12 (API 1.24), it is possible to manage services
48
using the Docker Engine API. Note that the engine needs to be part of a
59
[Swarm cluster](../swarm.rst) before you can use the service-related methods.

0 commit comments

Comments
 (0)