Resolves network issue with non-unique service names not playing well with the external letsencrypt network.

This commit is contained in:
Jonathan Rosenbaum 2025-06-08 17:53:29 +00:00
parent db9e08f943
commit b3c3c387f6
6 changed files with 57 additions and 25 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
.env .env
docker-compose.yml
default.conf

View File

@ -56,15 +56,19 @@ HTML_VOLUME=test_html_staging
## Usage ## Usage
### Initialize project with unique COMPOSE_PROJECT_NAME
[./generate-compose-nginx.sh](generate-compose-nginx.sh)
### Upgrade Process ### Upgrade Process
1. Stop the current containers and remove volumes: 1. Stop the current containers and remove volumes:
```bash ```bash
docker compose down -v 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 {}' 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 # Remove Wordpress Image
@ -76,22 +80,22 @@ docker rmi $(grep IMAGE_WEB .env | cut -d '=' -f2)
- Modify version in `Dockerfile.wordpress_fpm` - Modify version in `Dockerfile.wordpress_fpm`
- Update `IMAGE_WEB` in `.env` file - Update `IMAGE_WEB` in `.env` file
3. Build and test the new environment: 3. Build and reinstall the new environment:
```bash ```bash
# Build initial configuration # Build custom Wordpress image
docker compose -f docker-compose.first.yml build 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 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 docker compose -f docker-compose.first.yml down
# Start production environment # Start production environment
docker compose up -d docker compose up -d
``` ```
4. Migrate content: 4. Migrate content (e.g., themes, plugins) after a new installation or upgrade:
```bash ```bash
# In the wordpress service, copy new content to production # In the wordpress service, copy new content to production
cp -a wp-content-new/* wp-content/ cp -a wp-content-new/* wp-content/

View File

@ -5,7 +5,7 @@ server {
listen 80; listen 80;
listen [::]:80; listen [::]:80;
server_name wordpress; server_name wordpress_${COMPOSE_PROJECT_NAME};
root /var/www/html; root /var/www/html;
index index.php; 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$ { location ~ \.php$ {
#fastcgi_split_path_info ^(.+\.php)(/.+)$; #fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass wordpress:9000; fastcgi_pass wordpress_${COMPOSE_PROJECT_NAME}:9000;
fastcgi_index index.php; fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; include fastcgi_params;

View File

@ -26,8 +26,9 @@ services:
max-file: "3" max-file: "3"
restart: always restart: always
networks: networks:
- custom_network
- letsencrypt - letsencrypt
- default
volumes: volumes:
html: html:
@ -36,3 +37,5 @@ volumes:
networks: networks:
letsencrypt: letsencrypt:
external: true external: true
custom_network:
name: ${COMPOSE_PROJECT_NAME}_network # Uses project-specific name

View File

@ -18,8 +18,9 @@
services: services:
database: database_${SERVICE_SUFFIX}:
image: ${IMAGE_DATABASE:-database} image: ${IMAGE_DATABASE:-database}
container_name: ${COMPOSE_PROJECT_NAME}_database
volumes: volumes:
- db:/var/lib/mysql - db:/var/lib/mysql
environment: environment:
@ -34,13 +35,14 @@ services:
max-file: "3" max-file: "3"
restart: always restart: always
networks: networks:
- letsencrypt - custom_network
wordpress: wordpress_${SERVICE_SUFFIX}:
# build: # build:
# context: . # context: .
# dockerfile: Dockerfile.wordpress_fpm # dockerfile: Dockerfile.wordpress_fpm
image: ${IMAGE_WEB:-wordpress} image: ${IMAGE_WEB:-wordpress}
container_name: ${COMPOSE_PROJECT_NAME}_wordpress
volumes: volumes:
- html:/var/www/html - html:/var/www/html
- content:/var/www/html/wp-content - content:/var/www/html/wp-content
@ -61,19 +63,21 @@ services:
command: > command: >
bash -c "chown -R www-data:www-data /var/www/html/wp-content && php-fpm" bash -c "chown -R www-data:www-data /var/www/html/wp-content && php-fpm"
networks: networks:
- custom_network
- letsencrypt - letsencrypt
- default
# nginx:latest and/or can be customized # nginx:latest and/or can be customized
web: web_${SERVICE_SUFFIX}:
# build: # build:
# context: . # context: .
# dockerfile: Dockerfile.nginx # dockerfile: Dockerfile.nginx
image: nginx:latest image: nginx:latest
container_name: ${COMPOSE_PROJECT_NAME}_web
restart: always restart: always
volumes: volumes:
- html:/var/www/html/ - html:/var/www/html/
- content:/var/www/html/wp-content - content:/var/www/html/wp-content
- ./wp-config.php:/var/www/html/wp-config.php
- ./default.conf:/etc/nginx/conf.d/default.conf - ./default.conf:/etc/nginx/conf.d/default.conf
- ./nginx.conf:/etc/nginx/nginx.conf - ./nginx.conf:/etc/nginx/nginx.conf
environment: environment:
@ -82,8 +86,8 @@ services:
- LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL:-me} - LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL:-me}
- LETSENCRYPT_TEST=${LETSENCRYPT_TEST:-true} - LETSENCRYPT_TEST=${LETSENCRYPT_TEST:-true}
networks: networks:
- custom_network
- letsencrypt - letsencrypt
- default
# No variable substition for volumes in this version # No variable substition for volumes in this version
volumes: volumes:
@ -99,5 +103,5 @@ volumes:
networks: networks:
letsencrypt: letsencrypt:
external: true external: true
# default: custom_network:
# name: ${COMPOSE_PROJECT_NAME}_default name: ${COMPOSE_PROJECT_NAME}_network # Uses project-specific name

19
generate-compose-nginx.sh Executable file
View File

@ -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