2

I am using AWS ECS and have a container for my frontend (Node app) and for my backend (mongo database).

The mongo container is exposing port 27017, but I cannot figure out how to connect to it from my frontend container. If I try to connect to the db using 'mongodb://localhost:27017/db_name' I get an ECONNREFUSED error.

I have a service running for both of these task definitions with an ALB for the frontend. I don't have them in the same task definition because it doesn't seem optimal to have to scale them together.

I have tried multiple variations of the url

  • mongodb://0.0.0.0:27017/db_name
  • mongodb://localhost:27017/db_name

If I "curl" the mongo container from within the EC2 instance, I get an empty reply from server.

Database Task Definition:

{
  "executionRoleArn": null,
  "containerDefinitions": [
    {
      "dnsSearchDomains": null,
      "logConfiguration": null,
      "entryPoint": null,
      "portMappings": [
        {
          "hostPort": 27017,
          "protocol": "tcp",
          "containerPort": 27017
        }
      ],
      "command": null,
      "linuxParameters": null,
      "cpu": 0,
      "environment": [
        {
          "name": "MONGODB_ADMIN_PASS",
          "value": <PASSWORD>
        },
        {
          "name": "MONGODB_APPLICATION_DATABASE",
          "value": <DB NAME>
        },
        {
          "name": "MONGODB_APPLICATION_PASS",
          "value": <PASSWORD>
        },
        {
          "name": "MONGODB_APPLICATION_USER",
          "value": <USERNAME>
        }
      ],
      "ulimits": null,
      "dnsServers": null,
      "mountPoints": [],
      "workingDirectory": null,
      "dockerSecurityOptions": null,
      "memory": null,
      "memoryReservation": 128,
      "volumesFrom": [],
      "image": "registry.hub.docker.com/library/mongo:latest",
      "disableNetworking": null,
      "healthCheck": null,
      "essential": true,
      "links": null,
      "hostname": null,
      "extraHosts": null,
      "user": null,
      "readonlyRootFilesystem": null,
      "dockerLabels": null,
      "privileged": null,
      "name": "mongo"
    }
  ],
  "placementConstraints": [],
  "memory": null,
  "taskRoleArn": null,
  "compatibilities": [
    "EC2"
  ],
  "taskDefinitionArn": "arn:aws:ecs:us-east-2:821819063141:task-definition/dappy_coin_database:2",
  "family": "dappy_coin_database",
  "requiresAttributes": [
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
    }
  ],
  "requiresCompatibilities": null,
  "networkMode": null,
  "cpu": null,
  "revision": 2,
  "status": "ACTIVE",
  "volumes": []
}
connorvo
  • 761
  • 2
  • 7
  • 21
  • 1
    Just because mongo is exposing that port doesn't mean the container mongo runs in is exposing that port. What's your config look like? – Sam H. Mar 30 '18 at 18:52
  • Not sure if this is what you need, but I put the Task Definition JSON that includes mongo container in post – connorvo Mar 30 '18 at 19:00
  • Mongo needs to bind to the correct IP and port, and the container needs to expose that port (and if you were using EC2, that would also need to expose the port). – Nick Mar 30 '18 at 19:27
  • in "docker ps", it shows 0.0.0.0:27017->27017/tcp on the Mongo container. Also, the security groups seem to be working as their is no timeout issue and I exposed the port – connorvo Mar 30 '18 at 19:44
  • Why do you have `http` in the connection URL? Do you have an [http interface running](https://docs.mongodb.com/ecosystem/tools/http-interfaces/#http-interfaces)? From where are you trying to connect to the loopback IP? – Nick Mar 30 '18 at 20:00
  • I don't, I wrote it wrong. I just updated the link. In my app.js, I am using mongoose to connect the localhost URL but that is returning the error – connorvo Mar 30 '18 at 20:03
  • @Nick If it helps, I get an empty reply from the server when I curl the IP of the mongo container but it returns properly when I curl the node container – connorvo Mar 30 '18 at 23:29
  • Have you done everything in this https://node.university/blog/978472/aws-ecs-containers – Sam H. Mar 31 '18 at 18:17
  • That puts them in the same Task Definition which I don't want to do – connorvo Mar 31 '18 at 18:20
  • Can you add your curl and mongodb connect calls, along with the returned messages, to your question? – MrDuk Apr 02 '18 at 16:44
  • Just to confirm, you've verified your security group settings already? – MrDuk Apr 02 '18 at 16:46

1 Answers1

1

OLD:

You have to add, in the task definition for node, which I assume you have: links: ["mongo"] Then you can reference mongo://...

NEW:

Just saw that you want them in separate task definitions. That's a lot of complexity and I want to dissuade you from this path, because you are facing options like: ELB, service discovery via DNS, ambassador container pattern (per this answer - which, if that is all you wanted, this question is a dupe). If you have to do it, see that answer, and weep.

Maybe you would consider deploying your Node app as a single-container Elastic Beanstalk app, and connecting it to MongoDB Atlas? That way you get load balancing, auto-scaling, monitoring, all built in, instead of needing to do it yourself.

Or, at least you could use AWS Fargate. It is a launch mode of ECS that handles more of the infrastructure and networking for you. To quote the docs,

links are not allowed as they are a property of the “bridge” network mode (and are now a legacy feature of Docker). Instead, containers share a network namespace and communicate with each other over the localhost interface. They can be referenced using the following:

localhost/127.0.0.1:<some_port_number>

Where in this case, some_port_number = 27017.

Sam H.
  • 4,091
  • 3
  • 26
  • 34