From de8f71b2be275692bda4145bc4426295e68a0467 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Wed, 29 Jul 2026 16:11:01 -0400 Subject: [PATCH] refactor(compose): make app/db ports configurable via DB_PORT and SERVER_PORT Replace hardcoded port literals with env-driven variables so a single change in .env reconfigures the full stack consistently. Defaults are unchanged (DB 5432, app 8765), so existing setups need no .env changes. - DB_PORT (default 5432): drives the db host<->container port mapping, Postgres PGPORT (so it listens on the chosen port), and the app's DATABASE_PORT connection setting. Lets deployers avoid a host port conflict (e.g. another local Postgres) by setting DB_PORT once. - SERVER_PORT (default 8765): drives the app host<->container mapping, the SERVER_PORT the app listens on, and the healthcheck target URL. - Applied to both the base (docker-compose.yml) and the dev override (docker-compose.dev.yml, tests service) so dev and prod stay in sync. --- docker-compose.dev.yml | 4 ++-- docker-compose.yml | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 8f43223..9d69c8a 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -23,7 +23,7 @@ services: environment: # Database Configuration DATABASE_HOST: db - DATABASE_PORT: "5432" + DATABASE_PORT: ${DB_PORT:-5432} DATABASE_USER: postgres DATABASE_PASSWORD: ${DBPASS} DATABASE_NAME: bookhoard @@ -31,7 +31,7 @@ services: # Application Configuration JWT_SECRET: ${JWT_SECRET} - SERVER_PORT: "8765" + SERVER_PORT: ${SERVER_PORT:-8765} # Test Configuration TEST_MODE: "true" diff --git a/docker-compose.yml b/docker-compose.yml index 1e794e4..099eb17 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,13 +7,15 @@ services: POSTGRES_DB: bookhoard POSTGRES_USER: postgres POSTGRES_PASSWORD: ${DBPASS} + # PGPORT makes Postgres listen on DB_PORT (kept in sync with the host mapping + app's DATABASE_PORT) + PGPORT: ${DB_PORT:-5432} volumes: - postgres_data:/var/lib/postgresql/data - ./database/schema:/docker-entrypoint-initdb.d # Make other volumes as needed - ./uploads:/app/uploads ports: - - "5432:5432" + - "${DB_PORT:-5432}:${DB_PORT:-5432}" healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 30s @@ -32,14 +34,14 @@ services: environment: # Database Configuration DATABASE_HOST: db - DATABASE_PORT: "5432" + DATABASE_PORT: ${DB_PORT:-5432} DATABASE_USER: postgres DATABASE_PASSWORD: ${DBPASS} DATABASE_NAME: bookhoard # Application Configuration JWT_SECRET: ${JWT_SECRET} - SERVER_PORT: "8765" + SERVER_PORT: ${SERVER_PORT:-8765} # IMPORTANT: Device sync requires full URL with protocol # Local: http://localhost:8765 # Local network: http://192.168.1.X:8765 @@ -61,7 +63,7 @@ services: # System timezone (fallback for server-side time operations) TZ: ${TZ:-UTC} ports: - - "8765:8765" + - "${SERVER_PORT:-8765}:${SERVER_PORT:-8765}" depends_on: db: condition: service_healthy @@ -69,7 +71,7 @@ services: - ./uploads:/app/uploads - bookhoard_conversion_cache:/app/cache/kepub healthcheck: - test: ["CMD-SHELL", "curl -f http://localhost:8765/health || exit 1"] + test: ["CMD-SHELL", "curl -f http://localhost:${SERVER_PORT:-8765}/health || exit 1"] interval: 30s timeout: 5s retries: 3