Back to Blog

Building a Docker Compose Template Library for Your Homelab

Create reusable Docker Compose templates for rapid deployment. Learn to build a personal library of self-hosted service configurations with best practices.

Posted by

Docker Compose template library

Why Build a Template Library?

Every time you deploy a new service, you start from scratch - searching for compose files, figuring out volumes, setting up networks. A personal template library saves hours and ensures consistency.

Directory Structure

homelab-templates/
├── templates/
│   ├── databases/
│   │   ├── postgres.yml
│   │   ├── mysql.yml
│   │   └── redis.yml
│   ├── media/
│   │   ├── jellyfin.yml
│   │   └── plex.yml
│   ├── productivity/
│   │   ├── nextcloud.yml
│   │   └── vaultwarden.yml
│   └── monitoring/
│       ├── uptime-kuma.yml
│       └── grafana-stack.yml
├── base/
│   ├── networks.yml
│   └── common.yml
├── .env.example
└── README.md

Base Template with Extensions

# base/common.yml
x-common: &common
  restart: unless-stopped
  logging:
    driver: "json-file"
    options:
      max-size: "10m"
      max-file: "3"

# templates/databases/postgres.yml
services:
  postgres:
    <<: *common
    image: postgres:15-alpine
    environment:
      - POSTGRES_USER=${DB_USER:-postgres}
      - POSTGRES_PASSWORD=${DB_PASSWORD:?Required}
      - POSTGRES_DB=${DB_NAME:-app}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:

Deployment Script

#!/bin/bash
# deploy.sh - Deploy from templates

SERVICE=$1
if [ -z "$SERVICE" ]; then
  echo "Usage: ./deploy.sh <service>"
  exit 1
fi

TEMPLATE=$(find templates -name "$SERVICE.yml" | head -1)
if [ -z "$TEMPLATE" ]; then
  echo "Template not found: $SERVICE"
  exit 1
fi

mkdir -p ~/homelab/$SERVICE
cp $TEMPLATE ~/homelab/$SERVICE/docker-compose.yml
cp .env.example ~/homelab/$SERVICE/.env

echo "Deployed $SERVICE to ~/homelab/$SERVICE"
echo "Edit .env and run: docker compose up -d"

Continue Reading