且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在单独的Docker容器(AWS ECS)中连接到MongoDB

更新时间:2022-05-31 10:12:00

OLD:

您必须在节点的任务定义中添加以下内容:links: ["mongo"] 然后您可以引用mongo://...

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

新:

刚刚看到您想要它们在单独的任务定义中.这非常复杂,我想劝阻您不要走这条路,因为您面临的选择包括:ELB,通过DNS进行服务发现,大使容器模式(每个

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.

也许您会考虑将Node应用程序部署为单个容器的Elastic Beanstalk应用程序,并将其连接到 MongoDB地图集?这样,您就可以实现负载平衡,自动扩展,监视等所有内置功能,而无需自己进行操作.

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.

或者,至少您可以使用AWS Fargate .这是ECS的启动模式,可为您处理更多基础架构和网络.要引用文档,

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,因为它们是网桥"网络模式的属性(现在是Docker的遗留功能).相反,容器共享网络名称空间,并通过localhost接口相互通信.可以使用以下内容引用它们:

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>

在这种情况下为some_port_number = 27017.