A Python library for the Docker Engine API.
—
Docker configuration and secret management for secure credential handling and application configuration with support for external secret stores and configuration templating.
class ConfigCollection:
def create(self, **kwargs):
"""
Create a configuration.
Args:
**kwargs: Config options
Returns:
Config: Created config instance
Common kwargs:
name (str): Config name
data (bytes): Config data
labels (dict): Config labels
templating (dict): Template configuration
"""
def get(self, config_id):
"""
Get a config by ID or name.
Args:
config_id (str): Config ID or name
Returns:
Config: Config instance
"""
def list(self, filters=None):
"""
List configs.
Args:
filters (dict): Filter results
Returns:
list[Config]: List of config instances
"""class SecretCollection:
def create(self, **kwargs):
"""
Create a secret.
Args:
**kwargs: Secret options
Returns:
Secret: Created secret instance
Common kwargs:
name (str): Secret name
data (bytes): Secret data
labels (dict): Secret labels
driver (dict): Secret driver configuration
"""
def get(self, secret_id):
"""
Get a secret by ID or name.
Args:
secret_id (str): Secret ID or name
Returns:
Secret: Secret instance
"""
def list(self, filters=None):
"""
List secrets.
Args:
filters (dict): Filter results
Returns:
list[Secret]: List of secret instances
"""class Config:
"""
A Docker config instance.
Properties:
id (str): Config ID
name (str): Config name
attrs (dict): Raw config attributes
"""
def remove(self):
"""Remove the config."""
class Secret:
"""
A Docker secret instance.
Properties:
id (str): Secret ID
name (str): Secret name
attrs (dict): Raw secret attributes
"""
def remove(self):
"""Remove the secret."""import docker
client = docker.from_env()
# Create config
config_data = b'server { listen 80; location / { proxy_pass http://app:3000; } }'
config = client.configs.create(
name='nginx-config',
data=config_data,
labels={'app': 'web-proxy'}
)
# Create secret
secret_data = b'my-secret-password'
secret = client.secrets.create(
name='db-password',
data=secret_data
)
# Use in service
service = client.services.create(
image='nginx:latest',
name='web-proxy',
configs=[{
'ConfigID': config.id,
'ConfigName': 'nginx-config',
'File': {'Name': '/etc/nginx/nginx.conf', 'Mode': 0o644}
}],
secrets=[{
'SecretID': secret.id,
'SecretName': 'db-password',
'File': {'Name': '/run/secrets/db_password', 'Mode': 0o600}
}]
)