Python Library API for Docker

DockerOperating SystemOpen SourcePython

You can access, manage and manipulate docker objects such as containers, images, clusters, swarms, etc. using a python library API. You can do pretty much anything that docker commands let you do. This comes very handy when you are using a python app such as django or flask and you want to maintain your docker container using the same python script that you use for the application.

To use the python library API for docker, you need to install a package called docker−py. You can do so using the following pip command. If you have python 2 installed, replace pip3 with pip.

pip3 install docker−py

Now, we will look at different features of the python client library API for docker one by one.

In order to run docker commands inside the python script using the API, you first need to connect to the docker daemon. You can do so using the following commands −

#import client
from docker import client

#create a client object to connect to the daemon
myClient = client.Client(base_url='unix://var/run/docker.sock')

After you have connected to the docker daemon, you can get a list of all the containers using the following command. Please note that you are logged in as the root user before running any of the commands in order to avoid permission errors.

myClient.containers()

It will give you a list of all the containers that exist in your local machine along with their Id, associated image and image Id, labels, ports, status, etc.

To create a new container, you can use the create_container method from the client object.

myContainer=myClient.create_container(image='ubuntu:latest',command='/bin/bash')

Using the above commands, you can create a container from the ubuntu image and provide the command to open the bash or any other command as you want.

You can check that the container has been created by printing the container’s Id using the following command −

print(myContainer['Id'])

To inspect a particular container, you can use the inspect_container method on the client object.

myClient.inspect_container('a74688e3cf61ac11fd19bcbca1003465b03b117537adfc826db52c6430c46ba5')

You need to provide the container Id of the container you want to inspect inside as the argument. You can also inspect only specific fields such as the path or the name or the date of creation.

myClient.inspect_container('a74688e3cf61ac11fd19bcbca1003465b03b117537adfc826db52c6430c46ba5')['Name']

myClient.inspect_container('a74688e3cf61ac11fd19bcbca1003465b03b117537adfc826db52c6430c46ba5')['Created']

myClient.inspect_container('a74688e3cf61ac11fd19bcbca1003465b03b117537adfc826db52c6430c46ba5')['Path']

To commit a container, you can use the commit method on the container object. You can also provide a tag to the container.

myClient.commit('a74688e3cf61ac11fd19bcbca1003465b03b117537adfc826db52c6430c46ba5', tag='container1')
The above command will return the Id of the container. In order to restart a container, you need to make sure the container still exists. To avoid this, what we can do is to wrap the command inside a try−catch block.
try:
myClient.restart('a74688e3cf61ac11fd19bcbca1003465b03b117537adfc826db52c6430c46ba5')
except Exception as e:
print(e)

To get a list of all the images, you can use the images method on the client object.

images = myClient.images()

It will return a list of all the images. To print the details of the first image, use −

print(images[0])

To inspect the image −

myClient.inspect_image('9140108b62dc87d9b278bb0d4fd6a3e44c2959646eb966b86531306faa81b09b')

You need to provide the image Id as the argument.

We will now see some useful commands to work with volumes. To get a list of all the volumes, you can use the volumes method on the client object.

volumes = myClient.volumes()

It will return a list of all the volumes. To print the details of the first volume, you can use −

print(volumes['Volumes'][0])

In order to create a volume, you need to specify the volume name, driver name and optionally, you can also specify other options as well.

volume=myClient.create_volume(name='myVolume1', driver='local', driver_opts={})

To check whether the volume has been created or not, try printing it.

print(volume)

To inspect a volume, use the inspect_volume method on the client object.

myClient.inspect_volume('myVolume1')

To create a container with a volume mounted on it, you can use the following example −

mounted_container = myClient.create_container(
   'ubuntu', 'ls', volumes=['/var/lib/docker/volumes/myVolume1'],
   host_config=myClient.create_host_config(binds=[
  '/var/lib/docker/volumes/myVolume1:/usr/src/app/myVolume1'
  , ])
)

The above command creates a container from ubuntu image and specifies the entrypoint as ls and mounts a volume located at /var/lib/docker/volumes/myVolume1 in you local machine to /usr/src/app/myVolume1 in your docker container.

To conclude, in this article, we have discussed how to create, inspect and manage docker objects such as docker containers, images, volumes using a python script. This is very useful when you are building an application using python tools such as a web application using django or flask or a GUI application using tkinter or using any other python script. If you are willing to manage the application from a docker container, it is strongly advised to use python scripts to write docker commands instead of executing commands separately through the command line interface.

raja
Published on 27-Oct-2020 12:26:33
Advertisements