Kong config: Make upstream host references configurable
Parent: #758 (closed)
Summary
Make the Kong API gateway configuration (kong_config.json) use configurable upstream host references instead of hardcoded Docker service names. This is needed for deployments where Kong is on a different node than the backend, document-repository, or keycloak.
Current State — Hardcoded Hosts
api-gateway-solution/new-config/kong_config.json:
{
"services": [
{ "name": "express-api", "host": "backend", "port": 3000 },
{ "name": "document-repository", "host": "document-repository", "port": 3001 },
{ "name": "keycloak", "host": "keycloak", "port": 8080 }
],
"targets": [
{ "target": "backend:3000", "upstream": {"id": "..."} }
]
}
These 4 references are hardcoded to Docker DNS names. If backend or doc-repo are on a different node than Kong, these won't resolve.
Options
Option A: Template kong_config.json with envsubst
Convert kong_config.json to a template (kong_config.json.template) with shell variable placeholders:
{
"services": [
{ "name": "express-api", "host": "${KONG_BACKEND_HOST:-backend}", "port": ${KONG_BACKEND_PORT:-3000} },
{ "name": "document-repository", "host": "${KONG_DOCREPO_HOST:-document-repository}", "port": ${KONG_DOCREPO_PORT:-3001} },
{ "name": "keycloak", "host": "${KONG_KEYCLOAK_HOST:-keycloak}", "port": ${KONG_KEYCLOAK_PORT:-8080} }
],
"targets": [
{ "target": "${KONG_BACKEND_HOST:-backend}:${KONG_BACKEND_PORT:-3000}" }
]
}
Then in docker-compose.yaml, the kong-config service runs envsubst before applying:
kong-config:
command: ["sh", "-c", "envsubst < /opt/kong-config/kong_config.json.template > /tmp/kong_config.json && kong config db_import /tmp/kong_config.json"]
Option B: Kong declarative config with environment variable support
Kong's declarative config doesn't natively support env vars. Option A (envsubst) is the standard workaround.
Option C: Use Kong Admin API to register services dynamically
Replace the static JSON config with a startup script that calls the Kong Admin API to register each service with configurable hosts. More flexible but more complex.
Recommendation: Option A — simplest, standard pattern, no Kong knowledge required.
Implementation
- Rename
kong_config.json→kong_config.json.template - Replace hardcoded hosts with
${VAR:-default}placeholders - Update
docker-compose.yamlkong-config service to runenvsubstbefore applying - Pass
KONG_BACKEND_HOST,KONG_DOCREPO_HOST,KONG_KEYCLOAK_HOSTas environment vars
Files
| File | Change |
|---|---|
api-gateway-solution/new-config/kong_config.json |
Rename to .template, add env var placeholders |
docker-compose.yaml |
Update kong-config command to run envsubst |
Acceptance Criteria
-
Kong config uses env-var-with-default for all upstream hosts -
Default behavior unchanged (no env vars = current Docker DNS names) -
docker compose configvalidates without errors -
Kong correctly routes to backend when KONG_BACKEND_HOSTis overridden