Docker-compose无法进行跨容器请求

时间:2017-07-11 14:52:13

标签: docker-compose kong

我在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的名称作为上游网络的主机名。将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 exec it <container-id> /bin/bash的docker容器,并尝试向预期的flask终点发出curl请求。在运行API的容器上,我能够对localhost:5002/employees以及employeedb:5002/employees进行成功调用。然而,当从运行孔的容器制作时,我看到了

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

我是否遗漏了某种将容器彼此暴露的配置?

1 个答案:

答案 0 :(得分:2)

您需要像使用PostgreSQL数据库一样定义employeedb来使link容器对kong可见。只需将其添加为- postgres正下方的附加条目,即可通过Kong访问:

....
links:
  - postgres
  - employeedb

....