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
11 Our latest stable is always available on PyPi.
18 To instantiate a `Client` class that will allow you to communicate with
19 a Docker daemon, simply do:
22 c = docker.Client(base_url='unix://var/run/docker.sock',
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.
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):
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`).
48 c.commit(container, repository=None, tag=None, message=None, author=None,
52 Identical to the `docker commit` command.
55 c.containers(quiet=False, all=False, trunc=True, latest=False, since=None,
56 before=None, limit=-1)
59 Identical to the `docker ps` command.
62 c.copy(container, resource)
65 Identical to the `docker cp` command.
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,
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
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
101 Identical to the `docker diff` command.
107 Identical to the `docker export` command.
113 Identical to the `docker history` command.
116 c.images(name=None, quiet=False, all=False, viz=False)
119 Identical to the `docker images` command.
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.
136 Identical to the `docker info` command.
139 c.insert(image, url, path)
142 Identical to the `docker insert` command.
145 c.inspect_container(container)
148 Identical to the `docker inspect` command, but only for containers.
151 c.inspect_image(image_id)
154 Identical to the `docker inspect` command, but only for images.
157 c.kill(container, signal=None)
160 Kill a container. Similar to the `docker kill` command.
163 c.login(username, password=None, email=None, registry=None)
166 Identical to the `docker login` command (but non-interactive, obviously).
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.
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.
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.
192 c.port(container, private_port)
195 Identical to the `docker port` command.
198 c.pull(repository, tag=None, stream=False)
201 Identical to the `docker pull` command.
204 c.push(repository, tag=None, stream=False)
207 Identical to the `docker push` command.
210 c.remove_container(container, v=False, link=False)
213 Remove a container. Similar to the `docker rm` command.
216 c.remove_image(image)
219 Remove an image. Similar to the `docker rmi` command.
222 c.restart(container, timeout=10)
224 Restart a container. Similar to the `docker restart` command.
230 Identical to the `docker search` command.
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:
268 "MaximumRetryCount": 0,
272 for always restarting the container on exit or can specify to restart the container to restart on failure and can limit
277 "MaximumRetryCount": 5,
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:
293 c.stop(container, timeout=10)
296 Stops a container. Similar to the `docker stop` command.
299 c.tag(image, repository, tag=None, force=False)
302 Identical to the `docker tag` command.
308 Identical to the `docker top` command.
314 Identical to the `docker version` command.
320 Wait for a container and return its exit code. Similar to the `docker
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.
331 c.create_container('busybox', 'ls', ports=[1111, 2222])
334 Bindings are then declared in the `Client.start` method.
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:
343 c.start(container_id, port_bindings={1111: ('127.0.0.1', 4567)})
346 Or without host port assignment:
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:
356 container_id = c.create_container('busybox', 'ls', ports=[(1111, 'udp'), 2222])
357 c.start(container_id, port_bindings={'1111/udp': 4567, 2222: None})
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.
368 c.create_container('busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2'])
371 Volume mappings are then declared inside the `Client.start` method like this:
374 c.start(container_id, binds={
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
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):
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
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
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
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 ...`