An API client for docker written in Python
docker.Client(base_url='unix://var/run/docker.sock', version="1.4")
Client class. base_url refers to the protocol+hostname+port where the docker
server is hosted. Version is the version of the API the client will use.
-
c.build(path=None, tag=None, quiet=False, fileobj=None, nocache=False, rm=False)
Similar to thedocker buildcommand. Eitherpathorfileobjneeds to be set.pathcan be a local path (to a directory containing a Dockerfile) or a remote URL.fileobjmust be a readable file-like object to a Dockerfile. -
c.commit(container, repository=None, tag=None, message=None, author=None, conf=None)
Identical to thedocker commitcommand. -
c.containers(quiet=False, all=False, trunc=True, latest=False, since=None,before=None, limit=-1)
Identical to thedocker pscommand. -
c.copy(container, resource)
Identical to thedocker cpcommand. -
c.create_container(image, command=None, hostname=None, user=None, detach=False,stdin_open=False, tty=False, mem_limit=0, ports=None, environment=None, dns=None,volumes=None, volumes_from=None, privileged=False)
Creates a container that can then bestarted. Parameters are similar to those for thedocker runcommand except it doesn't support the attach options (-a)
In order to create volumes that can be rebinded at start time, use the following syntax:volumes={"/srv": "" }. Theportsparameter is a dictionary whose key is the port to expose and the value is an empty dictionary:ports={"2181/tcp": {}}. Note, this will simply expose the ports in the container, but does not make them available on the host. Seestartbelow. -
c.diff(container)
Identical to thedocker diffcommand. -
c.export(container)
Identical to thedocker exportcommand. -
c.history(image)
Identical to thedocker historycommand. -
c.images(name=None, quiet=False, all=False, viz=False)
Identical to thedocker imagescommand. -
c.import_image(src, repository=None, tag=None)
Identical to thedocker importcommand. Ifsrcis a string or unicode string, it will be treated as a URL to fetch the image from. To import an image from the local machine,srcneeds to be a file-like object or bytes collection. To import from a tarball use your absolute path to your tarball. To load arbitrary data as tarball use whatever you want as src and your tarball content in data. -
c.info()
Identical to thedocker infocommand. -
c.insert(url, path)
Identical to thedocker insertcommand. -
c.inspect_container(container)
Identical to thedocker inspectcommand, but only for containers. -
c.inspect_image(image_id)
Identical to thedocker inspectcommand, but only for images. -
c.kill(container)
Kill a container. Similar to thedocker killcommand. -
c.login(username, password=None, email=None)
Identical to thedocker logincommand (but non-interactive, obviously). -
c.logs(container)
Identical to thedocker logscommand. -
c.port(container, private_port)
Identical to thedocker portcommand. -
c.pull(repository, tag=None)Identical to thedocker pullcommand. -
c.push(repository)
Identical to thedocker pushcommand. -
c.remove_container(container, v=False)
Remove a container. Similar to thedocker rmcommand. -
c.remove_image(image)
Remove an image. Similar to thedocker rmicommand. -
c.restart(container, timeout=10)
Restart a container. Similar to thedocker restartcommand. -
c.search(term)
Identical to thedocker searchcommand. -
c.start(container, binds=None, port_bindings=None, lxc_conf=None)Similar to thedocker startcommand, but doesn't support attach options. Usedocker logsto recoverstdout/stderr
bindsAllows to bind a directory in the host to the container. Similar to thedocker runcommand with option-v="/host:/mnt". Note that you must declare "blank" volumes at container creation to use binds.
Example of binds mapping from host to container:{'/mnt/srv/': '/srv'}
port_bindingsExposes container ports to the host. This is a dictionary whose key is the container's port and the value is a[{'HostIp': '' 'HostPort': ''}]list. LeavingHostIpblank will expose the port on all host interfaces. By leaving theHostPortblank, Docker will automatically assign a port. For example:port_bindings={"2181/tcp": [{'HostIp': '' 'HostPort': ''}]}.lxc_confallows to pass LXC configuration options using a dictionary in the form of Key Value pairs. For example:lxc_conf=[{"Key":"lxc.cgroup.cpu.shares","Value":"1"}, {"Key": ..., "Value": }]. -
c.stop(container, timeout=10)
Stops a container. Similar to thedocker stopcommand. -
c.tag(image, repository, tag=None, force=False)
Identical to thedocker tagcommand. -
c.top(container_id)
Identical to thedocker topcommand. -
c.version()
Identical to thedocker versioncommand. -
c.wait(container)
Wait for a container and return its exit code. Similar to thedocker waitcommand.
