Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dockercomposefile-0.1.0.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dockercomposefile-0.1.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file dockercomposefile-0.1.0.tar.gz.

File metadata

  • Download URL: dockercomposefile-0.1.0.tar.gz
  • Upload date:
  • Size: 21.6 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

Hashes for dockercomposefile-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a084e15cedcea51a52285fbf3740a2ba28380545ca0f280a1380f3afc366f5b4
MD5 258e1cdfebffc7fa7cd95aa4dd8b49d7
BLAKE2b-256 7bc86898758e78508e917e7d6c52ccb578732ead1cdb035cd5cb4bcb8eb4ddac

See more details on using hashes here.

File details

Details for the file dockercomposefile-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dockercomposefile-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.0 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

Hashes for dockercomposefile-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3982099320275d41e062ddc0e9f860eb276c03fabb4ce73cc57d8d857b0fec88
MD5 8c3bef60f50f96a148054fcb582b99df
BLAKE2b-256 82532737ce021ec0637a51617870b125c02db84f0596fb4a3a38882c4bb89a97

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page