PHPackages                             brettmc/docker-compose-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. brettmc/docker-compose-generator

ActiveProject[Templating &amp; Views](/categories/templating)

brettmc/docker-compose-generator
================================

Template-driven docker-compose file generation

0.15(4y ago)9152[3 issues](https://github.com/brettmc/docker-compose-generator/issues)MITPHPPHP &gt;=7.3

Since Oct 23Pushed 4y ago1 watchersCompare

[ Source](https://github.com/brettmc/docker-compose-generator)[ Packagist](https://packagist.org/packages/brettmc/docker-compose-generator)[ Docs](https://github.com/brettmc/docker-compose-generator)[ RSS](/packages/brettmc-docker-compose-generator/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (10)Dependencies (7)Versions (22)Used By (0)

docker-compose generator
========================

[](#docker-compose-generator)

[![Build Status](https://camo.githubusercontent.com/8f1ce2bc25010a457c4cafce25518eb1751976b7ff9b6663df30f8f606e23b27/68747470733a2f2f7472617669732d63692e636f6d2f62726574746d632f646f636b65722d636f6d706f73652d67656e657261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/brettmc/docker-compose-generator)[![Coverage Status](https://camo.githubusercontent.com/331fe3c6cfd9e0166447ff4f6a0a24aeb395cbf9de98a7a7ce2eefb5c4630328/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f62726574746d632f646f636b65722d636f6d706f73652d67656e657261746f722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/brettmc/docker-compose-generator?branch=master)

A tool for populating a docker-compose file from one or more templates, using mustache-like replacement.

Installation/usage
==================

[](#installationusage)

Composer package:

```
composer require brettmc/docker-compose-generator

```

Docker image:

```
docker run --rm -it brettmc/docker-compose-generator --help

```

Overview
========

[](#overview)

If you need to maintain multiple, slightly-different, [docker-compose](https://docs.docker.com/compose) configurations which change per-environment, you've come to the right place.

This is being used in production in my day job for a couple of medium-sized projects, and is usually used as follows:

- git commit triggers a CI/CD pipeline
- pipeline tests, builds and tags some docker images, ready to be deployed
- pipeline executes a shell script, which tests some variables to work out which environment it is deploying to, and which files it should include:
    - tags -&gt; prod env
    - develop -&gt; test env
    - everything else -&gt; build env
- generate docker-compose config by combining a main template file with one or more environment-specific files, as well as optionally a main and environment-specific .ini file, and finally any dynamic values via the `-e` switch
- pass the generated file to `docker stack deploy`

Per-environment differences
---------------------------

[](#per-environment-differences)

For example, commits to development branches are built and deployed to a dev swarm. Each dev deployment gets its own database, but test and prod deployments do not.

Multiple input files
--------------------

[](#multiple-input-files)

Merge multiple `yaml` templates. For example, a `main.yml` template can be extended by having `dev.yml`add additional services. These will be merged together before applying later operations. If templates are provided from both `stdin` and via the `--input` option, `stdin` is processed first. Multiple inputs provided via `--input` are processed in order.

Variable substitution
---------------------

[](#variable-substitution)

After merging all templates, perform variable substitution across the result, based on values from `.ini` files, then environment variables and finally variables from `-e` flags.

Define variables in numerous ways
---------------------------------

[](#define-variables-in-numerous-ways)

Accepts variables from INI-style environment files, or command switches, in the following order:

- `.ini` files (eg `--ini global.ini --ini local.ini`) are processed in the order they are provided, with duplicate keys overriding earlier; then
- environment variables; then
- command-line variables (`-e FOO=foo`); and finally
- nullable command-line variables (`-o FOO=` or `-o FOO=foo`) - *if an empty value is given, it is ignored and does not clobber an earlier setting*

Usage
=====

[](#usage)

`main.yml`

```
version: '3.4'
networks:
  front:
  back:
services:
  my-service:
    image: "my-service:{{TAG_OR_HASH}}"
    environment:
      BAR: "{{BAR}}"

```

`dev.yml`

```
services:
  my-service:
    ports:
      - "80:80"
  db:
    image: "postgres"

```

`prod.yml`

```
services:
  my-service:
    image: "my-service:{{TAG_OR_HASH}}"

```

`prod.ini`

```
DB_HOST=prod.db.example.com
AUTH=prod.signon.example.com

```

`dev.ini`

```
AUTH=dev.signon.example.com

```

You can then roll your own logic to work out which files to apply after `main.yml`, eg

```
#!/bin/bash
echo "generating for env: ${CI_ENV} #pre-defined variable, eg from a cicd system
case ${CI_ENV} in
  BUILD)
    HOST=docker-build-swarm-manager.example.com
    STACK=${CI_COMMIT_HASH}
    TAG_OR_HASH=${CI_COMMIT_HASH}
    BAR=something
    ;;
  PROD)
    HOST=docker-prod-swarm-manager.example.com
    STACK=prod
    TAG_OR_HASH=${CI_COMMIT_TAG}
    BAR=something-else
    ;;
esac
bin/dcgen.php generate -e HASH=${CI_COMMIT_HASH} -e BAR=${BAR} --ini ${CI_ENV}.ini --input main.yml --input ${CI_ENV}.yml > docker-compose.yml
docker -H ${HOST} stack deploy --prune --with-registry-auth -c docker-compose.yml ${STACK}

```

Run via docker
==============

[](#run-via-docker)

NB, do not attach a TTY (ie, do not use docker's `-t` switch) if you are piping input

```
$ docker run --rm -i brettmc/docker-compose-generator generate -e FOO=foo -e BAR=bar < main.yml > output.yml

```

or

```
$ cat main.yml | docker run --rm -i brettmc/docker-compose-generator generate -e FOO=foo -e BAR=bar > output.yml

```

or just use volumes:

```
$ docker run --rm -v $(pwd)/conf:/srv/conf -v $(pwd)/template:/srv/template brettmc/docker-compose-generator generate --input /srv/template/main.yml --input /srv/template/dev.yml --ini /srv/conf/dev.ini -e DB_HOST=${HOST} > output.yml

```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance13

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~71 days

Recently: every ~36 days

Total

15

Last Release

1770d ago

PHP version history (2 changes)0.10PHP &gt;=7.2

0.11PHP &gt;=7.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4978962?v=4)[Brett McBride](/maintainers/brettmc)[@brettmc](https://github.com/brettmc)

---

Top Contributors

[![brettmc](https://avatars.githubusercontent.com/u/4978962?v=4)](https://github.com/brettmc "brettmc (59 commits)")

---

Tags

docker-composetemplatedockerdocker-compose

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/brettmc-docker-compose-generator/health.svg)

```
[![Health](https://phpackages.com/badges/brettmc-docker-compose-generator/health.svg)](https://phpackages.com/packages/brettmc-docker-compose-generator)
```

###  Alternatives

[laravel/sail

Docker files for running a basic Laravel application.

1.9k186.9M1.0k](/packages/laravel-sail)[tightenco/jigsaw

Simple static sites with Laravel's Blade.

2.2k438.5k29](/packages/tightenco-jigsaw)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[symfony/ux-toolkit

A tool to easily create a design system in your Symfony app with customizable, well-crafted Twig components

1432.0k](/packages/symfony-ux-toolkit)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
