Python program to control Docker using the API

April 3, 2024

Lang: cs en de es

Need to control Docker? The API, which is available for example via a Unix socket, is a great way to do this. As a programming language you can use for example Python, which is a scripting programming language, so you can quickly develop the functionality you need. In addition, there is a library for Python that implements the Docker API, so you can save yourself some work. How to do it? It's simple....I'll describe it in this tutorial.

Not only can you control Docker from the console or with some orchestration tool, but there is also an API. The Application Programming Interface) is a way (interface) to use your program to control another application.

Why use the Docker API?

Using the Docker API is useful in situations such as:

  1. Automation and integration: Integrating Docker APIs into existing tools or workflows, allows for process automation, which will increase work efficiency. This can be particularly useful in DevOps environments where you want to perform rapid continuous application development (CI/CD).
  2. Custom tools and interfaces: Using the Docker API allows you to create custom user interfaces or tools that better suit your organization's specific needs or processes.
  3. Container Orchestration and Management: Docker APIs allow you to programmatically control the starting, stopping, and managing of containers. This is essential for managing large numbers of containers. Tools that handle such a task are called orchestrators. These include Kubernetes, Docker Swarm, and others.
  4. Monitoring and resource management: Using the Docker API, you can monitor container health, collect metrics and resource usage information, which can be useful for performance management and application scaling.
  5. Integration with cloud services and other technologies: The Docker API enables integration with various cloud services and other technologies, which can improve flexibility and deployment options for applications.

Practical use

Imagine that you have an application that you need to run to execute batch jobs, and each run has to have different parameters or different input data. At the same time, for performance or rather load monitoring, you need to manage the execution of these services. In this situation, you can write your own program to collect the necessary data and run docker containers with the required parameters as needed.

Python program

Here is a basic code sample of how to use python and Docker API. This application connects to the Docker API and lists all running containers:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import docker

client = docker.from_env()

c=client.containers.list()
print(c)
for i in c:
    print(i)
    print(i.name)

Source code

The full sample, which can be run in a docker container, is here: https://github.com/josefjebavy/python-docker-example

Build image with sample application:

docker build -t python-docker-app .

Manually launch the application from the console:
docker run -it --rm -v $PWD:/usr/src/app -v /var/run/docker.sock:/var/run/docker.sock --name python-docker-app-run python-docker-app /bin/bash

The easiest way to run the project is with docker-compose, which will also do the initial build of the docker image with the application:
docker-compose up

The most key is to allow the application to access the socket docker daemon, so that the entire docker can be controlled.

Documentation

Python library documentation for controlling docker using the API: https://docker-py.readthedocs.io/en/stable/

Video preview

A complete video demonstration of how to prepare and run a Python application that will list all running docker containers. And other ways to use the Docker API:

Články na podobné téma

How to use MailCatcher to test emails
Python OpenAI API
Creating a WebSocket web application and setting up a proxy
Project management: agile software development
How to run old PHP applications
What a good programmer should know
Rust programming language
NodeJS: development, server configuration
Nette security bug CVE-2020-15227
REST API: platform API
Custom web and mail hosting with ISP Config software
Programming in SQL: PostgreSQL, MySQL/MariaDB
HTTPS: secure web
NoSQL database Mongo DB
Connecting to Microsoft SQL Server from Linux
What is the job description of a programmer
Python application localization
Which mail and web hosting to choose
Digispark - Program Atmel ATtiny microcontroller with Arduino IDE
Development for ARM processors with Arduino IDE
How to program the ESP8266 WiFi processor
Open smartphone with Linux - Openmoko Neo FreeRunner

Newsletter

If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.


+