I have a NodeJS Application that connects to a MongoDB server.
Both the node application and MongoDB server are served in a docker container (with docker-compose
)
docker-compose.yml
:
version: '3'
services:
redis:
image: "redis:alpine"
ports:
- "6379:6379"
expose:
- 6379
restart:
always
container_name: redis-server
mongo:
image: "mongo"
command: mongod --bind_ip_all --replSet rs8
volumes:
- c:\mongo\data:/data/db
ports:
- "27017:27017"
expose:
- 27017
restart:
always
container_name: mongo-server
accounts-service:
depends_on:
- redis
- mongo
build:
context: .
dockerfile: GenericNodeJSDockerfile
container_name: accounts-service
ports:
- "8001:8001"
In the node app, the connection in mongo looks like this:
const m = require('mongoose') const connectionConfig = { useUnifiedTopology: true, useNewUrlParser: true, }; let connectionString = 'mongodb://mongo:27017/mydb/replicaSet=rs8'; m.connect(connectionString, connectionConfig).then(_ => { console.log("Connected to MongoDB") }).catch(err => { console.log("Failed connecting to MongoDB: " + err); });
And the error that is thrown is:
Failed connecting to MongoDB: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
But when I'm commenting the row contains useUnifiedTopology
option, the connection succeeds.
Does anyone have an idea how to fix it?
It turns out that I had a wrong hosts
file configuration that made this issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.