'How to link mongo with node using docker compose
I'm trying link my node server with my db in Mongo, but doesn't work...
This is my Docker-Compose.yml:
version: '3.9'
services:
human:
build:
context: ./human-app
container_name: human
ports:
- "3001:3001"
mongodb:
image: mongo
container_name: mongodb
env_file:
- ./human-app/srcs/.env
environment:
- MONGO_INITDB_ROOT_USERNAME=Pepe
- MONGO_INITDB_ROOT_PASSWORD=PepePass
- MONGO_INITDB_DATABASE=Pool
ports:
- "27017:27017"
volumes:
- ./volumes_mongo:/data/db
And when i run the command:
docker-compose up -d
Everything work correctly but separate, i check the status with the command "docker ps -a" and the result is this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9335c5cd4940 mongo "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:27017->27017/tcp mongodb
2a28635d2caa human-selection-app_human "node server" 4 seconds ago Up 3 seconds 0.0.0.0:3001->3001/tcp human
If i enter in my container of mongo, and follow the next steps, thats happen:
1- docker exec -it mongodb bash 2- mongo 3- show dbs "result no dbs"
So, i can't link nothing with my node server, probably i need create a network for the 2 services? can anyone help me? thanks.
Extra, this is how i connect from my server to MongoDB:
async _connectDB() {
//! We create the connection to the database
const dbUser = process.env.DBUSER;;
const dbPassword = process.env.DBPASSWORD;
const dbName = process.env.DBNAME;
const dbUriLocal = `mongodb://${dbUser}:${dbPassword}@127.0.0.1:27017/${dbName}`;
mongoose.connect(dbUriLocal, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log(clc.cyan('Debug: Database connected successfully')))
.catch(e => console.log(e));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
