I'm trying to connect to a mongoDB database inside a docker container but I'm getting this error message : "getaddrinfo ENOTFOUND mongo" and I have no idea what caused it. I'm still new to docker and I can't seem to find a solution to this problem.
Here is my docker-compose file:
version: '3.9'
services:
mongo:
image: mongo:latest
restart: always
container_name: mongodb
ports:
- '27017:27017'
volumes:
- mongodb:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: hazem
MONGO_INITDB_ROOT_PASSWORD: 123
MONGO_INITDB_DATABASE: DB
volumes:
mongodb: {}
and here is the function to connect to the DB :
export const connectDB = async () => {
try {
await mongoose.connect("mongodb://hazem:123@mongo:27017/DB");
console.log("connected to DB");
} catch (error: any) {
console.log(error.message);
// retry to connect after 5 seconds
setTimeout(connectDB, 5000);
}
};