preprocess docker-compose.yml with m4
This repository has been archived on 2026-04-28. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
  • Shell 46.8%
  • M4 28.1%
  • Makefile 25.1%
Find a file
2024-01-29 06:20:50 +00:00
docker-compose-m4 Initial commit 2024-01-29 05:57:24 +00:00
docker-compose.m4 Initial commit 2024-01-29 05:57:24 +00:00
LICENSE Add LICENSE 2024-01-29 06:20:50 +00:00
Makefile Initial commit 2024-01-29 05:57:24 +00:00
README.md Add README.md 2024-01-29 06:18:31 +00:00

docker-compose-m4

preprocess docker-compose.yml with m4

This is a simple script that preprocesses your docker-compose.yml file with the m4 preprocessor.

Installation

# make install

Usage

Let's say we have a docker-compose.yml file like following. We would like to store it in a git repository, but we don't want to show people the postgres password.

One option would be to use secrets, but that won't work if one of the containers doesn't support reading the password from a file. We would need a preprocessor for that.

docker-compose.yml

version: '3'

services:
  db:
    image: postgres:15
    volumes:
      - ./data/postgres:/var/lib/postgresql/data
    restart: unless-stopped
    environment:
      - POSTGRES_USER=joplin
      - POSTGRES_DB=joplin
      - POSTGRES_PASSWORD=passwordpasswordpasswordpasswordpasswordpasswordpasswordpassword
  app:
    image: joplin/server:latest
    depends_on:
      - db
    ports:
      - "127.0.0.1:22300:22300"
    restart: unless-stopped
    environment:
      - APP_PORT=22300
      - APP_BASE_URL=https://joplin.example.com/
      - DB_CLIENT=pg
      - POSTGRES_CONNECTION_STRING=postgresql://joplin:passwordpasswordpasswordpasswordpasswordpasswordpasswordpassword@db/joplin?sslmode=disable
  1. Let's rename docker-compose.yml to docker-compose.yml.m4
mv docker-compose.yml docker-compose.yml.m4
  1. Move the password to a separate text file next to docker-compose.yml.m4 and modify docker-compose.yml.m4 to read the password from that file.

pgpass.txt

passwordpasswordpasswordpasswordpasswordpasswordpasswordpassword

We can use the iinclude() function from docker-compose-m4 to include a file and strip newlines.

docker-compose.yml.m4

version: '3'

services:
  db:
    image: postgres:15
    volumes:
      - ./data/postgres:/var/lib/postgresql/data
    restart: unless-stopped
    environment:
      - POSTGRES_USER=joplin
      - POSTGRES_DB=joplin
-      - POSTGRES_PASSWORD=passwordpasswordpasswordpasswordpasswordpasswordpasswordpassword
+      - POSTGRES_PASSWORD=iinclude(pgpass.txt)
  app:
    image: joplin/server:latest
    depends_on:
      - db
    ports:
      - "127.0.0.1:22300:22300"
    restart: unless-stopped
    environment:
      - APP_PORT=22300
      - APP_BASE_URL=https://joplin.example.com/
      - DB_CLIENT=pg
-      - POSTGRES_CONNECTION_STRING=postgresql://joplin:passwordpasswordpasswordpasswordpasswordpasswordpasswordpassword@db/joplin?sslmode=disable
+      - POSTGRES_CONNECTION_STRING=postgresql://joplin:iinclude(pgpass.txt)@db/joplin?sslmode=disable

Finally run the preprocessor to generate the docker-compose.yml file.

docker-compose-m4

docker-compose.yml is now created and ready to be used.

docker-compose.yml

# DO NOT EDIT THIS FILE
# it has been autogenerated from docker-compose.yml.m4, edit that file instead and then run docker-compose-m4

version: '3'

services:
  db:
    image: postgres:15
    volumes:
      - ./data/postgres:/var/lib/postgresql/data
    restart: unless-stopped
    environment:
      - POSTGRES_USER=joplin
      - POSTGRES_DB=joplin
      - POSTGRES_PASSWORD=passwordpasswordpasswordpasswordpasswordpasswordpasswordpassword
  app:
    image: joplin/server:latest
    depends_on:
      - db
    ports:
      - "127.0.0.1:22300:22300"
    restart: unless-stopped
    environment:
      - APP_PORT=22300
      - APP_BASE_URL=https://joplin.example.com/
      - DB_CLIENT=pg
      - POSTGRES_CONNECTION_STRING=postgresql://joplin:passwordpasswordpasswordpasswordpasswordpasswordpasswordpassword@db/joplin?sslmode=disable