0
# Secret Management
1
2
Docker swarm secret management for securely storing sensitive data like passwords, certificates, and API keys. Secrets are encrypted at rest and in transit, and are only accessible to services that explicitly request them.
3
4
## Capabilities
5
6
### Secret Creation
7
8
Create secrets from files or data with labeling and template support.
9
10
```python { .api }
11
def create(
12
name: str,
13
file: Optional[str] = None,
14
driver: Optional[str] = None,
15
labels: Optional[Dict[str, str]] = None,
16
template_driver: Optional[str] = None
17
) -> Secret:
18
"""
19
Create a secret object.
20
21
Parameters:
22
- name: Secret name
23
- file: Path to file containing secret data
24
- driver: Secret driver to use
25
- labels: Metadata labels for the secret
26
- template_driver: Template driver for processing
27
28
Returns:
29
Secret object
30
"""
31
```
32
33
### Secret Inspection
34
35
Inspect secret details including metadata and creation time.
36
37
```python { .api }
38
def inspect(x: Union[str, List[str]]) -> Union[Secret, List[Secret]]:
39
"""
40
Inspect one or more secrets.
41
42
Parameters:
43
- x: Secret name(s) or ID(s)
44
45
Returns:
46
Secret object(s) with detailed information
47
"""
48
```
49
50
### Secret Listing
51
52
List all secrets with optional filtering.
53
54
```python { .api }
55
def list(filters: Sequence[str] = ()) -> List[Secret]:
56
"""
57
List all secrets with optional filters.
58
59
Parameters:
60
- filters: Filter conditions
61
62
Returns:
63
List of Secret objects
64
"""
65
```
66
67
### Secret Removal
68
69
Remove secrets from the swarm.
70
71
```python { .api }
72
def remove(x: Union[str, List[str]]) -> None:
73
"""
74
Remove one or more secrets.
75
76
Parameters:
77
- x: Secret name(s) or ID(s)
78
"""
79
```
80
81
**Usage Examples:**
82
83
```python
84
from python_on_whales import docker
85
86
# Create secret from file
87
secret = docker.secret.create(
88
"db-password",
89
file="./secrets/db_password.txt",
90
labels={"environment": "production", "type": "password"}
91
)
92
93
# Create secret from string data
94
import tempfile
95
with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
96
f.write("my-secret-api-key")
97
temp_file = f.name
98
99
api_secret = docker.secret.create("api-key", file=temp_file)
100
101
# List all secrets
102
secrets = docker.secret.list()
103
for secret in secrets:
104
print(f"Secret: {secret.spec.name} - Created: {secret.created_at}")
105
106
# Remove secret
107
docker.secret.remove("old-secret")
108
```
109
110
## Types
111
112
```python { .api }
113
class Secret:
114
id: str
115
created_at: datetime
116
updated_at: datetime
117
spec: SecretSpec
118
119
def remove(self) -> None:
120
"""Remove this secret."""
121
122
class SecretSpec:
123
name: str
124
labels: Dict[str, str]
125
driver: Optional[SecretDriver]
126
template_driver: Optional[SecretDriver]
127
128
class SecretDriver:
129
name: str
130
options: Dict[str, str]
131
```