我有两个 Docker 容器。
- 测试网(曝光:80)
- 测试API(公开:80)
Testweb 容器调用 TestApi 容器。Host 可以通过 8080 端口与 TestWeb 容器通信。Host 可以使用 8081 与 TestApi 通信。
我可以让 TestWeb 在我的开发箱(Windows 10)中调用 TestApi,但是当我将代码部署到 AWS(ECS)时,我得到“未知主机”异常。这两个容器都工作得很好,我可以单独调用它们。但是当我调用一个内部使用 HttpClient 对 Container2 中的方法进行 Rest 调用的方法时,它会给出错误:
发送请求时出错。---> System.Net.Http.CurlException:无法解析主机名。
代码:
using (var client = new HttpClient())
{
try
{
string url = "http://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;
}
}
以下是我尝试过的事情:
两个容器(TestWeb、TestAPI)在 AWS ECS 中的同一个任务定义中。当我检查容器时,我会得到每个容器的 IP 地址。我可以用他们的 IP 地址从 container1 ping container2。但我无法使用 container2 名称 ping。它给了我“未知主机”错误。