Docker-compose 无法发出跨容器请求

Docker-compose unable to make cross container requests

我在 docker-compose 中遇到网络问题 运行 服务。本质上,我只是想通过 Kong 向我已设置的简单 Flask API 发出获取请求。 docker-compose.yml 如下 版本:“3.0”

services:
  postgres:
    image: postgres:9.4
    container_name: kong-database
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=kong
      - POSTGRES_DB=kong

  web:
    image: kong:latest
    container_name: kong
    environment:
      - DATABASE=postgres
      - KONG_PG_HOST=postgres
    restart: always
    ports:
      - "8000:8000"
      - "443:8443"
      - "8001:8001"
      - "7946:7946"
      - "7946:7946/udp"
    links:
      - postgres

  ui:
    image: pgbi/kong-dashboard
    container_name: kong-dashboard
    ports:
      - "8080:8080"
  employeedb:
    build: test-api/
    restart: always
    ports:
       - "5002:5002"

我用命令curl -i -X POST --url http://localhost:8001/apis/ --data name=employeedb --data upstream_url=http://localhost:5002 --data hosts=employeedb --data uris=/employees将API添加到kong。我尝试了多种输入组合,包括不同的名称,传入 Docker 网络 IP 和 test-api 的名称作为 upstreamurl 的主机名。将 API 添加到 Kong 后,我得到

HTTP/1.1 502 Bad Gateway
Date: Tue, 11 Jul 2017 14:17:17 GMT
Content-Type: text/plain; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/0.10.3

此外,我已经进入 docker 容器 运行 docker exec it <container-id> /bin/bash 并尝试向预期的烧瓶端点发出 curl 请求。在容器 运行 和 API 上,我能够成功调用 localhost:5002/employeesemployeedb:5002/employees。然而,当从容器 运行 Kong 制作时,我看到

curl -iv -X GET --url 'http://employeedb:5002/employees'
* About to connect() to employeedb port 5002 (#0)
*   Trying X.X.X.X...
* Connection refused
* Failed connect to employeedb:5002; Connection refused
* Closing connection 0

我是否缺少某种将容器相互公开的配置?

您需要通过定义 link 来使 employeedb 容器对 kong 可见,就像您对 PostgreSQL 数据库所做的那样。只需将其作为附加条目添加到 - postgres 正下方,Kong 应该可以访问它:

....
links:
  - postgres
  - employeedb

....