explaingit

docker/docker-py

7,174PythonAudience · developerComplexity · 2/5Setup · moderate

TLDR

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.

Mindmap

mindmap
  root((docker-py))
    What it does
      Control Docker via Python
      Automate containers
    Operations
      Start and stop containers
      Pull images
      Fetch logs
      Manage Swarm
    Tech stack
      Python
      Docker Engine API
    Audience
      Python developers
      DevOps engineers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Automate spinning up and tearing down Docker containers from a Python test suite or CI pipeline script

USE CASE 2

Pull images, start containers, and retrieve logs programmatically inside a Python application

USE CASE 3

Manage Docker Swarm clusters and services from Python without shelling out to the docker CLI

Tech stack

PythonDocker

Getting it running

Difficulty · moderate Time to first run · 30min

Requires Docker Engine installed and running locally or a reachable remote Docker host.

The explanation does not mention the license type.

In plain English

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.

Copy-paste prompts

Prompt 1
Using the Docker Python SDK, write a script that pulls the latest nginx image, starts a container on port 8080, waits 5 seconds, then prints the container logs.
Prompt 2
How do I use docker-py to list all currently running containers and stop any container whose name matches a given prefix?
Prompt 3
Write a Python function using docker-py that runs a shell command inside a new temporary container and returns the output as a string.
Prompt 4
Using the Docker Python SDK, how do I connect to a remote Docker host over TCP instead of the local Unix socket?
Open on GitHub → Explain another repo

← docker on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.