Kong with Docker ( Docker Compose )

basic install kong with docker-compose

  • Install Postgres database
$ docker run -d --name kong-database \
-e POSTGRES_PASSWORD=secret \
-p 5432:5432 \
postgres
  • Create database
$ docker exec -it kong-database psql --username postgres --command "CREATE DATABASE kong_app;"

or เพิ่ม -e POSTGRES_DB=kong ก็ได้

$ docker run -d --name kong-postgres \
-e POSTGRES_DB=kong \
--network=kong-net \
-e POSTGRES_PASSWORD=secret \
-p 5432:5432 \
postgres
  • Create network
$ docker network create kong-net

  • docker-compose.yml
version: '3'

services:
  migrations:
    image:  kong:latest
    container_name: kong-migrations
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: <ip host database>
      KONG_PG_USER: postgres
      KONG_PG_PASSWORD: secret
      KONG_PG_PORT: 5432
      KONG_PG_SCHEMA: public
      KONG_PG_DATABASE: kong
    networks:
      - kong-net
    command: kong migrations bootstrap

  kong:
    image: kong:latest
    container_name: kong
    restart: always
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: <ip host database>
      KONG_PG_USER: postgres
      KONG_PG_PASSWORD: secret
      KONG_PG_PORT: 5432
      KONG_PG_SCHEMA: public
      KONG_PG_DATABASE: kong
      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, 0.0.0.0:8444 ssl
    networks:
      - kong-net
    ports:
      - 80:8000
      - 8001:8001
      - 39021:8001
      - 39424:8444
networks:
  kong-net:
  • ส่วน migration
migrations:
    image:  kong:latest
    container_name: kong-migrations # ชื่อ container
    environment:
      KONG_DATABASE: postgres 
      KONG_PG_HOST: 127.0.0.1
      KONG_PG_USER: postgres
      KONG_PG_PASSWORD: secret
      KONG_PG_PORT: 5432
      KONG_PG_SCHEMA: public
      KONG_PG_DATABASE: kong
    networks: 
      - kong-net # network ที่เราสร้าง
    command: kong migrations bootstrap # สั่งให้ทำคำสั่งนี้หลัง start container
$ docker-compose up migrations
  • ส่วน kong app
kong:
    image: kong:latest
    container_name: kong
    restart: always
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: 127.0.0.1
      KONG_PG_USER: postgres
      KONG_PG_PASSWORD: secret
      KONG_PG_PORT: 5432
      KONG_PG_SCHEMA: public
      KONG_PG_DATABASE: kong
      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, 0.0.0.0:8444 ssl # port สำหรับ จัดการ
    networks:
      - kong-net
    ports:
      - 80:8000
      - 8001:8001
      - 39021:8001
      - 39424:8444
$ docker-compose up -d kong