A Docker client for Python, designed to be fun and intuitive!
—
Docker context management for switching between different Docker environments (local, remote, Kubernetes). Contexts store connection information, certificates, and configuration for different Docker daemons and orchestrators.
Create new contexts with Docker and Kubernetes endpoint configurations.
def create(
context_name: str,
*,
description: Optional[str] = None,
docker: Optional[DockerContextConfig] = None,
kubernetes: Optional[KubernetesContextConfig] = None,
default_stack_orchestrator: Optional[str] = None
) -> Context:
"""
Create a new Docker context.
Parameters:
- context_name: Name for the new context
- description: Optional description
- docker: Docker endpoint configuration
- kubernetes: Kubernetes endpoint configuration
- default_stack_orchestrator: Default orchestrator (swarm/kubernetes)
Returns:
Context object
"""Usage Examples:
from python_on_whales import docker, DockerContextConfig
# Create context for remote Docker daemon
remote_config = DockerContextConfig(
host="tcp://remote-docker:2376",
ca_cert="./certs/ca.pem",
cert="./certs/cert.pem",
key="./certs/key.pem"
)
context = docker.context.create(
"remote-prod",
description="Production Docker daemon",
docker=remote_config
)
# Create context for local daemon with custom socket
local_config = DockerContextConfig(host="unix:///var/run/docker.sock")
context = docker.context.create("local-custom", docker=local_config)Inspect context details including endpoints and metadata.
def inspect(x: Optional[str] = None) -> Context:
"""
Inspect a context (current context if none specified).
Parameters:
- x: Context name (defaults to current context)
Returns:
Context object with detailed information
"""List all available contexts.
def list() -> List[Context]:
"""
List all available contexts.
Returns:
List of Context objects
"""Set the default context for Docker operations.
def use(context: Union[str, Context]) -> None:
"""
Set the default context.
Parameters:
- context: Context name or Context object to use as default
"""Remove contexts from the system.
def remove(
x: Union[str, List[str]],
force: bool = False
) -> None:
"""
Remove one or more contexts.
Parameters:
- x: Context name(s) to remove
- force: Force removal even if context is in use
"""class Context:
name: str
metadata: Dict[str, Any]
endpoints: Dict[str, Any]
tls_material: Dict[str, Any]
storage: Dict[str, Any]
def remove(self, force: bool = False) -> None:
"""Remove this context."""
def update(self) -> Context:
"""Update context configuration (not implemented)."""
def use(self) -> None:
"""Set this context as default."""
class DockerContextConfig:
def __init__(
self,
host: str,
ca_cert: Optional[str] = None,
cert: Optional[str] = None,
key: Optional[str] = None,
skip_tls_verify: bool = False
): ...
class KubernetesContextConfig:
def __init__(
self,
config_file: Optional[str] = None,
context_override: Optional[str] = None,
namespace_override: Optional[str] = None
): ...Install with Tessl CLI
npx tessl i tessl/pypi-python-on-whales