node.js - 连接到单独的 docker 容器 (AWS ECS) 中的 MongoDB

标签 node.js mongodb amazon-web-services docker amazon-ecs

我正在使用 AWS ECS,并为我的前端(Node 应用程序)和后端(mongo 数据库)提供了一个容器。

mongo 容器公开端口 27017,但我不知道如何从我的前端容器连接到它。如果我尝试使用“mongodb://localhost:27017/db_name”连接到数据库,则会收到 ECONNREFUSED 错误。

我有一个为这两个任务定义运行的服务,并为前端提供了一个 ALB。我没有将它们放在同一个任务定义中,因为将它们一起扩展似乎并不是最佳选择。

我尝试了该网址的多种变体

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

如果我从 EC2 实例中“ curl ”mongo 容器,我会从服务器得到一个空回复。

数据库任务定义:

{
  "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": []
}

最佳答案

旧:

您必须在 Node 的任务定义中添加,我假设您有: links: ["mongo"] 那么你可以引用mongo://...

新:

刚刚看到您希望将它们放在单独的任务定义中。这太复杂了,我想劝阻你不要走这条路,因为你面临着这样的选择:ELB、通过 DNS 的服务发现、大使容器模式(根据 this answer - 如果这就是你想要的,那么这个问题就是一个骗局)。如果你必须这样做,请看到这个答案,然后哭泣。

也许您会考虑将 Node 应用程序部署为单容器 Elastic Beanstalk 应用程序,并将其连接到 MongoDB Atlas ?这样您就可以实现负载平衡、自动扩展、监控,所有这些都是内置的,而无需您自己完成。

或者,至少您可以使用 AWS Fargate 。它是 ECS 的一种启动模式,可以为您处理更多基础设施和网络。引用文档,

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 .

关于node.js - 连接到单独的 docker 容器 (AWS ECS) 中的 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49579796/

相关文章:

javascript - mongodb 更新后插入项目的顺序困惑

node.js - Node 未多次连接到 Mongo

node.js - 事务mongodb

javascript - 在 Express 应用程序的路由中运行 setInterval 函数的正确且非阻塞的方式是什么?

node.js - Angular - 如何创建加载屏幕 'like gmail'

python - 将字符串化列表转换为 python 列表并获取值

java - 如何将 AWS SDK for java 与 Maven 一起使用?

node.js - AWS SDK - 更改自动扩展组更新策略

mongodb - 使用 mongodb 和 redis 的缓存失效策略

mysql - 无法连接到使用 Elastic Beanstalk 创建的 RDS 实例