Skip to content

Instantly share code, notes, and snippets.

@jordanlambrecht
Created March 24, 2024 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanlambrecht/23f9bf5c792226bdc84ba7167fa5f61e to your computer and use it in GitHub Desktop.
Save jordanlambrecht/23f9bf5c792226bdc84ba7167fa5f61e to your computer and use it in GitHub Desktop.
Docker-Compose Template
---
name: PROJECT_NAME
version: "3.8"
# ╔══════════════════════════════════════════════════════════════════════════════╗
# ║ ║
# ║ TEMPLATE ║
# ║ ║
# ╚══════════════════════════════════════════════════════════════════════════════╝
# COMMON SERVICES FRAGMENT
x-common-service-config: &common-service-config
environment:
TZ: ${TZ}
PUID: ${PUID}
PGID: ${PGID}
UMASK: ${UMASK}
env_file:
- /Users/<redacted>/Docker/_globals/common.env
- /Users/<redacted>/Docker/_globals/databases.env
- .env
networks:
- network_universal
volumes:
- /Users/<redacted>/Docker/_globals/:/_globals/
entrypoint: ["/_globals/init-common.sh"]
labels:
com.centurylinklabs.watchtower.enable: "true"
uptime-kuma.enable: "true"
uptime-kuma.url: "http://uptime-kuma:${UPTIME_KUMA_PORT}"
restart: unless-stopped
# POSTGRESQL SERVICES FRAGMENT
x-postgres-config: &postgres-config
<<: *common-service-config
environment:
DATABASE_HOST: postgres
DATABASE_PORT: ${POSTGRES_PORT} # Originates from databases.env
entrypoint: ["/_globals/init-common.sh", "/_globals/init-postgres.sh"]
volumes:
- ${POSTGRES_ENTRYPOINT}:/usr/local/bin/init-postgres.sh
networks:
- network_databases
# MARIADB SERVICES FRAGMENT
x-mariadb-config: &mariadb-config
<<: *common-service-config
environment:
DATABASE_HOST: mariadb
DATABASE_PORT: ${MARIA_PORT} # Originates from databases.env
entrypoint: ["/_globals/init-common.sh", "/_globals/init-mariadb.sh"]
volumes:
- ${MARIA_ENTRYPOINT}:/usr/local/bin/init-mariadb.sh
networks:
- network_databases
# REDIS SERVICES FRAGMENT
x-redis-config: &redis-config
<<: *common-service-config
environment:
REDIS_HOST: redis
REDIS_PORT: ${REDIS_PORT} # Originates from databases.env
entrypoint: ["/_globals/init-common.sh", "/_globals/init-redis.sh"]
volumes:
- ${REDIS_ENTRYPOINT}:/usr/local/bin/init-redis.sh
networks:
- network_databases
services:
# ════════════════════════════════════
# STANDARD SERVICE
# ════════════════════════════════════
myservice_non_database:
image: myservice-image
ports:
- "${MYSERVICE_PORT}:8080"
labels:
# com.centurylinklabs.watchtower.enable: false #Future Self: Uncomment to disable watchtower
# uptime-kuma.enable: "false" #Uncomment to disable uptime-kuma
# ════════════════════════════════════
# POSTGRES-DEPENDENT SERVICE
# ════════════════════════════════════
myservice_postgres:
<<: *postgres-config
image: myservice-image
environment:
ports:
- "${MYSERVICE_PORT}:8080"
# ════════════════════════════════════
# MARIA-DEPENDENT SERVICE
# ════════════════════════════════════
myservice_mariadb:
<<: *mariadb-config
image: myservice-image
environment:
DATABASE_USER: myservice # specific to this service
DATABASE_PASSWORD: myservice # specific to this service
DATABASE_NAME: myservice # specific to this service
ports:
- "${MYSERVICE_PORT}:8080"
# ════════════════════════════════════
# REDIS-DEPENDENT SERVICE
# ════════════════════════════════════
myservice_redis:
<<: *redis-config
image: myservice-image
ports:
- "${MYSERVICE_PORT}:8080" # MYSERVICE_PORT needs to be defined in ./.env for this specific service
# ════════════════════════════════════
# NETWORK CONFIG
# ════════════════════════════════════
networks:
network_universal:
name: network_universal
external: true
network_databases:
name: network_databases
external: true
#!/bin/bash
set -a # Automatically export all variables
source /_globals/common.env
set +a # Disable automatic export
exec "$@"
#!/bin/bash
set -e
# Wait for MariaDB to be ready
until mysql -h "$MARIADB_HOST" -u "$MARIADB_USER" -p"$MARIADB_PASSWORD" -e '\q'; do
>&2 echo "MariaDB is unavailable - sleeping"
sleep 1
done
>&2 echo "MariaDB is up - executing command"
# Generate unique credentials based on the service's name or other criteria
DB_NAME="db_${SERVICE_NAME}"
DB_USER="user_${SERVICE_NAME}"
DB_PASS="pass_${SERVICE_NAME}"
# Create the database and user
mysql -u "$MARIADB_USER" -p"$MARIADB_PASSWORD" <<-EOSQL
CREATE DATABASE IF NOT EXISTS \`$DB_NAME\`;
CREATE USER IF NOT EXISTS '$DB_USER'@'%' IDENTIFIED BY '$DB_PASS';
GRANT ALL PRIVILEGES ON \`$DB_NAME\`.* TO '$DB_USER'@'%';
FLUSH PRIVILEGES;
EOSQL
# Execute the main command
exec "$@"
#!/bin/bash
set -e
# Generate unique credentials based on the service's name or other criteria
DB_NAME="db_${SERVICE_NAME}"
DB_USER="user_${SERVICE_NAME}"
DB_PASS="pass_${SERVICE_NAME}"
# Create the database and user
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE DATABASE "$DB_NAME";
CREATE USER "$DB_USER" WITH PASSWORD '$DB_PASS';
GRANT ALL PRIVILEGES ON DATABASE "$DB_NAME" TO "$DB_USER";
EOSQL
# Execute the main command
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment