Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Library API for Docker
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.
Installation
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
Connecting to Docker Daemon
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')
Container Management
Listing Containers
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.
Creating Containers
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'])
Inspecting Containers
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']
Container Operations
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)
Image Management
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.
Volume Management
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')
Mounting Volumes
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.
Conclusion
The Python Docker API provides comprehensive control over Docker objects including containers, images, and volumes through programmatic access. This integration is particularly valuable for Python applications like Django or Flask where Docker management needs to be seamlessly integrated into the application workflow, eliminating the need for separate CLI commands.
