From 25b68cb1df7a8f8749022536799b2c1531893505 Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 6 Sep 2026 21:07:51 +0200 Subject: [PATCH] Fix db host-port collision: separate DB_HOST_PORT from the internal port DB_PORT was doing double duty - the container-internal port baked into DATABASE_URL (must be 3306) and the host-published port in docker-compose.yml. Setting it to 3306 for the URL made compose try to publish host :3306, which collides with another stack's MySQL. DATABASE_URL now hardcodes database:3306 (always correct inside the compose network); the host publish uses ${DB_HOST_PORT:-3310}. Co-Authored-By: Claude Sonnet 5 --- .env | 15 ++++++++------- README.md | 4 ++-- docker-compose.yml | 2 +- docker/setup.sh | 4 ++-- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.env b/.env index b2be5d1..ff4c98a 100644 --- a/.env +++ b/.env @@ -34,18 +34,19 @@ DEFAULT_URI=https://dmtools.fvandenberg.nl ###> doctrine/doctrine-bundle ### # Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # -# In Docker (servers) the host is the `database` service and these parts come -# from docker-compose.yml / .env.local. On the dev box, point DATABASE_URL at -# your local MariaDB in .env.local, e.g.: -# DATABASE_URL="mysql://root:@127.0.0.1:3306/dmtools?serverVersion=10.10.2-MariaDB&charset=utf8mb4" -DB_HOST=database -DB_PORT=3306 +# In Docker (servers) php talks to the `database` service on its own port +# 3306 - always. The host-published port (for a DB client on the server) is +# a separate thing: docker-compose.yml uses ${DB_HOST_PORT:-3310}, override +# it in .env.local if 3310 is taken. +# +# On the dev box, point DATABASE_URL at your local MariaDB in .env.local: +# DATABASE_URL="mysql://root:@127.0.0.1:3307/dmtools?serverVersion=10.10.2-MariaDB&charset=utf8mb4" DB_NAME=dmtools DB_USER=dmtools DB_PASSWORD=ChangeMe DB_ROOT_PASSWORD=ChangeMeRoot DB_SERVER_VERSION=mariadb-12.3.2 -DATABASE_URL="mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?serverVersion=${DB_SERVER_VERSION}&charset=utf8mb4" +DATABASE_URL="mysql://${DB_USER}:${DB_PASSWORD}@database:3306/${DB_NAME}?serverVersion=${DB_SERVER_VERSION}&charset=utf8mb4" ###< doctrine/doctrine-bundle ### ###> symfony/messenger ### diff --git a/README.md b/README.md index a58a467..1d3fb69 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ MariaDB/MySQL (WAMP is fine). `.php-version` pins this directory to PHP 8.5. cp .env .env.local # then edit .env.local: # APP_ENV=dev -# DATABASE_URL="mysql://root:@127.0.0.1:3306/dmtools?serverVersion=10.10.2-MariaDB&charset=utf8mb4" +# DATABASE_URL="mysql://root:@127.0.0.1:3307/dmtools?serverVersion=10.10.2-MariaDB&charset=utf8mb4" # 2. Install and build symfony composer install @@ -51,7 +51,7 @@ Docker only. `docker/setup.sh` pins the compose project name to `dmtools` git pull cp .env .env.local # first time only # edit .env.local: real APP_SECRET (or let setup.sh generate it), -# DB_PASSWORD, DB_ROOT_PASSWORD, and NGINX_PORT / DB_PORT if 8085 / 3310 clash +# DB_PASSWORD, DB_ROOT_PASSWORD, and NGINX_PORT / DB_HOST_PORT if 8085 / 3310 clash ./docker/setup.sh docker compose -p dmtools exec php php bin/console app:user:create you@example.com --admin ``` diff --git a/docker-compose.yml b/docker-compose.yml index 9e70b89..abaf696 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -52,7 +52,7 @@ services: - --character-set-server=utf8mb4 - --collation-server=utf8mb4_unicode_ci ports: - - "127.0.0.1:${DB_PORT:-3310}:3306" + - "127.0.0.1:${DB_HOST_PORT:-3310}:3306" volumes: - database_data:/var/lib/mysql healthcheck: diff --git a/docker/setup.sh b/docker/setup.sh index f8a8266..a93aa5d 100755 --- a/docker/setup.sh +++ b/docker/setup.sh @@ -16,12 +16,12 @@ set -euo pipefail # ./docker/setup.sh --down # stop and remove containers # # NGINX_PORT=8086 ./docker/setup.sh # if 8085 is already taken on this host -# (or set NGINX_PORT / DB_PORT once in .env.local and every run picks it up) +# (or set NGINX_PORT / DB_HOST_PORT once in .env.local and every run picks it up) ROOT_DIR=$(cd "$(dirname "$0")"/.. && pwd) PROJECT=dmtools -for var in NGINX_PORT DB_PORT; do +for var in NGINX_PORT DB_HOST_PORT; do if [ -z "${!var:-}" ] && grep -q "^${var}=" "$ROOT_DIR/.env.local" 2>/dev/null; then export "$var=$(grep "^${var}=" "$ROOT_DIR/.env.local" | tail -1 | cut -d= -f2-)" fi