#!/bin/bash # Docker service names are designed to be static, not dynamic, # primarily due to the need for predictable and consistent communication # between services within a Docker Compose environment. However, since an # external letsencrypt network is utilized, conflicts occur if the service # name is not unique to the project. This script resolves that issue by # generating a docker-compose.yml file with unique service names, which is # the COMPOSE_PROJECT_NAME # Load variables from .env file if [ -f .env ]; then source .env fi export SERVICE_SUFFIX=${COMPOSE_PROJECT_NAME:-main} export COMPOSE_PROJECT_NAME envsubst < docker-compose.template.yml > docker-compose.yml # Define the new value for the database host export new_wp="wordpress_${COMPOSE_PROJECT_NAME}" # If default.conf doesn't exist, create it from the template (using envsubst restricted to COMPOSE_PROJECT_NAME) if [ ! -f default.conf ]; then echo "default.conf not found, creating from default.template.conf..." envsubst '${COMPOSE_PROJECT_NAME}' < default.template.conf > default.conf fi # Check if the existing default.conf contains a database hostname matching the pattern (it may or may not contain the env variable placeholder) if grep -qE "\bwordpress_\w+(?::\d+)?\b" default.conf; then # Update in-place only the portion that matches the database host pattern. # sed -i "s/\bwordpress_\w+(?::\d+)?\b/${new_wp}/g" default.conf perl -pi -e 's/\bwordpress_\w+(?::(\d+))?\b/$ENV{"new_wp"} . (defined($1) ? ":$1" : "")/eg' default.conf echo "default.conf updated with COMPOSE_PROJECT_NAME='${COMPOSE_PROJECT_NAME}'." fi