46

I try to setup a little example on AWS Fargate and try to have one application container with one database container there.

The task definition starts both containers, but my app container fails with getaddrinfo ENOTFOUND db db:3306. Therefore, the database container could not be found by my app container.

Since Fargate does not allow "links" of Docker containers, i'd like to know how they can communicate with each other. The database container has the name db with port 3306 and my app container is configured to use this.

Unfortunately, defining links of containers is not available for Fargate, but only for "oldschool" ECS/EC2.

In a local docker-compose setup it is working this way.

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
sebastian
  • 813
  • 1
  • 7
  • 14
  • But what was the change that you did to resolve your issue? Based on the answer you accepted, nothing is required for you to change. – OK999 Aug 05 '18 at 18:20
  • 3
    In my deployed app, I tried to reference the DB as "db:3306". When I deploy the app and db, and reference the db with "localhost:3306" (instead of the service name) it works. – sebastian Aug 08 '18 at 11:19

3 Answers3

73

Containers in Fargate tasks share a network namespace, so you don't need to use links at all. You can simply communicate via localhost.

For example, if you have container A running a web server on port 8000, container B could reach it with curl http://localhost:8000/

Noah
  • 1,051
  • 7
  • 5
  • Thanks, very useful! – Gonzalo Bahamondez Feb 13 '18 at 02:36
  • @Noah how would you go with services A and B both listening on 8080 internally, e.g. 2 spring boot projects – toske Apr 09 '18 at 08:53
  • @Noah How do you specify this in cloudFormation? – Sangam Belose Apr 20 '18 at 13:15
  • 1
    @toske You'll need to move one of the services to a different port. Or, in theory, if you want one of them exposed to localhost, and the other exposed to the network, you could explicitly bind one to 127.0.0.1 and the other to a different address. – Noah Apr 21 '18 at 15:51
  • 1
    @SangamBelose You shouldn't need to specify anything in cloudformation for this. 'awsvpc' mode, whether in ECS or Fargate, implies a shared network namespace (e.g. shared localhost) – Noah Apr 21 '18 at 15:52
  • What would need to be done to set up the tasks running on different ports? - would this be done via the Port Mappings "portMappings": [ { "hostPort": 80, "protocol": "tcp", "containerPort": 8080 } on the Task Definition? – fuzzi Oct 18 '18 at 20:02
  • 3
    More information from AWS - https://aws.amazon.com/blogs/compute/task-networking-in-aws-fargate/ – Son Lam Mar 25 '19 at 02:36
2

Now you can use AWS Cloud Map service discovery integration with Amazon ECS https://docs.aws.amazon.com/AmazonECS/latest/bestpracticesguide/networking-connecting-services.html With this service you will be able to call other container via DNS name

Dr.Crazy
  • 1,623
  • 1
  • 7
  • 8
-4

You can probably set it up so the services can communicate over the public IP address with each other, then secure the ports using a VPC only the services can communicate with themselves on.

  • 2
    And how do you know the public IP address when building the container? You maybe could do it by assigning an EIP first, then attaching it to Fargate. I don't think you can... Using localhost is much easier! – Max Allan Jun 29 '19 at 13:49