Docker SDK for Python
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
- breaking 'docker-py' is the old abandoned package (last release 2016). Install 'docker' instead. Both install as 'import docker' which causes confusion. Having both installed simultaneously causes conflicts.
- breaking docker.Client() removed. LLMs and old tutorials use Client(base_url=...) which raises AttributeError.
- gotcha from_env() raises DockerException on macOS with Docker Desktop if the socket symlink is missing. Error: 'Error while fetching server API version: FileNotFoundError /var/run/docker.sock'
- gotcha from_env() fails with Colima or Podman because they use different socket paths. Error: 'port is required' or 'FileNotFoundError'.
- gotcha containers.run() without detach=True blocks until container exits. Long-running containers will hang the process indefinitely.
- gotcha containers.run() with detach=True returns a Container object. containers.run() without detach=True returns bytes (stdout). Different return types for same method.
- gotcha Streams from container.logs(stream=True) or containers.run(stream=True) return a generator of bytes chunks — not a single bytes object.
Install
-
pip install docker -
pip install 'docker[ssh]'
Imports
- from_env / DockerClient
import docker # Recommended — reads DOCKER_HOST, DOCKER_TLS_VERIFY, DOCKER_CERT_PATH client = docker.from_env() # Or explicit: client = docker.DockerClient(base_url='unix://var/run/docker.sock') # Run container (blocks until exit) output = client.containers.run('ubuntu', 'echo hello') print(output) # b'hello\n' # Run detached (non-blocking) container = client.containers.run('nginx', detach=True) print(container.id) # List running containers for c in client.containers.list(): print(c.name, c.status) - containers.run (detach)
import docker client = docker.from_env() # Non-blocking — returns Container object container = client.containers.run( 'python:3.11', 'python -c "print(42)"', detach=True, remove=True # auto-remove on exit ) # Wait for completion and get logs container.wait() logs = container.logs() print(logs)
Quickstart
# 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)