0
# Configuration and Secrets
1
2
Docker configuration and secret management for secure credential handling and application configuration with support for external secret stores and configuration templating.
3
4
## Capabilities
5
6
### ConfigCollection
7
8
```python { .api }
9
class ConfigCollection:
10
def create(self, **kwargs):
11
"""
12
Create a configuration.
13
14
Args:
15
**kwargs: Config options
16
17
Returns:
18
Config: Created config instance
19
20
Common kwargs:
21
name (str): Config name
22
data (bytes): Config data
23
labels (dict): Config labels
24
templating (dict): Template configuration
25
"""
26
27
def get(self, config_id):
28
"""
29
Get a config by ID or name.
30
31
Args:
32
config_id (str): Config ID or name
33
34
Returns:
35
Config: Config instance
36
"""
37
38
def list(self, filters=None):
39
"""
40
List configs.
41
42
Args:
43
filters (dict): Filter results
44
45
Returns:
46
list[Config]: List of config instances
47
"""
48
```
49
50
### SecretCollection
51
52
```python { .api }
53
class SecretCollection:
54
def create(self, **kwargs):
55
"""
56
Create a secret.
57
58
Args:
59
**kwargs: Secret options
60
61
Returns:
62
Secret: Created secret instance
63
64
Common kwargs:
65
name (str): Secret name
66
data (bytes): Secret data
67
labels (dict): Secret labels
68
driver (dict): Secret driver configuration
69
"""
70
71
def get(self, secret_id):
72
"""
73
Get a secret by ID or name.
74
75
Args:
76
secret_id (str): Secret ID or name
77
78
Returns:
79
Secret: Secret instance
80
"""
81
82
def list(self, filters=None):
83
"""
84
List secrets.
85
86
Args:
87
filters (dict): Filter results
88
89
Returns:
90
list[Secret]: List of secret instances
91
"""
92
```
93
94
### Config and Secret Models
95
96
```python { .api }
97
class Config:
98
"""
99
A Docker config instance.
100
101
Properties:
102
id (str): Config ID
103
name (str): Config name
104
attrs (dict): Raw config attributes
105
"""
106
107
def remove(self):
108
"""Remove the config."""
109
110
class Secret:
111
"""
112
A Docker secret instance.
113
114
Properties:
115
id (str): Secret ID
116
name (str): Secret name
117
attrs (dict): Raw secret attributes
118
"""
119
120
def remove(self):
121
"""Remove the secret."""
122
```
123
124
## Usage Examples
125
126
```python
127
import docker
128
129
client = docker.from_env()
130
131
# Create config
132
config_data = b'server { listen 80; location / { proxy_pass http://app:3000; } }'
133
config = client.configs.create(
134
name='nginx-config',
135
data=config_data,
136
labels={'app': 'web-proxy'}
137
)
138
139
# Create secret
140
secret_data = b'my-secret-password'
141
secret = client.secrets.create(
142
name='db-password',
143
data=secret_data
144
)
145
146
# Use in service
147
service = client.services.create(
148
image='nginx:latest',
149
name='web-proxy',
150
configs=[{
151
'ConfigID': config.id,
152
'ConfigName': 'nginx-config',
153
'File': {'Name': '/etc/nginx/nginx.conf', 'Mode': 0o644}
154
}],
155
secrets=[{
156
'SecretID': secret.id,
157
'SecretName': 'db-password',
158
'File': {'Name': '/run/secrets/db_password', 'Mode': 0o600}
159
}]
160
)
161
```