阅读背景:

AWS-ECS - 容器之间的通信 - 未知主机错误

来源:互联网 

I have two Docker containers.

我有两个Docker容器。

  1. TestWeb (Expose: 80)
  2. TestWeb(曝光:80)

  3. TestAPI (Expose: 80)
  4. TestAPI(曝光:80)

Testweb container calls TestApi container. Host can communicate with TestWeb container from port 8080. Host can communicate with TestApi using 8081.

Testweb容器调用TestApi容器。主机可以从端口8080与TestWeb容器通信。主机可以使用8081与TestApi通信。

I can get TestWeb to call TestApi in my dev box (Windows 10) but when I deploy the code to AWS (ECS) I get "unknown host" exception. Both the containers work just fine and I can call them individually. But when I call a method that internally makes a Rest call using HttpClient to a method in Container2, it gives the error:

我可以让TestWeb在我的开发箱中调用TestApi(Windows 10)但是当我将代码部署到AWS(ECS)时,我得到“未知主机”异常。两个容器都工作正常,我可以单独调用它们。但是当我调用一个方法在内部使用HttpClient对一个方法在Container2中进行Rest调用时,它会给出错误:

An error occurred while sending the request. ---> System.Net.Http.CurlException: Couldn't resolve host name.

发送请求时发生错误。 ---> System.Net.Http.CurlException:无法解析主机名。

Code:

using (var client = new HttpClient())
        {
            try
            {
                string url = "https://testapi/api/Tenant/?i=" + id;
                var response = client.GetAsync(url).Result;
                if (response.IsSuccessStatusCode)
                {
                    var responseContent = response.Content;
                    string responseString = responseContent.ReadAsStringAsync().Result;
                    return responseString;
                }
                return response.StatusCode.ToString();
            }
            catch (HttpRequestException httpRequestException)
            {
                return httpRequestException.Message;
            }
}

The following are the things I have tried:

以下是我尝试过的事情:

The two containers (TestWeb, TestAPI) are in the same Task definition in AWS ECS. When I inspect the containers, I get the IP address of each of the containers. I can ping container2 from container1 with their IP address. But I can't ping using container2 name. It gives me "unknown host" error.

两个容器(TestWeb,TestAPI)位于AWS ECS中的相同任务定义中。当我检查容器时,我得到每个容器的IP地址。我可以使用他们的IP地址从container1 ping容器2。但我无法ping使用container2名称。它给了我“未知主机”错误。

1 个解决方案

#1


0  

AWS does not use Docker compose but provides a interface to add Task Definitions. Containers that need to communicate together can be put on the same Task definition. Then we can also specify in the links section the containers that will be called from the current container. Each container can be given its container name on the "Host" section of Task Definition. Once I added the container name to the "Host" field, Container1 (TestWeb) was able to communicate with Container2 (TestAPI).

AWS不使用Docker compose,但提供了添加任务定义的接口。需要一起通信的容器可以放在相同的任务定义中。然后我们还可以在links部分中指定将从当前容器调用的容器。可以在任务定义的“主机”部分为每个容器指定其容器名称。有一次,我增加了容器名称为“主机”字段,Container1(TestWeb)能够与container2的(TestAPI)进行通信。


分享到: