Search code examples
amazon-web-servicesdockeramazon-ec2amazon-ecsaws-fargate

containers can communicate with ports? ECS on EC2


I am moving my project from ECS on fargate to ECS on EC2

Now I have some troubles containers comunication.

f91bc9ec2092   678100221111.dkr.ecr.ap-northeast-1.amazonaws.com/ol-dev-django:latest   "./entrypoint.fargat…"   11 seconds ago   Up 9 seconds              0.0.0.0:8011->8011/tcp, :::8011->8011/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   ecs-oldevecsTaskDefinition4BE5F675-24-ol-django-container-bef283fff2ff8d876f00
654e3b4370f6   678100221111.dkr.ecr.ap-northeast-1.amazonaws.com/ol-dev-nginx:latest    "/docker-entrypoint.…"   11 seconds ago   Up 9 seconds              0.0.0.0:80->80/tcp, :::80->80/tcp                                                      ecs-oldevecsTaskDefinition4BE5F675-24-ol-nginx-container-d0bda396a09d84e6d601

From EC2 Node, I can see two containers are working.

then I login the container with EC2 node.

$docker exec -it 654e3b4370f6 /bin/bash` #login nginx container.

then in the container shell.

root@654e3b4370f6:/#curl localhost:8011` #connect to django container

However it says curl: (7) Failed to connect to localhost port 8011: Connection refused

WHy this happens ?

When I am using Fargate, containers can contact each other with this methods.

I can not use port on ECS on EC2?


Solution

  • AWS Fargate only supports the awsvpc Network mode for ECS tasks. In awsvpc Network mode containers can communicate with each other over localhost.

    EC2 supports several network modes for ECS tasks, and default to bridge network mode. If you want your containers to be able to communicate with each other over localhost and otherwise behave the same that they did when they were running on Fargate, then you need to configure your ECS deployed tasks to use awsvpc Network mode.