Refactor (dev) Docker Compose files for Improved Reusability and Consistency

Refactor the Docker Compose configurations to improve maintainability, reduce
duplication, and ensure consistent environment variable management across all services.

- **Extracted reusable volume and environment variable definitions** using YAML anchors:
  - `&geoserver_environment`: Defines common environment variables for GeoServer services.
  - `&geoserver_volumes`: Standardizes shared volume mounts.
  - `&gs-dependencies`: Centralized service dependencies.
- **Standardized environment variables** across all services:
  - Replaced repetitive `SPRING_PROFILES_ACTIVE` definitions with a shared `x-geoserver-env` anchor.
  - Removed redundant service-specific `JAVA_OPTS` definitions, replacing them with `JAVA_OPTS_DEFAULT`.
- **Updated `.env` file**:
  - Consolidated logging and default profile variables.
  - Cleaned up and reordered variables for clarity.
  - Removed unused `GATEWAY_SHARED_AUTH` and per-service `JAVA_OPTS_*` entries.
- **Refactored service definitions**:
  - Applied `x-geoserver-env` to all GeoServer services.
  - Applied `x-geoserver-volumes` to services requiring volume mounts.
  - Applied `x-gs-dependencies` to all dependent services.
- **Improved volume handling**:
  - Unified the `data_directory` and `geowebcache_data` volumes.
  - Ensured volumes are correctly referenced in all catalog configurations (`datadir`, `jdbcconfig`, `pgconfig`).

- **Reduces duplication**: Less repetition in `compose.yml` files.
- **Improves maintainability**: Easier to modify environment variables and dependencies in one place.
- **Ensures consistency**: All services use the same configuration structure.
- **Enhances readability**: More structured and understandable YAML files.

This refactor does not introduce functional changes but significantly improves configuration management.
This commit is contained in:
Gabriel Roldan 2025-02-18 16:28:53 -03:00
parent b22443aa1b
commit 990193b135
No known key found for this signature in database
GPG Key ID: 697E8F9DF72128E1
6 changed files with 117 additions and 213 deletions

View File

@ -4,21 +4,18 @@ TAG=2.27.0-SNAPSHOT
ACL_REPOSITORY=geoservercloud
ACL_TAG=2.3.1
GATEWAY_PORT=9090
GS_USER="1000:1000"
GATEWAY_SHARED_AUTH=true
# geoserver entry point for the gateway
GEOSERVER_BASE_PATH=/geoserver/cloud
# logging profile, either "default" or "json-logs"
#LOGGING_PROFILE=json-logs
LOGGING_PROFILE=default
GEOSERVER_DEFAULT_PROFILES="${LOGGING_PROFILE},acl"
GATEWAY_PORT=9090
GATEWAY_DEFAULT_PROFILES=${LOGGING_PROFILE}
DISCOVERY_SERVER_DEFAULT_PROFILES=${LOGGING_PROFILE}
# geoserver entry point for the gateway
GEOSERVER_BASE_PATH=/geoserver/cloud
# Either 'git' or 'native' active profile must be set. Use the default sample git repository to download the services configuration from
# If 'git', BEWARE config server will look for a branch called "master", and github changed the default branch name to "main"
@ -27,19 +24,7 @@ CONFIG_SERVER_DEFAULT_PROFILES=${LOGGING_PROFILE},native,standalone
JAVA_OPTS_DEFAULT=-XshowSettings:system -Dlogging.config=file:/etc/geoserver/logback-spring.xml -Xlog:cds
JAVA_OPTS_DISCOVERY=$JAVA_OPTS_DEFAULT
JAVA_OPTS_CONFIG=$JAVA_OPTS_DEFAULT
JAVA_OPTS_GATEWAY=$JAVA_OPTS_DEFAULT
JAVA_OPTS_GEOSERVER=$JAVA_OPTS_DEFAULT -Djndi.postgis.enabled=true
JAVA_OPTS_WFS=$JAVA_OPTS_GEOSERVER
JAVA_OPTS_WMS=$JAVA_OPTS_GEOSERVER
JAVA_OPTS_WCS=$JAVA_OPTS_GEOSERVER
JAVA_OPTS_WPS=$JAVA_OPTS_GEOSERVER
JAVA_OPTS_REST=$JAVA_OPTS_GEOSERVER
JAVA_OPTS_WEBUI=$JAVA_OPTS_GEOSERVER
JAVA_OPTS_GWC=$JAVA_OPTS_GEOSERVER
JAVA_OPTS_GEOSERVER=$JAVA_OPTS_DEFAULT
JDBCCONFIG_DBNAME=geoserver_config
JDBCCONFIG_URL=jdbc:postgresql://database:5432/${JDBCCONFIG_DBNAME}

View File

@ -8,11 +8,23 @@ name: gscloud_dev_datadir
volumes:
data_directory:
geowebcache_data:
# driver_opts:
# type: none
# o: bind
# device: $PWD/catalog-datadir
# Appned the shared data directory to the geoserver_volumes anchor
x-volume-mounts: &geoserver_volumes
- data_directory:/opt/app/data_directory:z
x-gs-dependencies: &gs-dependencies
init-datadir:
condition: service_completed_successfully
x-geoserver-env: &geoserver_environment
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},datadir"
services:
init-datadir:
image: alpine:3.18.4
@ -22,65 +34,37 @@ services:
- data_directory:/opt/app/data_directory
- ./catalog-datadir.tgz:/tmp/datadir.tgz
wfs:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},datadir"
depends_on:
init-datadir:
condition: service_completed_successfully
volumes:
- data_directory:/opt/app/data_directory:z
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
wms:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},datadir"
depends_on:
init-datadir:
condition: service_completed_successfully
volumes:
- data_directory:/opt/app/data_directory:z
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
wcs:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},datadir"
depends_on:
init-datadir:
condition: service_completed_successfully
volumes:
- data_directory:/opt/app/data_directory:z
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
wps:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},datadir"
depends_on:
init-datadir:
condition: service_completed_successfully
volumes:
- data_directory:/opt/app/data_directory:z
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
rest:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},datadir"
depends_on:
init-datadir:
condition: service_completed_successfully
volumes:
- data_directory:/opt/app/data_directory:z
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
webui:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},datadir"
depends_on:
init-datadir:
condition: service_completed_successfully
volumes:
- data_directory:/opt/app/data_directory:z
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
gwc:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},datadir"
depends_on:
init-datadir:
condition: service_completed_successfully
volumes:
- data_directory:/opt/app/data_directory:z
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies

View File

@ -4,10 +4,25 @@ volumes:
jdbcconfig_data: # volume for postgresql data, used to store the geoserver config through jdbcconfig
datadir:
x-volume-mounts: &geoserver_volumes
- datadir:/opt/app/data_directory
x-gs-dependencies: &gs-dependencies
database:
condition: service_started
init-sample-data:
condition: service_completed_successfully
#
# Configures all geoserver services to use the postgresql database server with jdbcconfig as catalog backend.
# Run with `docker compose -f compose.yml -f catalog-jdbcconfig.yml up -d`
#
x-geoserver-env: &geoserver_environment
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},jdbcconfig"
JDBCCONFIG_URL: "${JDBCCONFIG_URL}"
JDBCCONFIG_USERNAME: "${JDBCCONFIG_USERNAME}"
JDBCCONFIG_PASSWORD: "${JDBCCONFIG_PASSWORD}"
services:
init-sample-data:
@ -19,8 +34,9 @@ services:
- ./catalog-datadir.tgz:/tmp/datadir.tgz
database:
# be sure geoserver.backend.jdbcconfig.initdb is set to true in application.yml at lease for the first app run
image: postgres:13-alpine
extends:
file: templates.yml
service: postgrestemplate
environment:
POSTGRES_DB: "${JDBCCONFIG_DBNAME}"
POSTGRES_USER: "${JDBCCONFIG_USERNAME}"
@ -35,87 +51,37 @@ services:
cpus: '2.0'
memory: 1G
wfs:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},jdbcconfig"
JDBCCONFIG_URL: "${JDBCCONFIG_URL}"
JDBCCONFIG_USERNAME: "${JDBCCONFIG_USERNAME}"
JDBCCONFIG_PASSWORD: "${JDBCCONFIG_PASSWORD}"
volumes:
- datadir:/opt/app/data_directory
depends_on:
init-sample-data:
condition: service_completed_successfully
database:
condition: service_started
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
wms:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},jdbcconfig"
JDBCCONFIG_URL: "${JDBCCONFIG_URL}"
JDBCCONFIG_USERNAME: "${JDBCCONFIG_USERNAME}"
JDBCCONFIG_PASSWORD: "${JDBCCONFIG_PASSWORD}"
volumes:
- datadir:/opt/app/data_directory
depends_on:
init-sample-data:
condition: service_completed_successfully
database:
condition: service_started
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
wcs:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},jdbcconfig"
JDBCCONFIG_URL: "${JDBCCONFIG_URL}"
JDBCCONFIG_USERNAME: "${JDBCCONFIG_USERNAME}"
JDBCCONFIG_PASSWORD: "${JDBCCONFIG_PASSWORD}"
volumes:
- datadir:/opt/app/data_directory
depends_on:
init-sample-data:
condition: service_completed_successfully
database:
condition: service_started
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
wps:
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
rest:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},jdbcconfig"
JDBCCONFIG_URL: "${JDBCCONFIG_URL}"
JDBCCONFIG_USERNAME: "${JDBCCONFIG_USERNAME}"
JDBCCONFIG_PASSWORD: "${JDBCCONFIG_PASSWORD}"
volumes:
- datadir:/opt/app/data_directory
depends_on:
init-sample-data:
condition: service_completed_successfully
database:
condition: service_started
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
webui:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},jdbcconfig"
JDBCCONFIG_URL: "${JDBCCONFIG_URL}"
JDBCCONFIG_USERNAME: "${JDBCCONFIG_USERNAME}"
JDBCCONFIG_PASSWORD: "${JDBCCONFIG_PASSWORD}"
volumes:
- datadir:/opt/app/data_directory
depends_on:
init-sample-data:
condition: service_completed_successfully
database:
condition: service_started
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies
gwc:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},jdbcconfig"
JDBCCONFIG_URL: "${JDBCCONFIG_URL}"
JDBCCONFIG_USERNAME: "${JDBCCONFIG_USERNAME}"
JDBCCONFIG_PASSWORD: "${JDBCCONFIG_PASSWORD}"
volumes:
- datadir:/opt/app/data_directory
depends_on:
init-sample-data:
condition: service_completed_successfully
database:
condition: service_started
environment: *geoserver_environment
volumes: *geoserver_volumes
depends_on: *gs-dependencies

View File

@ -3,10 +3,16 @@ name: gscloud_dev_pgconfig
volumes:
pgconfigdb_data: # volume for postgresql data, used to store the geoserver config through pgconfig backend
x-gs-dependencies: &gs-dependencies
pgconfigdb:
condition: service_started
required: true
#
# Configures all geoserver services to use the postgresql database server with pgconfig as catalog backend.
# Run with `docker compose -f compose.yml -f catalog-pgconfig.yml up -d`
#
x-geoserver-env: &geoserver_environment
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},pgconfig"
services:
pgconfigdb:
@ -19,57 +25,29 @@ services:
- 8432:5432
wfs:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},pgconfig"
depends_on:
pgconfigdb:
condition: service_started
required: true
environment: *geoserver_environment
depends_on: *gs-dependencies
wms:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},pgconfig"
depends_on:
pgconfigdb:
condition: service_started
required: true
environment: *geoserver_environment
depends_on: *gs-dependencies
wcs:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},pgconfig"
depends_on:
pgconfigdb:
condition: service_started
required: true
environment: *geoserver_environment
depends_on: *gs-dependencies
rest:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},pgconfig"
depends_on:
pgconfigdb:
condition: service_started
required: true
environment: *geoserver_environment
depends_on: *gs-dependencies
webui:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},pgconfig"
depends_on:
pgconfigdb:
condition: service_started
required: true
environment: *geoserver_environment
depends_on: *gs-dependencies
gwc:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},pgconfig"
depends_on:
pgconfigdb:
condition: service_started
required: true
environment: *geoserver_environment
depends_on: *gs-dependencies
wps:
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES},pgconfig"
depends_on:
pgconfigdb:
condition: service_started
required: true
environment: *geoserver_environment
depends_on: *gs-dependencies

View File

@ -65,7 +65,7 @@ services:
image: ${REPOSITORY}/geoserver-cloud-config:${TAG}
user: ${GS_USER}
environment:
JAVA_OPTS: "${JAVA_OPTS_CONFIG}"
JAVA_OPTS: "${JAVA_OPTS_DEFAULT}"
SPRING_PROFILES_ACTIVE: "${CONFIG_SERVER_DEFAULT_PROFILES}"
# restart: unless-stopped
volumes:
@ -87,7 +87,7 @@ services:
image: ${REPOSITORY}/geoserver-cloud-discovery:${TAG}
user: ${GS_USER}
environment:
JAVA_OPTS: "${JAVA_OPTS_DISCOVERY}"
JAVA_OPTS: "${JAVA_OPTS_DEFAULT}"
SPRING_PROFILES_ACTIVE: "${DISCOVERY_SERVER_DEFAULT_PROFILES}"
ports:
- 8761:8761 # for development, so services can be run from localhost and find the discovery service running on docker
@ -109,11 +109,10 @@ services:
discovery:
condition: service_healthy
environment:
JAVA_OPTS: "${JAVA_OPTS_GATEWAY}"
JAVA_OPTS: "${JAVA_OPTS_DEFAULT}"
# eat our own dogfood and set a base path
GEOSERVER_BASE_PATH: ${GEOSERVER_BASE_PATH}
SPRING_PROFILES_ACTIVE: "${GATEWAY_DEFAULT_PROFILES}"
GATEWAY_SHARED_AUTH: "${GATEWAY_SHARED_AUTH}" #same as in gstemplate
ports:
- ${GATEWAY_PORT}:8080
deploy:
@ -127,8 +126,6 @@ services:
file: templates.yml
service: gstemplate
image: ${REPOSITORY}/geoserver-cloud-wfs:${TAG}
environment:
JAVA_OPTS: "${JAVA_OPTS_WFS}"
depends_on: *gs-dependencies
wms:
@ -136,8 +133,6 @@ services:
extends:
file: templates.yml
service: gstemplate
environment:
JAVA_OPTS: "${JAVA_OPTS_WMS}"
depends_on: *gs-dependencies
wcs:
@ -145,8 +140,6 @@ services:
file: templates.yml
service: gstemplate
image: ${REPOSITORY}/geoserver-cloud-wcs:${TAG}
environment:
JAVA_OPTS: "${JAVA_OPTS_WCS}"
depends_on: *gs-dependencies
wps:
@ -154,8 +147,6 @@ services:
file: templates.yml
service: gstemplate
image: ${REPOSITORY}/geoserver-cloud-wps:${TAG}
environment:
JAVA_OPTS: "${JAVA_OPTS_WPS}"
depends_on: *gs-dependencies
gwc:
@ -163,8 +154,6 @@ services:
file: templates.yml
service: gstemplate
image: ${REPOSITORY}/geoserver-cloud-gwc:${TAG}
environment:
JAVA_OPTS: "${JAVA_OPTS_GWC}"
depends_on: *gs-dependencies
deploy:
resources:
@ -177,8 +166,6 @@ services:
file: templates.yml
service: gstemplate
image: ${REPOSITORY}/geoserver-cloud-rest:${TAG}
environment:
JAVA_OPTS: "${JAVA_OPTS_REST}"
depends_on: *gs-dependencies
webui:
@ -186,7 +173,5 @@ services:
file: templates.yml
service: gstemplate
image: ${REPOSITORY}/geoserver-cloud-webui:${TAG}
environment:
JAVA_OPTS: "${JAVA_OPTS_WEBUI}"
depends_on: *gs-dependencies

View File

@ -1,14 +1,20 @@
# Define reusable volume mounts as an anchor
x-geoserver-volume-mounts: &geoserver_volumes
- geowebcache_data:/data/geowebcache
# Define reusable environment variables
x-geoserver-env: &geoserver_environment
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES}"
# Enable the PostGIS JNDI datasource (for development purposes)
JNDI_POSTGIS_ENABLED: true
GEOWEBCACHE_CACHE_DIR: /data/geowebcache
JAVA_OPTS: "${JAVA_OPTS_GEOSERVER}"
services:
gstemplate:
user: ${GS_USER}
environment:
SPRING_PROFILES_ACTIVE: "${GEOSERVER_DEFAULT_PROFILES}"
# enable the postgis jndi datasource. This is just an example used for development
JNDI_POSTGIS_ENABLED: true
GEOWEBCACHE_CACHE_DIR: /data/geowebcache
GATEWAY_SHARED_AUTH: "${GATEWAY_SHARED_AUTH}"
volumes:
- geowebcache_data:/data/geowebcache
environment: *geoserver_environment
volumes: *geoserver_volumes
deploy:
mode: replicated
replicas: 1