Python Client Library API for Docker

In this tutorial we will learn how to use Docker Python Client Library https://github.com/docker/docker-py

Installation

Python 2.x

$ pip install docker-py

Python 3.x

$ pip3 install docker-py

Connect to Docker Dameon

from docker import client
cli = Client(base_url='unix://var/run/docker.sock')

Get List of Containers

cli.containers()
[{'Command': '/bin/bash',
  'Created': 1447062465,
  'HostConfig': {'NetworkMode': 'default'},
  'Id': '7f7e943a8a854c09e9c14fdf37bfc652844621bb4cac2f308a0a3874469c1879',
  'Image': 'ubuntu',
  'ImageID': '07f8e8c5e66084bef8f848877857537ffe1c47edd01a93af27e7161672ad0e95',
  'Labels': {},
  'Names': ['/high_kare'],
  'Ports': [],
  'Status': 'Up 4 weeks'}]

Create Container

container = cli.create_container(image='ubuntu:latest', command='/bin/sleep 30')
print(container['Id'])
0f477c47c911d022d5bfd66617b7a568763b7dbf740ba46c066ae8d824bc4421
cli.containers()
[{'Command': '/bin/bash',
  'Created': 1447062465,
  'HostConfig': {'NetworkMode': 'default'},
  'Id': '7f7e943a8a854c09e9c14fdf37bfc652844621bb4cac2f308a0a3874469c1879',
  'Image': 'ubuntu',
  'ImageID': '07f8e8c5e66084bef8f848877857537ffe1c47edd01a93af27e7161672ad0e95',
  'Labels': {},
  'Names': ['/high_kare'],
  'Ports': [],
  'Status': 'Up 4 weeks'}]

Inspect Container

cli.inspect_container('7f7e943a8a854c09e9c14fdf37bfc652844621bb4cac2f308a0a3874469c1879')['Name']
'/high_kare'

Container Commit

cli.commit('7f7e943a8a854c09e9c14fdf37bfc652844621bb4cac2f308a0a3874469c1879', repository='test/container-1',
          tag='version1')
{'Id': 'bc0bfb26f00e3c883294ac284cf259d500ce2a1a7a947423dc07b1840944ab33'}

Container Restart

container_id = '7f7e943a8a854c09e9c14fdf37bfc652844621bb4cac2f308a0a3874469c1879'
try:
  cli.restart(container_id)
except Exception as e:
    print(e)
500 Server Error: Internal Server Error ("b'Cannot restart container 7f7e943a8a854c09e9c14fdf37bfc652844621bb4cac2f308a0a3874469c1879: [2] Container does not exist: container destroyed'")

Images

Get List of images using images() method.

images = cli.images()
print(images[0])
{'Size': 15, 'Created': 1449850121, 'ParentId': '07f8e8c5e66084bef8f848877857537ffe1c47edd01a93af27e7161672ad0e95', 'VirtualSize': 188304310, 'Labels': {}, 'RepoTags': ['test/container-1:version1'], 'Id': 'bc0bfb26f00e3c883294ac284cf259d500ce2a1a7a947423dc07b1840944ab33', 'RepoDigests': []}

Inspect Image

cli.inspect_image('bc0bfb26f00e3c883294ac284cf259d500ce2a1a7a947423dc07b1840944ab33')
{'Architecture': 'amd64',
 'Author': '',
 'Comment': '',
 'Config': {'AttachStderr': False,
  'AttachStdin': False,
  'AttachStdout': False,
  'Cmd': ['/bin/bash'],
  'Domainname': '',
  'Entrypoint': None,
  'Env': None,
  'Hostname': '',
  'Image': '',
  'Labels': {},
  'OnBuild': None,
  'OpenStdin': False,
  'StdinOnce': False,
  'Tty': False,
  'User': '',
  'Volumes': {'/volume1': {}},
  'WorkingDir': ''},
 'Container': '7f7e943a8a854c09e9c14fdf37bfc652844621bb4cac2f308a0a3874469c1879',
 'ContainerConfig': {'AttachStderr': True,
  'AttachStdin': True,
  'AttachStdout': True,
  'Cmd': ['/bin/bash'],
  'Domainname': '',
  'Entrypoint': None,
  'Env': None,
  'Hostname': '7f7e943a8a85',
  'Image': 'ubuntu',
  'Labels': {},
  'OnBuild': None,
  'OpenStdin': True,
  'StdinOnce': True,
  'StopSignal': 'SIGTERM',
  'Tty': True,
  'User': '',
  'Volumes': {'/volume1': {}},
  'WorkingDir': ''},
 'Created': '2015-12-11T16:08:41.294792143Z',
 'DockerVersion': '1.9.0',
 'GraphDriver': {'Data': None, 'Name': 'aufs'},
 'Id': 'bc0bfb26f00e3c883294ac284cf259d500ce2a1a7a947423dc07b1840944ab33',
 'Os': 'linux',
 'Parent': '07f8e8c5e66084bef8f848877857537ffe1c47edd01a93af27e7161672ad0e95',
 'RepoDigests': [],
 'RepoTags': ['test/container-1:version1'],
 'Size': 15,
 'VirtualSize': 188304310}

Volumes

Get List of Volumes

volumes = cli.volumes()
print(volumes['Volumes'][0])
{'Name': '07c473532ed7024bda91889e2467bbb1709df29e62ae0359c26cb24e2ab5e227', 'Driver': 'local', 'Mountpoint': '/var/lib/docker/volumes/07c473532ed7024bda91889e2467bbb1709df29e62ae0359c26cb24e2ab5e227/_data'}

Create Volume

volume = cli.create_volume(name='volume1', driver='local', driver_opts={})
print(volume)
{'Name': 'volume1', 'Driver': 'local', 'Mountpoint': '/var/lib/docker/volumes/volume1/_data'}

Using Volumes

This example shows how to use a Volume to create a container

container_id = cli.create_container(
    'busybox', 'ls', volumes=['/var/lib/docker/volumes/volume1'],
    host_config=cli.create_host_config(binds=[
        '/var/lib/docker/volumes/volume1:/mnt/vol1',
    ])
)
print(container_id)
{'Id': '31a297f9bb8c6c08a1ebd22469177f290b22a298fce103689dfc92fa88dba070', 'Warnings': None}

Inspect Volumes

cli.inspect_volume('volume1')
{'Driver': 'local',
 'Mountpoint': '/var/lib/docker/volumes/volume1/_data',
 'Name': 'volume1'}

Get Archive

ctnr = cli.create_container('ubuntu', 'true')
strm, stat = cli.get_archive(ctnr, '/bin/sh')
print(stat)
{'name': 'sh', 'linkTarget': '/bin/dash', 'size': 4, 'mode': 134218239, 'mtime': '2014-02-19T04:13:52-08:00'}