4

I have two containers added to a task definition

  1. Node Container:

    name :nodeAPI
    port :exposed 5001
    mongoconnection string in the env variable : mongodb://mongo  [name of mongo container]
    
  2. Mongo container:

    name :mongo
    port :exposed 27017
    

The node container is not able to connect to Mongo when I run this task. I am using Fargate and network as awsvpc.

  1. How do I fix this?
  2. How do I make it work running them from separate task definitions?
halfer
  • 19,824
  • 17
  • 99
  • 186
Tanmay Bhattacharya
  • 551
  • 1
  • 5
  • 16
  • Just to clarify, are both containers in the same task definition? If so, you should be able to access the mongo container over localhost. – Roy Kachouh Jul 27 '18 at 19:59
  • Since you are using awsvpc mode, you could try using service discovery provided part of Route 53 to find each other services. https://aws.amazon.com/blogs/aws/amazon-ecs-service-discovery/ – Imran Jul 28 '18 at 16:01
  • Roy ,okay I will try that . I am just new to AWS , I should put the database in as a separate service right ? how do I connect if they are in separate services ? – Tanmay Bhattacharya Jul 29 '18 at 07:55

3 Answers3

2

As every task in Fargate gets it own ENI, therefore correct way to communicate between containers in same task definition is calling local loopback 127.0.0.1 or localhost.

For example:

First Container will be able to communicate to second container using 127.0.0.1:<port of second container> as address and second container can communicate to First container using 127.0.0.1:<port of first container>

This is very well explained in AWS Blog: https://aws.amazon.com/blogs/compute/task-networking-in-aws-fargate/

Mangal
  • 607
  • 4
  • 8
0

there's a security-group configuration when you running the task check whether those are open

fargate security groep

fargate acts like a ec2 onlything is you can use a docker image so you have to do normal ec2 configs

0

If both containers are defined within the same "Task Definition" than they are able to communicate using "localhost".

In your example your NodeJs app will talk with mongo localhost:27017

Filip
  • 63
  • 1
  • 10