I'm new to Docker and struggling to use it with mongo and mongo_express. I found many StackOverflow posts about this issue, but none of them solved the problem I'm dealing with.
I'm making a website that requires a user login function, and usernames and passwords are supposed to be stored on mongod. However, it seems like my database server (node.js express) is not connected to the database that is located in another container. I think this is the reason why I receive this (MongooseServerSelectionError: getaddrinfo ENOTFOUND MongoDB).
Here is my docker-compose.yaml file, and express is node server, mongo is database, and mongo_express is a database management tool.
services:
express:
container_name: video-express
image: video-express:latest
build:
context: ./express
dockerfile: Dockerfile.express
ports:
- "13000:3000"
volumes:
- video_express_node:/app/node_modules
- ./express:/app
tty: true
stdin_open: true
depends_on:
- mongo
networks:
- default
mongo:
image: mongo:latest
restart: always
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: q-cards-db
volumes:
- ./data/db:/database
networks:
- default
- mongo-express
mongo_express:
image: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: password
ME_CONFIG_MONGODB_SERVER: mongo
# ME_CONFIG_BASICAUTH_USERNAME: admin
# ME_CONFIG_BASICAUTH_PASSWORD: Password1
depends_on:
- mongo
networks:
- mongo-express
dev:
container_name: video-dev
image: video-dev:latest
build:
context: .
dockerfile: Dockerfile
ports:
- "15173:5173"
volumes:
- video_dev_node_modules:/app/node_modules
- .:/app
depends_on:
- express
tty: true
stdin_open: true
networks:
- default
networks:
default:
driver: bridge
mongo-express:
driver: bridge
volumes:
video_dev_node_modules:
video_express_node:
And here is the connection part of server.js file.
mongoose.connect("mongodb://root:pass@mongodb:27017/videoUserDB", {
useNewUrlParser: true
});
If I connect to http://0.0.0.0:8081/db/admin/, I can open mongo-express page, so I assume the problem is the connection between server.js and mongod.
These are error messages I see now. There are some I cannot understand, but if you can identify the problem by these, that would be great.
(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is
deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
video-management-mongo_express-1 | Could not connect to database using connectionString: mongodb://root:password@mongo:27017/"
video-management-mongo_express-1 | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: connect ECONNREFUSED 172.19.0.2:27017
video-management-mongo_express-1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
video-management-mongo_express-1 | name: 'MongoNetworkError'
video-management-mongo_express-1 | }]
video-management-mongo_express-1 | at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
video-management-mongo_express-1 | Mongo Express server listening at http://0.0.0.0:8081
video-management-mongo_express-1 | Server is open to allow connections from anyone (0.0.0.0)
video-express | /app/node_modules/mongoose/lib/connection.js:824
video-express | const serverSelectionError = new ServerSelectionError();
video-express | ^
video-express |
video-express | MongooseServerSelectionError: getaddrinfo ENOTFOUND mongodb
video-express | at Connection.openUri (/app/node_modules/mongoose/lib/connection.js:824:32)
video-express | at /app/node_modules/mongoose/lib/index.js:381:10
video-express | at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
video-express | at new Promise (<anonymous>)
video-express | at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
video-express | at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1234:10)
video-express | at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:380:20)
video-express | at Object.<anonymous> (/app/server.js:50:10)
video-express | at Module._compile (node:internal/modules/cjs/loader:1159:14)
video-express | at Module._extensions..js (node:internal/modules/cjs/loader:1213:10) {
video-express | reason: TopologyDescription {
video-express | type: 'Unknown',
video-express | servers: Map(1) {
video-express | 'mongodb:27017' => ServerDescription {
video-express | address: 'mongodb:27017',
video-express | type: 'Unknown',