Skip to content

Commit 302cb78

Browse files
authored
Merge pull request #2172 from docker/fix_docs
Documentation fixes
2 parents 5467658 + 35b9460 commit 302cb78

File tree

11 files changed

+137
-48
lines changed

11 files changed

+137
-48
lines changed

docker/api/container.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -473,16 +473,12 @@ def create_host_config(self, *args, **kwargs):
473473
signals and reaps processes
474474
init_path (str): Path to the docker-init binary
475475
ipc_mode (str): Set the IPC mode for the container.
476-
isolation (str): Isolation technology to use. Default: `None`.
477-
links (dict or list of tuples): Either a dictionary mapping name
478-
to alias or as a list of ``(name, alias)`` tuples.
479-
log_config (dict): Logging configuration, as a dictionary with
480-
keys:
481-
482-
- ``type`` The logging driver name.
483-
- ``config`` A dictionary of configuration for the logging
484-
driver.
485-
476+
isolation (str): Isolation technology to use. Default: ``None``.
477+
links (dict): Mapping of links using the
478+
``{'container': 'alias'}`` format. The alias is optional.
479+
Containers declared in this dict will be linked to the new
480+
container using the provided alias. Default: ``None``.
481+
log_config (LogConfig): Logging configuration
486482
lxc_conf (dict): LXC config.
487483
mem_limit (float or str): Memory limit. Accepts float values
488484
(which represent the memory limit of the created container in
@@ -543,7 +539,7 @@ def create_host_config(self, *args, **kwargs):
543539
}
544540
545541
ulimits (:py:class:`list`): Ulimits to set inside the container,
546-
as a list of dicts.
542+
as a list of :py:class:`docker.types.Ulimit` instances.
547543
userns_mode (str): Sets the user namespace mode for the container
548544
when user namespace remapping option is enabled. Supported
549545
values are: ``host``
@@ -611,9 +607,10 @@ def create_endpoint_config(self, *args, **kwargs):
611607
aliases (:py:class:`list`): A list of aliases for this endpoint.
612608
Names in that list can be used within the network to reach the
613609
container. Defaults to ``None``.
614-
links (:py:class:`list`): A list of links for this endpoint.
615-
Containers declared in this list will be linked to this
616-
container. Defaults to ``None``.
610+
links (dict): Mapping of links for this endpoint using the
611+
``{'container': 'alias'}`` format. The alias is optional.
612+
Containers declared in this dict will be linked to this
613+
container using the provided alias. Defaults to ``None``.
617614
ipv4_address (str): The IP address of this container on the
618615
network, using the IPv4 protocol. Defaults to ``None``.
619616
ipv6_address (str): The IP address of this container on the
@@ -628,7 +625,7 @@ def create_endpoint_config(self, *args, **kwargs):
628625
629626
>>> endpoint_config = client.create_endpoint_config(
630627
aliases=['web', 'app'],
631-
links=['app_db'],
628+
links={'app_db': 'db', 'another': None},
632629
ipv4_address='132.65.0.123'
633630
)
634631
@@ -697,6 +694,18 @@ def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
697694
Raises:
698695
:py:class:`docker.errors.APIError`
699696
If the server returns an error.
697+
698+
Example:
699+
700+
>>> c = docker.APIClient()
701+
>>> f = open('./sh_bin.tar', 'wb')
702+
>>> bits, stat = c.get_archive(container, '/bin/sh')
703+
>>> print(stat)
704+
{'name': 'sh', 'size': 1075464, 'mode': 493,
705+
'mtime': '2018-10-01T15:37:48-07:00', 'linkTarget': ''}
706+
>>> for chunk in bits:
707+
... f.write(chunk)
708+
>>> f.close()
700709
"""
701710
params = {
702711
'path': path
@@ -1074,7 +1083,8 @@ def stats(self, container, decode=None, stream=True):
10741083
Args:
10751084
container (str): The container to stream statistics from
10761085
decode (bool): If set to true, stream will be decoded into dicts
1077-
on the fly. False by default.
1086+
on the fly. Only applicable if ``stream`` is True.
1087+
False by default.
10781088
stream (bool): If set to false, only the current stats will be
10791089
returned instead of a stream. True by default.
10801090
@@ -1088,6 +1098,10 @@ def stats(self, container, decode=None, stream=True):
10881098
return self._stream_helper(self._get(url, stream=True),
10891099
decode=decode)
10901100
else:
1101+
if decode:
1102+
raise errors.InvalidArgument(
1103+
"decode is only available in conjuction with stream=True"
1104+
)
10911105
return self._result(self._get(url, params={'stream': False}),
10921106
json=True)
10931107

docker/api/daemon.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def events(self, since=None, until=None, filters=None, decode=None):
4242
4343
Example:
4444
45-
>>> for event in client.events()
46-
... print event
45+
>>> for event in client.events(decode=True)
46+
... print(event)
4747
{u'from': u'image/with:tag',
4848
u'id': u'container-id',
4949
u'status': u'start',
@@ -54,7 +54,7 @@ def events(self, since=None, until=None, filters=None, decode=None):
5454
5555
>>> events = client.events()
5656
>>> for event in events:
57-
... print event
57+
... print(event)
5858
>>> # and cancel from another thread
5959
>>> events.close()
6060
"""

docker/api/image.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_image(self, image, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
3232
Example:
3333
3434
>>> image = cli.get_image("busybox:latest")
35-
>>> f = open('/tmp/busybox-latest.tar', 'w')
35+
>>> f = open('/tmp/busybox-latest.tar', 'wb')
3636
>>> for chunk in image:
3737
>>> f.write(chunk)
3838
>>> f.close()
@@ -352,8 +352,8 @@ def pull(self, repository, tag=None, stream=False, auth_config=None,
352352
353353
Example:
354354
355-
>>> for line in cli.pull('busybox', stream=True):
356-
... print(json.dumps(json.loads(line), indent=4))
355+
>>> for line in cli.pull('busybox', stream=True, decode=True):
356+
... print(json.dumps(line, indent=4))
357357
{
358358
"status": "Pulling image (latest) from busybox",
359359
"progressDetail": {},
@@ -428,12 +428,12 @@ def push(self, repository, tag=None, stream=False, auth_config=None,
428428
If the server returns an error.
429429
430430
Example:
431-
>>> for line in cli.push('yourname/app', stream=True):
432-
... print line
433-
{"status":"Pushing repository yourname/app (1 tags)"}
434-
{"status":"Pushing","progressDetail":{},"id":"511136ea3c5a"}
435-
{"status":"Image already pushed, skipping","progressDetail":{},
436-
"id":"511136ea3c5a"}
431+
>>> for line in cli.push('yourname/app', stream=True, decode=True):
432+
... print(line)
433+
{'status': 'Pushing repository yourname/app (1 tags)'}
434+
{'status': 'Pushing','progressDetail': {}, 'id': '511136ea3c5a'}
435+
{'status': 'Image already pushed, skipping', 'progressDetail':{},
436+
'id': '511136ea3c5a'}
437437
...
438438
439439
"""

docker/api/service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ def inspect_service(self, service, insert_defaults=None):
197197
into the service inspect output.
198198
199199
Returns:
200-
``True`` if successful.
200+
(dict): A dictionary of the server-side representation of the
201+
service, including all relevant properties.
201202
202203
Raises:
203204
:py:class:`docker.errors.APIError`

docker/models/containers.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515

1616

1717
class Container(Model):
18-
18+
""" Local representation of a container object. Detailed configuration may
19+
be accessed through the :py:attr:`attrs` attribute. Note that local
20+
attributes are cached; users may call :py:meth:`reload` to
21+
query the Docker daemon for the current properties, causing
22+
:py:attr:`attrs` to be refreshed.
23+
"""
1924
@property
2025
def name(self):
2126
"""
@@ -228,6 +233,17 @@ def get_archive(self, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
228233
Raises:
229234
:py:class:`docker.errors.APIError`
230235
If the server returns an error.
236+
237+
Example:
238+
239+
>>> f = open('./sh_bin.tar', 'wb')
240+
>>> bits, stat = container.get_archive('/bin/sh')
241+
>>> print(stat)
242+
{'name': 'sh', 'size': 1075464, 'mode': 493,
243+
'mtime': '2018-10-01T15:37:48-07:00', 'linkTarget': ''}
244+
>>> for chunk in bits:
245+
... f.write(chunk)
246+
>>> f.close()
231247
"""
232248
return self.client.api.get_archive(self.id, path, chunk_size)
233249

@@ -380,7 +396,8 @@ def stats(self, **kwargs):
380396
381397
Args:
382398
decode (bool): If set to true, stream will be decoded into dicts
383-
on the fly. False by default.
399+
on the fly. Only applicable if ``stream`` is True.
400+
False by default.
384401
stream (bool): If set to false, only the current stats will be
385402
returned instead of a stream. True by default.
386403
@@ -574,15 +591,11 @@ def run(self, image, command=None, stdout=True, stderr=False,
574591
``{"label1": "value1", "label2": "value2"}``) or a list of
575592
names of labels to set with empty values (e.g.
576593
``["label1", "label2"]``)
577-
links (dict or list of tuples): Either a dictionary mapping name
578-
to alias or as a list of ``(name, alias)`` tuples.
579-
log_config (dict): Logging configuration, as a dictionary with
580-
keys:
581-
582-
- ``type`` The logging driver name.
583-
- ``config`` A dictionary of configuration for the logging
584-
driver.
585-
594+
links (dict): Mapping of links using the
595+
``{'container': 'alias'}`` format. The alias is optional.
596+
Containers declared in this dict will be linked to the new
597+
container using the provided alias. Default: ``None``.
598+
log_config (LogConfig): Logging configuration.
586599
mac_address (str): MAC address to assign to the container.
587600
mem_limit (int or str): Memory limit. Accepts float values
588601
(which represent the memory limit of the created container in
@@ -691,8 +704,8 @@ def run(self, image, command=None, stdout=True, stderr=False,
691704
}
692705
693706
tty (bool): Allocate a pseudo-TTY.
694-
ulimits (:py:class:`list`): Ulimits to set inside the container, as
695-
a list of dicts.
707+
ulimits (:py:class:`list`): Ulimits to set inside the container,
708+
as a list of :py:class:`docker.types.Ulimit` instances.
696709
user (str or int): Username or UID to run commands as inside the
697710
container.
698711
userns_mode (str): Sets the user namespace mode for the container

docker/models/images.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def save(self, chunk_size=DEFAULT_DATA_CHUNK_SIZE, named=False):
8484
Example:
8585
8686
>>> image = cli.get_image("busybox:latest")
87-
>>> f = open('/tmp/busybox-latest.tar', 'w')
87+
>>> f = open('/tmp/busybox-latest.tar', 'wb')
8888
>>> for chunk in image:
8989
>>> f.write(chunk)
9090
>>> f.close()

docker/types/containers.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@ class LogConfigTypesEnum(object):
2323

2424

2525
class LogConfig(DictType):
26+
"""
27+
Configure logging for a container, when provided as an argument to
28+
:py:meth:`~docker.api.container.ContainerApiMixin.create_host_config`.
29+
You may refer to the
30+
`official logging driver documentation <https://docs.docker.com/config/containers/logging/configure/>`_
31+
for more information.
32+
33+
Args:
34+
type (str): Indicate which log driver to use. A set of valid drivers
35+
is provided as part of the :py:attr:`LogConfig.types`
36+
enum. Other values may be accepted depending on the engine version
37+
and available logging plugins.
38+
config (dict): A driver-dependent configuration dictionary. Please
39+
refer to the driver's documentation for a list of valid config
40+
keys.
41+
42+
Example:
43+
44+
>>> from docker.types import LogConfig
45+
>>> lc = LogConfig(type=LogConfig.types.JSON, config={
46+
... 'max-size': '1g',
47+
... 'labels': 'production_status,geo'
48+
... })
49+
>>> hc = client.create_host_config(log_config=lc)
50+
>>> container = client.create_container('busybox', 'true',
51+
... host_config=hc)
52+
>>> client.inspect_container(container)['HostConfig']['LogConfig']
53+
{'Type': 'json-file', 'Config': {'labels': 'production_status,geo', 'max-size': '1g'}}
54+
55+
""" # flake8: noqa
2656
types = LogConfigTypesEnum
2757

2858
def __init__(self, **kwargs):
@@ -50,14 +80,40 @@ def config(self):
5080
return self['Config']
5181

5282
def set_config_value(self, key, value):
83+
""" Set a the value for ``key`` to ``value`` inside the ``config``
84+
dict.
85+
"""
5386
self.config[key] = value
5487

5588
def unset_config(self, key):
89+
""" Remove the ``key`` property from the ``config`` dict. """
5690
if key in self.config:
5791
del self.config[key]
5892

5993

6094
class Ulimit(DictType):
95+
"""
96+
Create a ulimit declaration to be used with
97+
:py:meth:`~docker.api.container.ContainerApiMixin.create_host_config`.
98+
99+
Args:
100+
101+
name (str): Which ulimit will this apply to. A list of valid names can
102+
be found `here <http://tinyurl.me/ZWRkM2Ztwlykf>`_.
103+
soft (int): The soft limit for this ulimit. Optional.
104+
hard (int): The hard limit for this ulimit. Optional.
105+
106+
Example:
107+
108+
>>> nproc_limit = docker.types.Ulimit(name='nproc', soft=1024)
109+
>>> hc = client.create_host_config(ulimits=[nproc_limit])
110+
>>> container = client.create_container(
111+
'busybox', 'true', host_config=hc
112+
)
113+
>>> client.inspect_container(container)['HostConfig']['Ulimits']
114+
[{'Name': 'nproc', 'Hard': 0, 'Soft': 1024}]
115+
116+
"""
61117
def __init__(self, **kwargs):
62118
name = kwargs.get('name', kwargs.get('Name'))
63119
soft = kwargs.get('soft', kwargs.get('Soft'))

docker/types/daemon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CancellableStream(object):
1515
Example:
1616
>>> events = client.events()
1717
>>> for event in events:
18-
... print event
18+
... print(event)
1919
>>> # and cancel from another thread
2020
>>> events.close()
2121
"""

docker/utils/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,10 @@ def convert_filters(filters):
386386
v = 'true' if v else 'false'
387387
if not isinstance(v, list):
388388
v = [v, ]
389-
result[k] = v
389+
result[k] = [
390+
str(item) if not isinstance(item, six.string_types) else item
391+
for item in v
392+
]
390393
return json.dumps(result)
391394

392395

@@ -441,7 +444,7 @@ def normalize_links(links):
441444
if isinstance(links, dict):
442445
links = six.iteritems(links)
443446

444-
return ['{0}:{1}'.format(k, v) for k, v in sorted(links)]
447+
return ['{0}:{1}'.format(k, v) if v else k for k, v in sorted(links)]
445448

446449

447450
def parse_env_file(env_file):

docs/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Configuration types
140140
.. autoclass:: Healthcheck
141141
.. autoclass:: IPAMConfig
142142
.. autoclass:: IPAMPool
143+
.. autoclass:: LogConfig
143144
.. autoclass:: Mount
144145
.. autoclass:: Placement
145146
.. autoclass:: Privileges
@@ -151,4 +152,5 @@ Configuration types
151152
.. autoclass:: SwarmExternalCA
152153
.. autoclass:: SwarmSpec(*args, **kwargs)
153154
.. autoclass:: TaskTemplate
155+
.. autoclass:: Ulimit
154156
.. autoclass:: UpdateConfig

0 commit comments

Comments
 (0)