diff --git a/.gitignore b/.gitignore index 2eea525..8dbba9d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.env \ No newline at end of file +.env +docker-compose.yml +default.conf \ No newline at end of file diff --git a/README.md b/README.md index 804b16d..77d9d73 100644 --- a/README.md +++ b/README.md @@ -56,15 +56,19 @@ HTML_VOLUME=test_html_staging ## Usage +### Initialize project with unique COMPOSE_PROJECT_NAME + +[./generate-compose-nginx.sh](generate-compose-nginx.sh) + ### Upgrade Process 1. Stop the current containers and remove volumes: ```bash docker compose down -v -# Optional Steps +# Optional Steps for testing and development -# Clean volumes +# Remove external database and content volume (DANGER: only do this if you have backed up these two volumes) grep -E 'DB_VOLUME|CONTENT_VOLUME' .env | cut -d '=' -f2 | xargs -I {} sh -c 'docker volume rm {} 2>/dev/null; docker volume create {}' # Remove Wordpress Image @@ -76,22 +80,22 @@ docker rmi $(grep IMAGE_WEB .env | cut -d '=' -f2) - Modify version in `Dockerfile.wordpress_fpm` - Update `IMAGE_WEB` in `.env` file -3. Build and test the new environment: +3. Build and reinstall the new environment: ```bash -# Build initial configuration +# Build custom Wordpress image docker compose -f docker-compose.first.yml build -# Test the build +# Install or Upgrade Wordpress /var/www/html volume (not external) from custom Wordpress image docker compose -f docker-compose.first.yml up -d -# Stop test environment +# Stop and remove Wordpress container, but keep html volume docker compose -f docker-compose.first.yml down # Start production environment docker compose up -d ``` -4. Migrate content: +4. Migrate content (e.g., themes, plugins) after a new installation or upgrade: ```bash # In the wordpress service, copy new content to production cp -a wp-content-new/* wp-content/ diff --git a/default.conf b/default.template.conf similarity index 90% rename from default.conf rename to default.template.conf index f2e5e90..2946697 100644 --- a/default.conf +++ b/default.template.conf @@ -5,7 +5,7 @@ server { listen 80; listen [::]:80; - server_name wordpress; + server_name wordpress_${COMPOSE_PROJECT_NAME}; root /var/www/html; index index.php; @@ -24,11 +24,11 @@ server { } - # pass the PHP scripts to FastCGI server listening on wordpress2:9000 + # pass the PHP scripts to FastCGI server listening on wordpress:9000 # location ~ \.php$ { #fastcgi_split_path_info ^(.+\.php)(/.+)$; - fastcgi_pass wordpress:9000; + fastcgi_pass wordpress_${COMPOSE_PROJECT_NAME}:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; diff --git a/docker-compose.first.yml b/docker-compose.first.yml index 951d775..f95a5c1 100644 --- a/docker-compose.first.yml +++ b/docker-compose.first.yml @@ -11,7 +11,7 @@ services: WORDPRESS_VERSION: ${WORDPRESS_VERSION} image: ${IMAGE_WEB:-web} volumes: - - html:/var/www/html + - html:/var/www/html environment: - WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST:-database:3306} - WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD:-fake} @@ -26,8 +26,9 @@ services: max-file: "3" restart: always networks: + - custom_network - letsencrypt - - default + volumes: html: @@ -35,4 +36,6 @@ volumes: networks: letsencrypt: - external: true \ No newline at end of file + external: true + custom_network: + name: ${COMPOSE_PROJECT_NAME}_network # Uses project-specific name \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.template.yml similarity index 83% rename from docker-compose.yml rename to docker-compose.template.yml index 7e271f0..3fac10f 100644 --- a/docker-compose.yml +++ b/docker-compose.template.yml @@ -18,8 +18,9 @@ services: - database: + database_${SERVICE_SUFFIX}: image: ${IMAGE_DATABASE:-database} + container_name: ${COMPOSE_PROJECT_NAME}_database volumes: - db:/var/lib/mysql environment: @@ -34,13 +35,14 @@ services: max-file: "3" restart: always networks: - - letsencrypt + - custom_network - wordpress: + wordpress_${SERVICE_SUFFIX}: # build: # context: . # dockerfile: Dockerfile.wordpress_fpm - image: ${IMAGE_WEB:-wordpress} + image: ${IMAGE_WEB:-wordpress} + container_name: ${COMPOSE_PROJECT_NAME}_wordpress volumes: - html:/var/www/html - content:/var/www/html/wp-content @@ -61,19 +63,21 @@ services: command: > bash -c "chown -R www-data:www-data /var/www/html/wp-content && php-fpm" networks: + - custom_network - letsencrypt - - default # nginx:latest and/or can be customized - web: + web_${SERVICE_SUFFIX}: # build: # context: . # dockerfile: Dockerfile.nginx - image: nginx:latest + image: nginx:latest + container_name: ${COMPOSE_PROJECT_NAME}_web restart: always volumes: - html:/var/www/html/ - content:/var/www/html/wp-content + - ./wp-config.php:/var/www/html/wp-config.php - ./default.conf:/etc/nginx/conf.d/default.conf - ./nginx.conf:/etc/nginx/nginx.conf environment: @@ -82,8 +86,8 @@ services: - LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL:-me} - LETSENCRYPT_TEST=${LETSENCRYPT_TEST:-true} networks: - - letsencrypt - - default + - custom_network + - letsencrypt # No variable substition for volumes in this version volumes: @@ -99,5 +103,5 @@ volumes: networks: letsencrypt: external: true - # default: - # name: ${COMPOSE_PROJECT_NAME}_default \ No newline at end of file + custom_network: + name: ${COMPOSE_PROJECT_NAME}_network # Uses project-specific name \ No newline at end of file diff --git a/generate-compose-nginx.sh b/generate-compose-nginx.sh new file mode 100755 index 0000000..a580f06 --- /dev/null +++ b/generate-compose-nginx.sh @@ -0,0 +1,19 @@ +#!/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 +envsubst '${COMPOSE_PROJECT_NAME}' < default.template.conf > default.conf \ No newline at end of file