Kong API Gateway
Kong API Gateway with PostgreSQL and Konga admin dashboard for API management.
Overview
Kong is an open-source, cloud-native API gateway built on top of NGINX that provides traffic control, security, analytics, and transformation capabilities for APIs and microservices. Originally developed by Mashape (now Kong Inc.) in 2015, Kong has become one of the most popular API gateways due to its plugin architecture, high performance, and extensive feature set for managing API traffic at scale.
This deployment creates a complete Kong API management platform using four interconnected services: a PostgreSQL database (kong-database) for storing Kong's configuration, a migration service (kong-migration) that initializes the database schema, the Kong gateway itself handling API traffic, and Konga providing a web-based administrative interface. The stack includes proper health checks and dependency management to ensure services start in the correct order, with Kong's database migrations completing before the gateway starts.
This configuration is ideal for organizations implementing microservices architectures, API-first development strategies, or those needing centralized API management with advanced features like rate limiting, authentication, and request transformation. The inclusion of Konga makes this particularly valuable for teams who prefer visual administration over command-line API management, while the PostgreSQL backend ensures Kong's configuration persists and can handle enterprise-scale API traffic.
Key Features
- Plugin-based architecture with over 50 official plugins for authentication, security, traffic control, and analytics
- Declarative configuration support allowing GitOps-style API management through YAML files
- Advanced rate limiting with multiple algorithms including sliding window, fixed window, and leaky bucket
- Built-in support for multiple authentication methods including JWT, OAuth 2.0, LDAP, and API keys
- Request and response transformation capabilities with custom headers, body modification, and routing rules
- Konga web dashboard for visual API management, service discovery, and plugin configuration
- Real-time API analytics and logging with structured output for integration with monitoring systems
- Load balancing with health checks, circuit breakers, and automatic failover for upstream services
Common Use Cases
- 1Microservices API gateway for containerized applications requiring centralized traffic management
- 2Enterprise API management platform with role-based access control and audit trails
- 3E-commerce platforms needing rate limiting, API key management, and payment gateway integration
- 4SaaS applications requiring multi-tenant API access with quotas and billing integration
- 5Mobile backend services with JWT authentication, CORS handling, and request throttling
- 6Legacy system modernization using Kong as a facade for gradual API migration
- 7Development and staging environments where teams need visual API testing and configuration management
Prerequisites
- Docker Engine 20.10+ and Docker Compose V2 for proper health check and dependency support
- Minimum 2GB RAM allocated to Docker (Kong requires 512MB, PostgreSQL 256MB, plus overhead)
- Available ports 8000 (Kong proxy), 8001 (Kong admin), 8443/8444 (HTTPS), and 1337 (Konga)
- Environment variables KONG_PG_PASSWORD and KONGA_TOKEN_SECRET configured in .env file
- Basic understanding of API gateway concepts, HTTP proxying, and PostgreSQL administration
- Familiarity with Kong's plugin system and REST API for advanced configuration management
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 kong-database: 3 image: postgres:16-alpine4 container_name: kong-database5 environment: 6 - POSTGRES_USER=kong7 - POSTGRES_DB=kong8 - POSTGRES_PASSWORD=${KONG_PG_PASSWORD}9 volumes: 10 - kong_data:/var/lib/postgresql/data11 healthcheck: 12 test: ["CMD-SHELL", "pg_isready -U kong"]13 interval: 10s14 timeout: 5s15 retries: 516 networks: 17 - kong-network1819 kong-migration: 20 image: kong:3.521 command: kong migrations bootstrap22 environment: 23 - KONG_DATABASE=postgres24 - KONG_PG_HOST=kong-database25 - KONG_PG_USER=kong26 - KONG_PG_PASSWORD=${KONG_PG_PASSWORD}27 depends_on: 28 kong-database: 29 condition: service_healthy30 networks: 31 - kong-network3233 kong: 34 image: kong:3.535 container_name: kong36 environment: 37 - KONG_DATABASE=postgres38 - KONG_PG_HOST=kong-database39 - KONG_PG_USER=kong40 - KONG_PG_PASSWORD=${KONG_PG_PASSWORD}41 - KONG_PROXY_ACCESS_LOG=/dev/stdout42 - KONG_ADMIN_ACCESS_LOG=/dev/stdout43 - KONG_PROXY_ERROR_LOG=/dev/stderr44 - KONG_ADMIN_ERROR_LOG=/dev/stderr45 - KONG_ADMIN_LISTEN=0.0.0.0:800146 ports: 47 - "8000:8000"48 - "8443:8443"49 - "8001:8001"50 - "8444:8444"51 depends_on: 52 kong-migration: 53 condition: service_completed_successfully54 healthcheck: 55 test: ["CMD", "kong", "health"]56 interval: 10s57 timeout: 10s58 retries: 1059 networks: 60 - kong-network6162 konga: 63 image: pantsel/konga:latest64 container_name: konga65 environment: 66 - NODE_ENV=production67 - TOKEN_SECRET=${KONGA_TOKEN_SECRET}68 ports: 69 - "1337:1337"70 depends_on: 71 - kong72 networks: 73 - kong-network7475volumes: 76 kong_data: 7778networks: 79 kong-network: 80 driver: bridge.env Template
.env
1# Kong API Gateway2KONG_PG_PASSWORD=kong_secure_password3KONGA_TOKEN_SECRET=your-secret-tokenUsage Notes
- 1Kong Admin API at http://localhost:8001
- 2Kong Proxy at http://localhost:8000
- 3Konga Dashboard at http://localhost:1337
- 4Connect Konga to http://kong:8001
- 5Supports plugins for auth, rate-limiting, etc.
Individual Services(4 services)
Copy individual services to mix and match with your existing compose files.
kong-database
kong-database:
image: postgres:16-alpine
container_name: kong-database
environment:
- POSTGRES_USER=kong
- POSTGRES_DB=kong
- POSTGRES_PASSWORD=${KONG_PG_PASSWORD}
volumes:
- kong_data:/var/lib/postgresql/data
healthcheck:
test:
- CMD-SHELL
- pg_isready -U kong
interval: 10s
timeout: 5s
retries: 5
networks:
- kong-network
kong-migration
kong-migration:
image: kong:3.5
command: kong migrations bootstrap
environment:
- KONG_DATABASE=postgres
- KONG_PG_HOST=kong-database
- KONG_PG_USER=kong
- KONG_PG_PASSWORD=${KONG_PG_PASSWORD}
depends_on:
kong-database:
condition: service_healthy
networks:
- kong-network
kong
kong:
image: kong:3.5
container_name: kong
environment:
- KONG_DATABASE=postgres
- KONG_PG_HOST=kong-database
- KONG_PG_USER=kong
- KONG_PG_PASSWORD=${KONG_PG_PASSWORD}
- KONG_PROXY_ACCESS_LOG=/dev/stdout
- KONG_ADMIN_ACCESS_LOG=/dev/stdout
- KONG_PROXY_ERROR_LOG=/dev/stderr
- KONG_ADMIN_ERROR_LOG=/dev/stderr
- KONG_ADMIN_LISTEN=0.0.0.0:8001
ports:
- "8000:8000"
- "8443:8443"
- "8001:8001"
- "8444:8444"
depends_on:
kong-migration:
condition: service_completed_successfully
healthcheck:
test:
- CMD
- kong
- health
interval: 10s
timeout: 10s
retries: 10
networks:
- kong-network
konga
konga:
image: pantsel/konga:latest
container_name: konga
environment:
- NODE_ENV=production
- TOKEN_SECRET=${KONGA_TOKEN_SECRET}
ports:
- "1337:1337"
depends_on:
- kong
networks:
- kong-network
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 kong-database:5 image: postgres:16-alpine6 container_name: kong-database7 environment:8 - POSTGRES_USER=kong9 - POSTGRES_DB=kong10 - POSTGRES_PASSWORD=${KONG_PG_PASSWORD}11 volumes:12 - kong_data:/var/lib/postgresql/data13 healthcheck:14 test: ["CMD-SHELL", "pg_isready -U kong"]15 interval: 10s16 timeout: 5s17 retries: 518 networks:19 - kong-network2021 kong-migration:22 image: kong:3.523 command: kong migrations bootstrap24 environment:25 - KONG_DATABASE=postgres26 - KONG_PG_HOST=kong-database27 - KONG_PG_USER=kong28 - KONG_PG_PASSWORD=${KONG_PG_PASSWORD}29 depends_on:30 kong-database:31 condition: service_healthy32 networks:33 - kong-network3435 kong:36 image: kong:3.537 container_name: kong38 environment:39 - KONG_DATABASE=postgres40 - KONG_PG_HOST=kong-database41 - KONG_PG_USER=kong42 - KONG_PG_PASSWORD=${KONG_PG_PASSWORD}43 - KONG_PROXY_ACCESS_LOG=/dev/stdout44 - KONG_ADMIN_ACCESS_LOG=/dev/stdout45 - KONG_PROXY_ERROR_LOG=/dev/stderr46 - KONG_ADMIN_ERROR_LOG=/dev/stderr47 - KONG_ADMIN_LISTEN=0.0.0.0:800148 ports:49 - "8000:8000"50 - "8443:8443"51 - "8001:8001"52 - "8444:8444"53 depends_on:54 kong-migration:55 condition: service_completed_successfully56 healthcheck:57 test: ["CMD", "kong", "health"]58 interval: 10s59 timeout: 10s60 retries: 1061 networks:62 - kong-network6364 konga:65 image: pantsel/konga:latest66 container_name: konga67 environment:68 - NODE_ENV=production69 - TOKEN_SECRET=${KONGA_TOKEN_SECRET}70 ports:71 - "1337:1337"72 depends_on:73 - kong74 networks:75 - kong-network7677volumes:78 kong_data:7980networks:81 kong-network:82 driver: bridge83EOF8485# 2. Create the .env file86cat > .env << 'EOF'87# Kong API Gateway88KONG_PG_PASSWORD=kong_secure_password89KONGA_TOKEN_SECRET=your-secret-token90EOF9192# 3. Start the services93docker compose up -d9495# 4. View logs96docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/kong-gateway-postgres/run | bashTroubleshooting
- kong-migration fails with connection refused: Ensure kong-database health check passes before migration runs, check KONG_PG_PASSWORD environment variable matches
- Kong admin API returns 404 errors: Verify KONG_ADMIN_LISTEN is set to 0.0.0.0:8001 and container port 8001 is properly exposed
- Konga cannot connect to Kong: Use connection URL http://kong:8001 (internal Docker network name), not localhost:8001
- Kong proxy returns 503 Service Temporarily Unavailable: No upstream services configured yet, add services and routes through Konga or Admin API
- Database migration hangs indefinitely: Check PostgreSQL logs for connection limits or disk space issues, restart kong-database service if needed
- Konga dashboard shows 'Invalid token' errors: Regenerate KONGA_TOKEN_SECRET environment variable and restart konga container
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Components
kongkongapostgres
Tags
#kong#api-gateway#microservices#proxy#rate-limiting
Category
Web Servers & Reverse ProxiesAd Space
Shortcuts: C CopyF FavoriteD Download