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
docker-compose.yml
default.conf

View File

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

View File

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

View File

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

View File

@ -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}
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
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:
- custom_network
- letsencrypt
- default
# No variable substition for volumes in this version
volumes:
@ -99,5 +103,5 @@ volumes:
networks:
letsencrypt:
external: true
# default:
# name: ${COMPOSE_PROJECT_NAME}_default
custom_network:
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