Pydantic models for Docker Compose files with YAML builder and exporter
Project description
dockercomposefile
Pydantic2 models for Docker Compose files with YAML builder and exporter.
Installation
pip install dockercomposefile
Usage
Parse a Compose file
from dockercomposefile import ComposeBuilder
# From a YAML string
compose = ComposeBuilder.from_string("""
services:
web:
image: nginx:latest
ports:
- "80:80"
""")
# From a file
compose = ComposeBuilder.from_file("docker-compose.yml")
# From a dictionary
compose = ComposeBuilder.from_dict({"services": {"web": {"image": "nginx"}}})
Access model data
# Access services
web = compose.services["web"]
print(web.image) # nginx:latest
# Access ports (normalized to long form)
for port in web.ports:
print(f"{port.target} -> {port.published}")
# Access networks
for name, network in compose.networks.items():
print(f"Network: {name}, driver: {network.driver}")
Export to YAML
from dockercomposefile import ComposeExporter
# To a string
yaml_str = ComposeExporter.to_string(compose)
# To a file
ComposeExporter.to_file(compose, "docker-compose.yml")
# To a dictionary
data = ComposeExporter.to_dict(compose)
Create programmatically
from dockercomposefile.models import DockerComposeFile, Service
compose = DockerComposeFile(
services={
"web": Service(
image="nginx:latest",
ports=[{"target": 80, "published": "8080"}],
),
},
)
Supported Compose Specification
The library models the full Docker Compose specification, including:
- Services: image, build, command, entrypoint, environment, ports, volumes, networks, depends_on, healthcheck, logging, deploy, develop, and more
- Networks: driver, driver_opts, ipam, external, labels
- Volumes: driver, driver_opts, external, labels
- Configs/Secrets: file, environment, content, external
- Models (AI): model, context_size, runtime_flags
- Build: context, dockerfile, args, cache_from, secrets, ssh, platforms
- Deploy: replicas, resources, placement, restart_policy, update_config
- Develop: watch rules for file syncing and rebuilds
Short syntax (e.g., ports: ["80:80"]) is automatically normalized to long
form (structured objects) on parse. On export, the long form is emitted.
x-* extension fields are preserved on both parse and export.
Development
# Install with dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run tests with coverage
pytest --cov=dockercomposefile --cov-report=term-missing
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dockercomposefile-0.1.1.tar.gz.
File metadata
- Download URL: dockercomposefile-0.1.1.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbb54528c286537ac9d8b3b8213df8d8990c85b1e8709ef54c02dc89683b38be
|
|
| MD5 |
cd937451e93e0df698321703dcf6a897
|
|
| BLAKE2b-256 |
2897c591b119725f1ddd2e02c7e7d316719950010e4f134cd4439681b6ca188a
|
File details
Details for the file dockercomposefile-0.1.1-py3-none-any.whl.
File metadata
- Download URL: dockercomposefile-0.1.1-py3-none-any.whl
- Upload date:
- Size: 22.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b95729cf34279b839ef07f22a20e39a8a870977c30acb7751d4bf605da5dc00c
|
|
| MD5 |
e8544cefeeb42d22ec203b0aa8bd9679
|
|
| BLAKE2b-256 |
6fdfa2c3f2b788ddecef915a2cce942940ab884d76e8509c07f92e5b6cfcfd5b
|