Docker Compose for Keycloak + PostgreSQL

Keycloak identity and access management with PostgreSQL. Enterprise SSO and OAuth.

keycloak auth sso postgres identity
compose.yaml
services:
  keycloak:
    image: quay.io/keycloak/keycloak:latest
    command: start
    ports:
      - target: 8080
        mode: ingress
    environment:
      - KC_DB=postgres
      - KC_DB_URL_HOST=db
      - KC_DB_URL_DATABASE=keycloak
      - KC_DB_USERNAME=postgres
      - KC_DB_PASSWORD=${KC_DB_PASSWORD}
      - KEYCLOAK_ADMIN=admin
      - KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_ADMIN_PASSWORD}
      - KC_HOSTNAME_STRICT=false
      - KC_HTTP_ENABLED=true
    healthcheck:
      test:
        - CMD
        - curl
        - -f
        - http://localhost:8080/
      interval: 30s
      timeout: 5s
      retries: 3
    depends_on:
      - db
    deploy:
      resources:
        reservations:
          cpus: "1"
          memory: 1G
    restart: unless-stopped
  db:
    image: postgres:16
    ports:
      - target: 5432
        mode: host
    environment:
      - POSTGRES_DB=keycloak
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    healthcheck:
      test:
        - CMD-SHELL
        - pg_isready -U postgres
      interval: 10s
      timeout: 5s
      retries: 5
    deploy:
      resources:
        reservations:
          cpus: "0.5"
          memory: 256M
    restart: unless-stopped
    x-defang-postgres: true

Services

  • keycloak quay.io/keycloak/keycloak:latest
  • db postgres:16

Deploy

$ npx defang@latest compose up

Learn more about Defang