Skip to main content

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

Metadata

Release files for dockercomposefile 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dockercomposefile 0.1.1
File Size Uploaded
dockercomposefile-0.1.1.tar.gz 22.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dockercomposefile 0.1.1
File Interpreter ABI Platform
dockercomposefile-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 44.1 kB

Release files / dockercomposefile-0.1.1.tar.gz

Download URL dockercomposefile-0.1.1.tar.gz
Size 22.0 kB
Tags Source
SHA-256 checksum
How to use checksums
dbb54528c286537ac9d8b3b8213df8d8990c85b1e8709ef54c02dc89683b38be
BLAKE2b-256 checksum
How to use checksums
2897c591b119725f1ddd2e02c7e7d316719950010e4f134cd4439681b6ca188a
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / dockercomposefile-0.1.1-py3-none-any.whl

Download URL dockercomposefile-0.1.1-py3-none-any.whl
Size 22.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b95729cf34279b839ef07f22a20e39a8a870977c30acb7751d4bf605da5dc00c
BLAKE2b-256 checksum
How to use checksums
6fdfa2c3f2b788ddecef915a2cce942940ab884d76e8509c07f92e5b6cfcfd5b
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page