Skip to main content

A Python tool for formatting Docker Compose YAML files

Project description

composefmt

A Python tool for formatting Docker Compose YAML files. It reorders top-level and service-level keys in a consistent, predefined order while preserving comments, quotes, and structure.

Features

  • Reorders top-level and service-level keys in Docker Compose files
  • Configure key orders and YAML indentation via a TOML config file
  • Preserves comments, quotes, and YAML structure
  • Supports files (glob patterns) and stdin piping
  • Outputs formatted content to stdout or overwrites files in-place
  • Check-mode for verify formatting without modifying files

Requirements

  • Python 3.13+
  • uv (for dependency management and installation)

For a full list of Python dependencies, see uv tree or check pyproject.toml.

Setup

git clone <repository-url>
cd composefmt
uv sync

Installation (Optional)

Install composefmt as a global tool:

uv tool install .
# or
uv tool install git+https://git.smsvc.net/docker/composefmt.git

Configuration (Optional)

Create a config file at $XDG_CONFIG_HOME/composefmt.toml to customize key orders and YAML indentation.
This is optional: If no configuration file exists or config options are omitted, the builtin defaults will be used (see src/composefmt/lib/defaults.py).
YAML keys that are not defined in the configuration will be appended in existing order.

# Custom top-level order
top_level_order = [
    "name",
    "services",
    "networks",
    "volumes",
]

# Custom service-level order
service_order = [
    "image",
    "build",
    "ports",
    "volumes",
    "environment",
]

# Custom YAML indentation
yaml_indent = 4

Usage (multiple ways)

# Format files and overwrite in-place
./src/composefmt/cli.py docker-compose.yml

# Format files and output to stdout
uv run composefmt docker-compose.yml --stdout

# Check if files are formatted (without modifying them)
composefmt docker-compose.yml --check

# Read from stdin (e.g., for piping)
cat docker-compose.yml | composefmt

see --help for further details

Testing

All tests are defined in ./tests/ and can be run via pytest.

Exit Codes

Exit codes are only relevant in check mode (--check).
In all other modes, the script exits with 0.

Code Meaning
0 No files would be changed
1 One or more files need formatting

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

composefmt-0.3.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

composefmt-0.3.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file composefmt-0.3.1.tar.gz.

File metadata

  • Download URL: composefmt-0.3.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"EndeavourOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for composefmt-0.3.1.tar.gz
Algorithm Hash digest
SHA256 f70d293e3dac46d5029d332a9341e4b8fe5d334ddaf7143dab89ae8ddeeae619
MD5 f7219b3feee4a2bb2f95dc8a0c127ad8
BLAKE2b-256 4ebc0c7686d23d2e0484b13f5df2021df6fad9efd69d345c369d9b8e6396b77e

See more details on using hashes here.

File details

Details for the file composefmt-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: composefmt-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"EndeavourOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for composefmt-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8dc4c5c3129d8564a9917ea58288e334dd91b0f10b02df12b81bc3da19df24c1
MD5 22de95d760c1e68f75fd7e5b1d1c01c8
BLAKE2b-256 fc8f4b3b58a8f67d749e3f7f503a8d74f8747b92b2c873e7ca2e3bed4004d731

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