duanliujie8639 2019-08-27 11:01
浏览 528
已采纳

如何在Docker容器中调用公共地址时解决拨号TCP I / O超时问题?

I'm setting up a new container 'A' which is calling some endpoints from container 'B'. Why are these calls always return dial tcp 116.203.153.48:8082: i/o timeout?

The call from container 'A' is using public IP. All containers are deployed on the CentOS 7. Every container has own network with own database in this network. Also, the call which returns error works fine from any REST-API client, such Postman. Nameservers in resolv.conf file has been changed to google's 8.8.8.8 and 8.8.4.4

Error: error="Post http://116.203.153.48:8082/new_user?email=eto@email.com: dial tcp 116.203.153.48:8082: i/o timeout"

Call from the program:

req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://116.203.153.48:8082/new_user?email=%s", user.Email), nil)
    if err != nil {
        return err
    }
    httpClient := &http.Client{}
    resp, err := httpClient.Do(req)
    if err != nil {
        return err
    }

UPD:

Docker-compose of the first container:


  payment-ms:
    container_name: payment-ms
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - DB_HOST=payment-ms-db
    ports:
      - 8082:8082

Docker-compose file of the second container:

user-ms:
    container_name: user-ms
    build:
      context: .
      dockerfile: Dockerfile
    environment: 
      - DB_HOST=user-ms-db
    ports: 
      - 8080:8080
    depends_on:
      user-ms-db:
        condition: service_healthy

Also, on my local machine with MacOS everything works fine, problem reproducing only on VPS with CentOS7.

  • 写回答

1条回答 默认 最新

  • douzhaocheng4533 2019-08-28 12:03
    关注

    The problem was triggered by 2 issues. First, containers must be in the same network. And the second one, when containers are in the same network, calls to each other must be with the property container name host. For example:

    $ docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS                              NAMES
    9c6c31b8ec21        user-ms             "./user-ms run"          3 minutes ago       Up 3 minutes             8080/tcp, 0.0.0.0:9980->9980/tcp   user-ms
    13863218f942        finance-ms          "./finance-ms run"       3 minutes ago       Up 3 minutes             0.0.0.0:9982->9982/tcp             finance-ms
    

    That's mean, what curl and all other calls from container user-ms to finance-ms must be with finance-ms:9982 address.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况