pycomposefile

0.0.34 · active · verified Thu Apr 09

pycomposefile provides structured deserialization of Docker Compose files into Python objects, making it easier to programmatically inspect and manipulate Compose configurations. It is currently at version 0.0.34 and sees regular, minor releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a Docker Compose file and access its deserialized properties. It also shows how environment variable interpolation (e.g., `${VAR:-default}`) is handled.

import os
from pycomposefile.compose_file import load_composefile

# Create a dummy docker-compose.yaml file for demonstration
compose_content = """
version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "${APP_PORT:-8080}:80"
    environment:
      - DB_HOST=${DB_HOST:-localhost}
  db:
    image: postgres:13
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:
"""

with open("docker-compose.yaml", "w") as f:
    f.write(compose_content)

# Set environment variables for testing
os.environ['APP_PORT'] = os.environ.get('APP_PORT', '9000')
# DB_HOST is intentionally left unset to test default value

# Load the compose file
compose_file = load_composefile("docker-compose.yaml")

# Access services and their properties
web_service = compose_file.services['web']
print(f"Web service image: {web_service.image}")
print(f"Web service ports: {web_service.ports}")
print(f"Web service DB_HOST environment variable: {web_service.environment.get('DB_HOST')}")

# Clean up the dummy file
os.remove("docker-compose.yaml")

view raw JSON →