Top Related Projects
The Moby Project - a collaborative project for the container ecosystem to assemble container-based systems
INACTIVE: A simple docker client for the JVM
Define and run multi-container applications with Docker
Production-Grade Container Scheduling and Management
An open and reliable container runtime
CLI tool for spawning and running containers according to the OCI specification
Quick Overview
Docker-py is the official Python SDK for Docker. It provides a Python interface to interact with Docker, allowing developers to manage containers, images, networks, and other Docker resources programmatically. This library is essential for automating Docker-related tasks and integrating Docker functionality into Python applications.
Pros
- Official Docker SDK, ensuring compatibility and up-to-date features
- Comprehensive coverage of Docker API functionalities
- Well-documented with extensive examples and API references
- Active development and community support
Cons
- Learning curve for users new to Docker concepts
- Performance overhead compared to direct Docker CLI usage for simple tasks
- Potential version compatibility issues between docker-py and Docker engine
- Limited support for some advanced Docker features compared to CLI
Code Examples
- Creating and running a container:
import docker
client = docker.from_env()
container = client.containers.run("ubuntu:latest", "echo hello world", remove=True)
print(container)
- Listing all running containers:
import docker
client = docker.from_env()
containers = client.containers.list()
for container in containers:
print(f"Container ID: {container.id}, Name: {container.name}")
- Building an image from a Dockerfile:
import docker
client = docker.from_env()
image, build_logs = client.images.build(path="./", tag="my-image:latest")
for log in build_logs:
print(log)
Getting Started
To get started with docker-py, follow these steps:
-
Install docker-py using pip:
pip install docker
-
Import the library and create a client:
import docker client = docker.from_env()
-
Use the client to interact with Docker:
# List all images images = client.images.list() for image in images: print(image.tags) # Run a container container = client.containers.run("ubuntu:latest", "echo hello world", remove=True) print(container)
For more detailed information and advanced usage, refer to the official documentation at https://docker-py.readthedocs.io/.
Competitor Comparisons
The Moby Project - a collaborative project for the container ecosystem to assemble container-based systems
Pros of Moby
- More comprehensive and feature-rich, serving as the core of Docker
- Supports a wider range of Docker functionalities and use cases
- Larger community and more frequent updates
Cons of Moby
- Steeper learning curve due to its complexity
- Heavier and requires more resources to run
- May be overkill for simple Docker operations or integrations
Code Comparison
Moby (Go):
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
Docker-py (Python):
import docker
client = docker.from_env()
Key Differences
- Docker-py is a Python library for interacting with Docker, while Moby is the core component of Docker itself
- Docker-py is more suitable for Python developers and simpler integrations
- Moby provides lower-level access to Docker internals and is used for building Docker itself
Use Cases
- Choose Docker-py for Python-based projects or when simplicity is preferred
- Opt for Moby when building Docker-related tools or requiring deep integration with Docker internals
Community and Support
- Moby has a larger community and more frequent updates
- Docker-py is well-maintained but has a smaller, more focused community
Performance
- Moby is generally faster and more efficient for complex Docker operations
- Docker-py may have slight overhead due to Python, but is sufficient for most use cases
INACTIVE: A simple docker client for the JVM
Pros of docker-client
- Written in Java, providing better integration with Java-based projects
- Supports both synchronous and asynchronous operations
- More comprehensive documentation and examples
Cons of docker-client
- Less actively maintained compared to docker-py
- May have slower performance for Python-based applications
- Limited support for newer Docker features
Code Comparison
docker-py:
import docker
client = docker.from_env()
container = client.containers.run("ubuntu", "echo hello world")
print(container.logs())
docker-client:
DockerClient client = DefaultDockerClient.fromEnv().build();
ContainerCreation container = client.createContainer(ContainerConfig.builder()
.image("ubuntu").cmd("echo", "hello world").build());
client.startContainer(container.id());
Summary
docker-py is the official Python SDK for Docker, offering better maintenance and up-to-date feature support. It's ideal for Python projects and has a more Pythonic API. docker-client, on the other hand, is better suited for Java applications and provides both synchronous and asynchronous operations. However, it may lack support for newer Docker features and has less active maintenance. The choice between the two largely depends on the programming language and specific requirements of your project.
Define and run multi-container applications with Docker
Pros of Compose
- Higher-level abstraction for managing multi-container applications
- Declarative YAML configuration for defining and running complex Docker environments
- Built-in support for environment variables and scaling services
Cons of Compose
- Limited to Docker-specific deployments, less flexible for other container runtimes
- Steeper learning curve for users new to container orchestration
- May introduce overhead for simple single-container applications
Code Comparison
Compose (docker-compose.yml):
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
docker-py:
import docker
client = docker.from_env()
container = client.containers.run("nginx", ports={'80/tcp': 80})
Key Differences
- Compose uses a declarative YAML format, while docker-py is a Python library for programmatic Docker interactions
- Compose is designed for multi-container applications, docker-py is more flexible for single-container operations
- Compose provides built-in orchestration features, while docker-py requires custom implementation for complex scenarios
Use Cases
- Compose: Development environments, CI/CD pipelines, and simple production deployments
- docker-py: Custom Docker management tools, automation scripts, and integration with Python applications
Production-Grade Container Scheduling and Management
Pros of Kubernetes
- More comprehensive container orchestration platform
- Supports advanced scaling and load balancing features
- Larger ecosystem with extensive tooling and integrations
Cons of Kubernetes
- Steeper learning curve and more complex setup
- Potentially overkill for smaller projects or simpler deployments
- Requires more resources to run effectively
Code Comparison
Kubernetes manifest example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Docker-py example:
import docker
client = docker.from_env()
container = client.containers.run("nginx:1.14.2", detach=True, ports={'80/tcp': 8080})
The Kubernetes manifest defines a more complex deployment with multiple replicas, while the Docker-py code creates a single container with simpler configuration. Kubernetes offers more advanced features for orchestration, while Docker-py provides a straightforward Python API for Docker operations.
An open and reliable container runtime
Pros of containerd
- Lower-level container runtime with broader ecosystem support
- Designed for integration into larger systems like Kubernetes
- More lightweight and focused on core container operations
Cons of containerd
- Steeper learning curve for developers new to container technologies
- Less user-friendly for simple container management tasks
- Requires additional tools for higher-level operations
Code comparison
containerd (Go):
client, err := containerd.New("/run/containerd/containerd.sock")
container, err := client.NewContainer(ctx, "redis", spec)
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
docker-py (Python):
client = docker.from_env()
container = client.containers.run("redis", detach=True)
logs = container.logs()
Summary
containerd is a lower-level container runtime offering more control and integration capabilities, while docker-py provides a higher-level Python API for Docker operations. containerd is better suited for complex container orchestration systems, while docker-py is more user-friendly for simple container management tasks in Python applications.
CLI tool for spawning and running containers according to the OCI specification
Pros of runc
- Lower-level container runtime, providing more direct control over container execution
- Lightweight and focused on container runtime functionality
- Implements OCI (Open Container Initiative) standards, ensuring compatibility across container ecosystems
Cons of runc
- Requires more technical expertise to use effectively
- Limited to container runtime operations, lacking higher-level management features
- Less extensive documentation and community support compared to docker-py
Code Comparison
runc (Go):
spec, err := loadSpec(context.Background(), specConfig)
if err != nil {
return err
}
status, err := startContainer(context.Background(), spec, opts)
docker-py (Python):
client = docker.from_env()
container = client.containers.run("ubuntu:latest", "echo hello world", detach=True)
logs = container.logs()
Key Differences
- runc is a low-level container runtime, while docker-py is a high-level Docker API client
- runc is written in Go, docker-py in Python
- docker-py provides a more user-friendly interface for Docker operations
- runc offers finer-grained control over container execution
- docker-py integrates with the broader Docker ecosystem, while runc focuses on core container runtime functionality
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Docker SDK for Python
A Python library for the Docker Engine API. It lets you do anything the docker
command does, but from within Python apps â run containers, manage containers, manage Swarms, etc.
Installation
The latest stable version is available on PyPI. Install with pip:
pip install docker
Older versions (< 6.0) required installing
docker[tls]
for SSL/TLS support. This is no longer necessary and is a no-op, but is supported for backwards compatibility.
Usage
Connect to Docker using the default socket or the configuration in your environment:
import docker
client = docker.from_env()
You can run containers:
>>> client.containers.run("ubuntu:latest", "echo hello world")
'hello world\n'
You can run containers in the background:
>>> client.containers.run("bfirsh/reticulate-splines", detach=True)
<Container '45e6d2de7c54'>
You can manage containers:
>>> client.containers.list()
[<Container '45e6d2de7c54'>, <Container 'db18e4f20eaa'>, ...]
>>> container = client.containers.get('45e6d2de7c54')
>>> container.attrs['Config']['Image']
"bfirsh/reticulate-splines"
>>> container.logs()
"Reticulating spline 1...\n"
>>> container.stop()
You can stream logs:
>>> for line in container.logs(stream=True):
... print(line.strip())
Reticulating spline 2...
Reticulating spline 3...
...
You can manage images:
>>> client.images.pull('nginx')
<Image 'nginx'>
>>> client.images.list()
[<Image 'ubuntu'>, <Image 'nginx'>, ...]
Read the full documentation to see everything you can do.
Top Related Projects
The Moby Project - a collaborative project for the container ecosystem to assemble container-based systems
INACTIVE: A simple docker client for the JVM
Define and run multi-container applications with Docker
Production-Grade Container Scheduling and Management
An open and reliable container runtime
CLI tool for spawning and running containers according to the OCI specification
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot