MongoError: failed to connect to server [<docker-machine>:<port>] on first connect

MongoError: failed to connect to server [<docker-machine>:<port>] on first connect

正在尝试使用 mongoose 库将 MongoDB 与 replicaSet 连接字符串连接起来。但是没有连接到生产服务器,它给我一个错误:

name: 'MongoError',
 message: 'failed to connect to server [test-machine:27018] on first connect [MongoError: getaddrinfo ENOTFOUND test-machine:27018 test-machine:27018]'   
var mongoose = require('mongoose') //version 4.13.6

var options = { "db": {
        "readPreference": "secondaryPreferred"
    },
    "replset": {
        "rs_name": "testReplica",
    }
}

var connection = mongoose.createConnection(`mongodb://localhost:27018/`,options);

connection.on('open', function () {
    console.log('We are connected')
})

connection.on('error', function (err) {
    console.log('Something went wrong')
    console.log(err)
})

注意:上面的代码工作正常,并显示一条成功消息 in/with 我的本地计算机。

这是一个 DNS 解析错误。

已将 test-machine 添加到我的 /etc/hosts 文件