将 kong.yml 中的环境变量与 Kong 和 Docker-compose 一起使用

Spi*_*der 7 docker docker-compose kong

我正在尝试通过 kong.yml 文件以声明方式使用 Kong。我希望能够在 kong.yml 文件中使用环境变量。我的 docker-compose 中有以下内容:

kong: 
    image: kong:2.7.0-alpine
    restart: "always"
    hostname: kong
    container_name: kong
    networks:
      - kong-net
    environment:
      KONG_DATABASE: "off"
      KONG_PROXY_ACCESS_LOG: /dev/stdout
      KONG_PROXY_ERROR_LOG: /dev/stderr
      KONG_ADMIN_ACCESS_LOG: /dev/stdout
      KONG_ADMIN_ERROR_LOG: /dev/stderr
      KONG_ADMIN_LISTEN: "0.0.0.0:8001, 0.0.0.0:8444 ssl"
      KONG_DECLARATIVE_CONFIG: "/opt/kong/kong.yml"
      ADMIN_MICROSERVICE_PORT: ${ADMIN_MICROSERVICE_PORT}
      REPORTING_MICROSERVICE_PORT: ${REPORTING_MICROSERVICE_PORT}
      INTERNAL_IP: ${INTERNAL_IP}
    command: "kong start --v"
    ports: 
      - "${INTERNAL_IP}:8000:8000"
      - "${INTERNAL_IP}:8443:8443"
      - "${INTERNAL_IP}:8001:8001"
      - "${INTERNAL_IP}:8444:8444"
    volumes:
      - ./kong/config:/opt/kong
Run Code Online (Sandbox Code Playgroud)

还有一个 kong.yml 文件:

_format_version: "2.1"
_transform: true


services:
- name: controllers
  url: https://${INTERNAL_IP}:${ADMIN_MICROSERVICE_PORT}
  routes:
  - name: controller-routes
    paths:
    - /admin/controllers 

- name: vpnprofiles
  url: https://${INTERNAL_IP}:${ADMIN_MICROSERVICE_PORT}
  routes:
  - name: vpnprofiles-routes
    paths:
    - /admin/vpnprofiles

- name: device_report
  url: https://${INTERNAL_IP}:${REPORTING_MICROSERVICE_PORT}
  routes:
  - name: device-report-routes
    paths:
    - /reporting/device_report
Run Code Online (Sandbox Code Playgroud)

以下是 kong 镜像的日志:

022/01/01 12:38:46 [error] 1095#0: init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:551: error parsing declarative config file /opt/kong/kong.yml:
in 'services':
  - in entry 1 of 'services':
    in 'host': invalid value: ${INTERNAL_IP}
    in 'port': expected an integer
  - in entry 2 of 'services':
    in 'host': invalid value: ${INTERNAL_IP}
    in 'port': expected an integer
  - in entry 3 of 'services':
    in 'host': invalid value: ${INTERNAL_IP}
    in 'port': expected an integer
stack traceback:
    [C]: in function 'error'
    /usr/local/share/lua/5.1/kong/init.lua:551: in function 'init'
    init_by_lua:3: in main chunk
Run Code Online (Sandbox Code Playgroud)

但是,kong.yml 中的变量在启动时不会被替换。难道我做错了什么 ?