代码之家  ›  专栏  ›  技术社区  ›  William Hammond

Docker compose无法发出跨容器请求

  •  1
  • William Hammond  · 技术社区  · 7 年前

    我在运行服务时遇到网络问题 docker-compose 版本:“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"
    

    我使用命令将API添加到kong curl -i -X POST --url http://localhost:8001/apis/ --data name=employeedb --data upstream_url=http://localhost:5002 --data hosts=employeedb --data uris=/employees .我尝试了许多输入组合,包括不同的名称,传入Docker网络IP和测试api的名称作为上游URL的主机名。在将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/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 回复  |  直到 7 年前
        1
  •  2
  •   JSchirrmacher    7 年前

    employeedb 通过定义 link 就像你对PostgreSQL数据库所做的那样。只需将其作为一个附加条目直接添加到下面即可 - postgres 孔应该可以到达:

    ....
    links:
      - postgres
      - employeedb
    
    ....