Align Docker Compose Environment Variables
Align environment variable definitions in Docker Compose files for improved readability across services.
Detailed Explanation
Tidying Docker Compose Environment Blocks
Docker Compose YAML files often include environment blocks with many key-value pairs. When these grow beyond a few entries, alignment makes the configuration easier to review and maintain.
Before
DB_HOST=postgres
DB_PORT=5432
DB_NAME=app
DB_USER=admin
DB_PASSWORD=secret
REDIS_URL=redis://redis:6379
JWT_SECRET=my-long-secret-key
LOG_LEVEL=debug
CORS_ORIGIN=http://localhost:3000
MAX_UPLOAD_SIZE=10485760
After
DB_HOST =postgres
DB_PORT =5432
DB_NAME =app
DB_USER =admin
DB_PASSWORD =secret
REDIS_URL =redis://redis:6379
JWT_SECRET =my-long-secret-key
LOG_LEVEL =debug
CORS_ORIGIN =http://localhost:3000
MAX_UPLOAD_SIZE =10485760
Configuration
- Select Equals = as the input delimiter.
- Output delimiter: Same as Input.
- Enable Trim whitespace.
- Left-align both columns.
Docker Compose YAML Syntax
There are two ways to define environment variables in Docker Compose:
- Mapping style:
KEY: value— use the Colon delimiter for these. - Array style:
- KEY=value— strip the leading-first, then use the Equals delimiter.
This tool works best with the second (array) style after removing the YAML list markers. For mapping style, switch to the colon delimiter.
Multiple Services
If you have many services each with their own environment block, align each block separately. The tool processes all lines as one table, so mixing entries from different services would create an overly wide alignment.
Use Case
A DevOps engineer is reviewing a Docker Compose file with 8 services and wants to align the environment blocks for each service to improve readability during code review.