Docker SDK for Python

7.1.0 · active · verified Wed Mar 25

Official Python client for Docker Engine API. Current version: 7.1.0 (Mar 2026). Install is 'docker' (not 'docker-py' — that's the old abandoned package). Two client patterns: docker.from_env() uses environment variables, DockerClient() for explicit config. containers.run() blocks by default — use detach=True for async. On macOS with Docker Desktop, Colima, or Podman the socket path may differ from default causing DockerException on from_env().

Warnings

Install

Imports

Quickstart

Docker SDK for Python — run, manage and inspect containers.

# pip install docker
import docker

client = docker.from_env()

# Pull image
client.images.pull('alpine')

# Run and get output (blocking)
output = client.containers.run('alpine', 'echo hello world')
print(output)  # b'hello world\n'

# Run detached service
container = client.containers.run(
    'nginx',
    detach=True,
    ports={'80/tcp': 8080},
    name='my-nginx'
)
print(f'Started: {container.name} ({container.short_id})')

# Inspect
container.reload()
print(container.status)  # 'running'

# Stop and remove
container.stop()
container.remove()

# List all images
for image in client.images.list():
    print(image.tags)

view raw JSON →