Raghav
Raghav

Reputation: 21

AWS-ECS - Communication between containers - Unknown host error

I have two Docker containers.

  1. TestWeb (Expose: 80)
  2. TestAPI (Expose: 80)

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

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:

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

Code:

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;
            }
}

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.

Upvotes: 2

Views: 2348

Answers (2)

Elijah Lynn
Elijah Lynn

Reputation: 13468

It appears ECS doesn't use legit docker-compose under the hood, however, their implementation does support the Compose V2 "links" feature.

Here is a portion of my compose file I just ran on ECS that needed this same functionality AND had the same "could not resolve host" error you were getting. The "links" I added fixed my hostname resolution issue on Elastic Container Service!

version: '3'
services:
  appserver:
    links:
      - database:database
      - socks-proxy:socks-proxy

This allowed my appserver to communicate TO the database and socks-proxy hostnames. The format is "SERVICE:ALIAS" and it is fine to keep them both the same as a default practice.

In your example it would be:

version: '3'
services:
  testapi:
    links:
      - testweb:testweb
  testweb:
    links:
      - testapi:testapi

Upvotes: 3

Raghav
Raghav

Reputation: 21

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).

Upvotes: 0

Related Questions