1.9.30 sync.
[gae.git] / python / lib / docker / README.md
blob52139b217a1ceb96d9034a6d5853b16bf1f26571
1 docker-py
2 =========
4 [![Build Status](https://travis-ci.org/docker/docker-py.png)](https://travis-ci.org/docker/docker-py)
6 An API client for docker written in Python
8 Installation
9 ============
11 Our latest stable is always available on PyPi.
13     pip install docker-py
15 API
16 ===
18 To instantiate a `Client` class that will allow you to communicate with
19 a Docker daemon, simply do:
21 ```python
22 c = docker.Client(base_url='unix://var/run/docker.sock',
23                   version='1.12',
24                   timeout=10)
25 ```
27 `base_url` refers to the protocol+hostname+port where the docker server
28 is hosted. `version` is the version of the API the client will use and
29 `timeout` specifies the HTTP request timeout, in seconds.
31 ```python
32 c.build(path=None, tag=None, quiet=False, fileobj=None, nocache=False,
33         rm=False, stream=False, timeout=None,
34         custom_context=False, encoding=None):
35 ```
37 Similar to the `docker build` command. Either `path` or `fileobj` needs
38 to be set. `path` can be a local path (to a directory containing a
39 Dockerfile) or a remote URL. `fileobj` must be a readable file-like
40 object to a Dockerfile.
42 If you have a tar file for the docker build context (including a dockerfile)
43 already, pass a readable file-like object to `fileobj` and also pass
44 `custom_context=True`. If the stream is compressed also, set `encoding` to
45 the correct value (e.g `gzip`).
47 ```python
48 c.commit(container, repository=None, tag=None, message=None, author=None,
49          conf=None)
50 ```
52 Identical to the `docker commit` command.
54 ```python
55 c.containers(quiet=False, all=False, trunc=True, latest=False, since=None,
56              before=None, limit=-1)
57 ```
59 Identical to the `docker ps` command.
61 ```python
62 c.copy(container, resource)
63 ```
65 Identical to the `docker cp` command.
67 ```python
68 c.create_container(image, command=None, hostname=None, user=None,
69                    detach=False, stdin_open=False, tty=False, mem_limit=0,
70                    ports=None, environment=None, dns=None, volumes=None,
71                    volumes_from=None, network_disabled=False, name=None,
72                    entrypoint=None, cpu_shares=None, working_dir=None,
73                    memswap_limit=0)
74 ```
76 Creates a container that can then be `start`ed. Parameters are similar
77 to those for the `docker run` command except it doesn't support the
78 attach options (`-a`). See "Port bindings" and "Using volumes" below for
79 more information on how to create port bindings and volume mappings.
81 `command` is the command to be run in the container. String or list types are
82 accepted.
84 The `environment` variable accepts a dictionary or a list of strings
85 in the following format `["PASSWORD=xxx"]` or `{"PASSWORD": "xxx"}`.
87 The `mem_limit` variable accepts float values (which represent the memory limit
88 of the created container in bytes) or a string with a units identification char
89 ('100000b', 1000k', 128m', '1g'). If a string is specified without a units
90 character, bytes are assumed as an intended unit.
92 `volumes_from` and `dns` arguments raise TypeError exception if they are used
93 against v1.10 of docker remote API. Those arguments should be passed to
94 `start()` instead.
97 ```python
98 c.diff(container)
99 ```
101 Identical to the `docker diff` command.
103 ```python
104 c.export(container)
107 Identical to the `docker export` command.
109 ```python
110 c.history(image)
113 Identical to the `docker history` command.
115 ```python
116 c.images(name=None, quiet=False, all=False, viz=False)
119 Identical to the `docker images` command.
121 ```python
122 c.import_image(src, data=None, repository=None, tag=None)
125 Identical to the `docker import` command. If `src` is a string or
126 unicode string, it will be treated as a URL to fetch the image from. To
127 import an image from the local machine, `src` needs to be a file-like
128 object or bytes collection.  To import from a tarball use your absolute
129 path to your tarball.  To load arbitrary data as tarball use whatever
130 you want as src and your tarball content in data.
132 ```python
133 c.info()
136 Identical to the `docker info` command.
138 ```python
139 c.insert(image, url, path)
142 Identical to the `docker insert` command.
144 ```python
145 c.inspect_container(container)
148 Identical to the `docker inspect` command, but only for containers.
150 ```python
151 c.inspect_image(image_id)
154 Identical to the `docker inspect` command, but only for images.
156 ```python
157 c.kill(container, signal=None)
160 Kill a container. Similar to the `docker kill` command.
162 ```python
163 c.login(username, password=None, email=None, registry=None)
166 Identical to the `docker login` command (but non-interactive, obviously).
168 ```python
169 c.logs(container, stdout=True, stderr=True, stream=False, timestamps=False)
172 Identical to the `docker logs` command. The `stream` parameter makes the
173 `logs` function return a blocking generator you can iterate over to
174 retrieve log output as it happens.
176 ```python
177 c.attach(container, stdout=True, stderr=True, stream=False, logs=False)
180 The `logs` function is a wrapper around this one, which you can use
181 instead if you want to fetch/stream container output without first
182 retrieving the entire backlog.
184 ```python
185 c.ping()
188 Hits the /_ping endpoint of the remote API and returns the result.
189 An exception will be raised if the endpoint isn't responding.
191 ```python
192 c.port(container, private_port)
195 Identical to the `docker port` command.
197 ```python
198 c.pull(repository, tag=None, stream=False)
201 Identical to the `docker pull` command.
203 ```python
204 c.push(repository, tag=None, stream=False)
207 Identical to the `docker push` command.
209 ````python
210 c.remove_container(container, v=False, link=False)
213 Remove a container. Similar to the `docker rm` command.
215 ```python
216 c.remove_image(image)
219 Remove an image. Similar to the `docker rmi` command.
221 ```python
222 c.restart(container, timeout=10)
224 Restart a container. Similar to the `docker restart` command.
226 ```python
227 c.search(term)
230 Identical to the `docker search` command.
232 ```python
233 c.start(container, binds=None, port_bindings=None, lxc_conf=None,
234         publish_all_ports=False, links=None, privileged=False,
235         dns=None, dns_search=None, volumes_from=None, network_mode=None,
236         restart_policy=None, cap_add=None, cap_drop=None)
239 Similar to the `docker start` command, but doesn't support attach
240 options.  Use `docker logs` to recover `stdout`/`stderr`.
242 `binds` allows to bind a directory in the host to the container. See
243 "Using volumes" below for more information. `port_bindings` exposes
244 container ports to the host. See "Port bindings" below for more
245 information. `lxc_conf` allows to pass LXC configuration options using a
246 dictionary. `privileged` starts the container in privileged mode.
248 [Links](http://docs.docker.io/en/latest/use/working_with_links_names/)
249 can be specified with the `links` argument. They can either be
250 specified as a dictionary mapping name to alias or as a list of
251 `(name, alias)` tuples.
253 `dns` and `volumes_from` are only available if they are used with version v1.10
254 of docker remote API. Otherwise they are ignored.
256 `network_mode` is available since v1.11 and sets the Network mode for the
257 container ('bridge': creates a new network stack for the container on the
258 docker bridge, 'none': no networking for this container, 'container:[name|id]':
259 reuses another container network stack), 'host': use the host network stack
260 inside the container.
262 `restart_policy` is available since v1.2.0 and sets the RestartPolicy for how a container should or should not be 
263 restarted on exit. By default the policy is set to no meaning do not restart the container when it exits. 
264 The user may specify the restart policy as a dictionary for example:
265 for example: 
268     "MaximumRetryCount": 0, 
269     "Name": "always"
272 for always restarting the container on exit or can specify to restart the container to restart on failure and can limit
273 number of restarts. 
274 for example:
277     "MaximumRetryCount": 5, 
278     "Name": "on-failure"
282 `cap_add` and `cap_drop` are available since v1.2.0 and can be used to add or drop certain capabilities.
283 The user may specify the capabilities as an array for example:
286     "SYS_ADMIN",
287     "MKNOD"
292 ```python
293 c.stop(container, timeout=10)
296 Stops a container. Similar to the `docker stop` command.
298 ```python
299 c.tag(image, repository, tag=None, force=False)
302 Identical to the `docker tag` command.
304 ```python
305 c.top(container)
308 Identical to the `docker top` command.
310 ```python
311 c.version()
314 Identical to the `docker version` command.
316 ```python
317 c.wait(container)
320 Wait for a container and return its exit code. Similar to the `docker
321 wait` command.
324 Port bindings
325 =============
327 Port bindings is done in two parts. Firstly, by providing a list of ports to
328 open inside the container in the `Client.create_container` method.
330 ```python
331 c.create_container('busybox', 'ls', ports=[1111, 2222])
334 Bindings are then declared in the `Client.start` method.
336 ```python
337 c.start(container_id, port_bindings={1111: 4567, 2222: None})
340 You can limit the host address on which the port will be exposed like such:
342 ```python
343 c.start(container_id, port_bindings={1111: ('127.0.0.1', 4567)})
346 Or without host port assignment:
348 ```python
349 c.start(container_id, port_bindings={1111: ('127.0.0.1',)})
352 If you wish to use UDP instead of TCP (default), you need to declare it
353 like such in both the `create_container()` and `start()` calls:
355 ```python
356 container_id = c.create_container('busybox', 'ls', ports=[(1111, 'udp'), 2222])
357 c.start(container_id, port_bindings={'1111/udp': 4567, 2222: None})
361 Using volumes
362 =============
364 Similarly, volume declaration is done in two parts. First, you have to provide
365 a list of mountpoints to the `Client.create_container` method.
367 ```python
368 c.create_container('busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2'])
371 Volume mappings are then declared inside the `Client.start` method like this:
373 ```python
374 c.start(container_id, binds={
375     '/home/user1/':
376         {
377             'bind': '/mnt/vol2',
378             'ro': False
379         },
380     '/var/www':
381         {
382             'bind': '/mnt/vol1',
383             'ro': True
384         }
388 Connection to daemon using HTTPS
389 ================================
391 *These instructions are docker-py specific. Please refer to
392 http://docs.docker.com/articles/https/ first.*
394 *  Authenticate server based on public/default CA pool
396 ```python
397 client = docker.Client(base_url='<https_url>', tls=True)
400 Equivalent CLI options: `docker --tls ...`
402 If you want to use TLS but don't want to verify the server certificate
403 (for example when testing with a self-signed certificate):
405 ```python
406 tls_config = docker.tls.TLSConfig(verify=False)
407 client = docker.Client(base_url='<https_url>', tls=tls_config)
410 * Authenticate server based on given CA
412 ```python
413 tls_config = docker.tls.TLSConfig(ca_cert='/path/to/ca.pem')
414 client = docker.Client(base_url='<https_url>', tls=tls_config)
417 Equivalent CLI options: `docker --tlsverify --tlscacert /path/to/ca.pem ...`
419 * Authenticate with client certificate, do not authenticate server
420   based on given CA
422 ```python
423 tls_config = docker.tls.TLSConfig(
424   client_cert=('/path/to/client-cert.pem', '/path/to/client-key.pem')
426 client = docker.Client(base_url='<https_url>', tls=tls_config)
429 Equivalent CLI options:
430 `docker --tls --tlscert /path/to/client-cert.pem
431 --tlskey /path/to/client-key.pem ...`
433 * Authenticate with client certificate, authenticate server based on given CA
435 ```python
436 tls_config = docker.tls.TLSConfig(
437   client_cert=('/path/to/client-cert.pem', '/path/to/client-key.pem'),
438   ca_cert='/path/to/ca.pem'
440 client = docker.Client(base_url='<https_url>', tls=tls_config)
443 Equivalent CLI options:
444 `docker --tlsverify --tlscert /path/to/client-cert.pem
445 --tlskey /path/to/client-key.pem --tlscacert /path/to/ca.pem ...`