docker-py
A Python library for the Docker Engine API
The official Python library from Docker that lets you start, stop, and manage Docker containers and images entirely from Python code, replacing the need to run docker CLI commands in a shell.
Docker SDK for Python is an official Python library that lets you control Docker from within a Python program. Docker is a tool for running applications inside containers, which are isolated, self-contained environments that include everything a program needs to run. Normally you interact with Docker through a command-line tool, but this library exposes the same capabilities through Python code, so you can automate Docker operations inside scripts or applications.
With this library you can start and stop containers, list running containers, retrieve logs, pull container images from a registry, manage Docker Swarm clusters, and do most other things the docker command-line tool supports. You connect to the local Docker installation with a single call using the default socket, or configure it to connect to a remote Docker host.
Installation is through pip, the standard Python package manager, with the package name docker. The README is short and focuses on quick examples: running a container and getting its output back as a string, starting a container in the background and retrieving a reference to it, listing running containers, fetching logs, stopping a container, and pulling or listing images. The full API documentation is hosted separately on Read the Docs.
This is the official SDK maintained by Docker itself, so it tracks the Docker Engine API closely. It works with Docker containers, images, networks, volumes, and Swarm services.
Where it fits
- Automate spinning up and tearing down Docker containers from a Python test suite or CI pipeline script
- Pull images, start containers, and retrieve logs programmatically inside a Python application
- Manage Docker Swarm clusters and services from Python without shelling out to the docker CLI