Reputation: 821
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
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": []
}
Upvotes: 2
Views: 5737
Reputation: 4349
OLD:
You have to add, in the task definition for node, which I assume you have:
links: ["mongo"]
Then you can referencemongo://...
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
.
Upvotes: 1