0
# Context Management
1
2
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.
3
4
## Capabilities
5
6
### Context Creation
7
8
Create new contexts with Docker and Kubernetes endpoint configurations.
9
10
```python { .api }
11
def create(
12
context_name: str,
13
*,
14
description: Optional[str] = None,
15
docker: Optional[DockerContextConfig] = None,
16
kubernetes: Optional[KubernetesContextConfig] = None,
17
default_stack_orchestrator: Optional[str] = None
18
) -> Context:
19
"""
20
Create a new Docker context.
21
22
Parameters:
23
- context_name: Name for the new context
24
- description: Optional description
25
- docker: Docker endpoint configuration
26
- kubernetes: Kubernetes endpoint configuration
27
- default_stack_orchestrator: Default orchestrator (swarm/kubernetes)
28
29
Returns:
30
Context object
31
"""
32
```
33
34
**Usage Examples:**
35
36
```python
37
from python_on_whales import docker, DockerContextConfig
38
39
# Create context for remote Docker daemon
40
remote_config = DockerContextConfig(
41
host="tcp://remote-docker:2376",
42
ca_cert="./certs/ca.pem",
43
cert="./certs/cert.pem",
44
key="./certs/key.pem"
45
)
46
47
context = docker.context.create(
48
"remote-prod",
49
description="Production Docker daemon",
50
docker=remote_config
51
)
52
53
# Create context for local daemon with custom socket
54
local_config = DockerContextConfig(host="unix:///var/run/docker.sock")
55
context = docker.context.create("local-custom", docker=local_config)
56
```
57
58
### Context Inspection
59
60
Inspect context details including endpoints and metadata.
61
62
```python { .api }
63
def inspect(x: Optional[str] = None) -> Context:
64
"""
65
Inspect a context (current context if none specified).
66
67
Parameters:
68
- x: Context name (defaults to current context)
69
70
Returns:
71
Context object with detailed information
72
"""
73
```
74
75
### Context Listing
76
77
List all available contexts.
78
79
```python { .api }
80
def list() -> List[Context]:
81
"""
82
List all available contexts.
83
84
Returns:
85
List of Context objects
86
"""
87
```
88
89
### Context Switching
90
91
Set the default context for Docker operations.
92
93
```python { .api }
94
def use(context: Union[str, Context]) -> None:
95
"""
96
Set the default context.
97
98
Parameters:
99
- context: Context name or Context object to use as default
100
"""
101
```
102
103
### Context Removal
104
105
Remove contexts from the system.
106
107
```python { .api }
108
def remove(
109
x: Union[str, List[str]],
110
force: bool = False
111
) -> None:
112
"""
113
Remove one or more contexts.
114
115
Parameters:
116
- x: Context name(s) to remove
117
- force: Force removal even if context is in use
118
"""
119
```
120
121
## Types
122
123
```python { .api }
124
class Context:
125
name: str
126
metadata: Dict[str, Any]
127
endpoints: Dict[str, Any]
128
tls_material: Dict[str, Any]
129
storage: Dict[str, Any]
130
131
def remove(self, force: bool = False) -> None:
132
"""Remove this context."""
133
134
def update(self) -> Context:
135
"""Update context configuration (not implemented)."""
136
137
def use(self) -> None:
138
"""Set this context as default."""
139
140
class DockerContextConfig:
141
def __init__(
142
self,
143
host: str,
144
ca_cert: Optional[str] = None,
145
cert: Optional[str] = None,
146
key: Optional[str] = None,
147
skip_tls_verify: bool = False
148
): ...
149
150
class KubernetesContextConfig:
151
def __init__(
152
self,
153
config_file: Optional[str] = None,
154
context_override: Optional[str] = None,
155
namespace_override: Optional[str] = None
156
): ...
157
```