无法将节点docker容器连接到mongo docker容器

时间:2018-11-13 18:16:13

标签: javascript node.js mongodb docker networking

我收到此错误:

Server running...
{ MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoNetworkError: getaddrinfo EAI_AGAIN mongo mongo:27017]
    at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:562:11)
    at Pool.emit (events.js:182:13)
    at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:316:12)
    at Object.onceWrapper (events.js:273:13)
    at Connection.emit (events.js:182:13)
    at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:245:50)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  name: 'MongoNetworkError',
  message:
   'failed to connect to server [mongo:27017] on first connect [MongoNetworkError: getaddrinfo EAI_AGAIN mongo mongo:27017]',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

这是我的docker-compose.yml

version: '3'
services:
  app:
    container_name: docker-node-mongo
    restart: always
    build: .
    ports:
      - '3001:3000'

这是我尝试从节点脚本进行连接的方法:

mongoose
  .connect(
    'mongodb://127.0.0.1:27017/test2',
    { useNewUrlParser: true }
  )
  .then(() => console.log('MongoDB Connected'))
  .catch(err => console.log("errorErrorError"));

这是docker容器ls -a

的输出
6f14a98e2d51        dockernodemongo_app       "npm start"              11 minutes ago      Up 11 minutes           0.0.0.0:3001->3000/tcp                     docker-node-mongo
f3fb55dad886        mongo                     "docker-entrypoint.s…"   24 minutes ago      Up 24 minutes           127.0.0.1:27017->27017/tcp                 mongo

这是我从另一个docker-compose.yml文件运行mongo的方式:

    mongo:
        restart: always
        image: mongo
        container_name: mongo
        network_mode: "bridge"
#        expose:
#           - 27017
        volumes:
            - ./mongo_data:/data/db
        ports:
            - "127.0.0.1:27017:27017"

1 个答案:

答案 0 :(得分:1)

您无法通过这种方式将应用程序连接到mongo。 127.0.0.1是指容器的本地主机,而不是主机。您有两种选择:

  • 创建一个network并将两个容器都添加到其中。然后使用mongodb://mongo:27017/test2到达mongo容器。 Docker将服务名称用作主机,因此您可以通过该名称访问它。

  • 将mongo绑定到0.0.0.0(从端口声明中省略127.0.0.1),并使用网桥ip地址连接到数据库。

请考虑第一种选择,因为如果没有适当的防火墙,第二种选择会将您的mongo数据库公开到Internet。

例如:

version: '3.5'
services:
  app:
    container_name: docker-node-mongo
    restart: always
    build: .
    ports:
      - '3001:3000'
    networks:
      - mongo

networks:
  mongo:
    external: true

然后在您的mongo撰写文件上:

version: '3.5'
services:
  mongo:
    restart: always
    image: mongo
    container_name: mongo
    expose:
      - 27017
    volumes:
      - ./mongo_data:/data/db
    ports:
      - "127.0.0.1:27017:27017"
    networks:
      - mongo

networks:
  mongo:
    name: mongo

然后在您的代码上:

mongoose
  .connect(
    'mongodb://mongo:27017/test2',
    { useNewUrlParser: true }
  )
  .then(() => console.log('MongoDB Connected'))
  .catch(err => console.log("errorErrorError"));

编辑:更新的网络名称