mongoose couldn't connect to mongodb in docker - node.js

mongoose couldn't authenticate with docker mongodb container.
Note: mongo is in docker and my API app out of the docker.
docker-compose:
version: "3.7"
services:
db:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME}"
MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD}"
MONGO_INITDB_DB: "${MONGO_DB}"
ports:
- 27017:27017
volumes:
- mongo_data:/data/db
volumes:
mongo_data:
.env:
MONGO_USERNAME=root
MONGO_PASSWORD=123456
MONGO_DB=nodeApp
db.js (database connection file):
(async () => {
try {
const uri = `mongodb://127.0.0.1:27017/${process.env.MONGO_DB}`;
await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
user: process.env.MONGO_USERNAME,
pass: process.env.MONGO_PASSWORD,
});
console.log("Database connection completed successfully");
} catch (error) {
console.error(error);
}
})();
and finally this is what I got from console:
I can connect to mongo with mongoose with username and password.

Docker only supports using default environment variables to be used like what you did in your code.
https://docs.docker.com/compose/env-file/
Docker added support to env filed since version 1.3 (as I remember). The soloution is to use env_file key in your yml file.
services:
db:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME}"
MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD}"
MONGO_INITDB_DB: "${MONGO_DB}"
ports:
- 27017:27017
volumes:
- mongo_data:/data/db
env_file:
- mongo_variables.env

Instead of localhost use your ip on database configuration.
Replace this DBurl=mongodb://localhost:27017/
to this one DBurl=mongodb://X.X.X.X:27017/ ( X.X.X.X means your ip address )on your database configuration file.
On your docker-compose.yml file
Inside of services of api add CONNECTIONSTRING.
version: "3.7"
services:
api:
build: node-server ## as i am using node
ports:
- 3000:3000
environment:
- CONNECTIONSTRING=mongodb://X.X.X.X:27017/ ##X.X.X.X means your ip address
db:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME}"
MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD}"
MONGO_INITDB_DB: "${MONGO_DB}"
ports:
- 27017:27017
volumes:
- mongo_data:/data/db
volumes:
mongo_data:

you see that in the inspector docker -> port of your container -> (0.0.0.0:27017)
mongodb://0.0.0.0:27017/
;)

Related

PG Pool is not working inside Docker container

I am running a PERN application and currently trying to dockerize it. When I run the database as a container and the server and client locally I have no issues. However, when I containerize the server, client, and database respectively, I am unable to make requests. It results in 404 errors. This is the same behavior that occurs when I pass pool the wrong port or host. So I'm wondering if somehow I am giving the wrong host and/or port to pool or if I should change it when I containerize it.
This is the Pool Instance
const Pool = require('pg').Pool
const pool = new Pool({
user: 'docker',
password: 'docker',
host: "localhost",
port: 4000,
database: "docker"
})
This is part of the rest api in the server:
const express = require("express")
const router = express.Router()
const pool = require('../database/database.js')
router.get("/:login", async (req, res) => {
try {
let loginReq = JSON.parse(decodeURIComponent(req.params.login))
const user = await pool.query(
"SELECT user_id,first_name,last_name,email FROM \"user\" where email = $1 and password = $2",
[loginReq.email, loginReq.password]
)
if(user.rows.length) {
res.json(user.rows[0])
} else {
throw new Error('User Not Found')
}
} catch (err) {
res.status(404).json(err.message)
}
})
This is each of my dockerfiles for my client, server, and database
Client:
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN npm install --production
CMD ["npm","start"]
EXPOSE 3000
Server:
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN npm install --production
CMD ["node","index.js"]
EXPOSE 5000
Database;
FROM postgres:15.1-alpine
COPY init.sql /docker-entrypoint-initdb.d/
This is my docker-compose.yml
version: "3.8"
services:
client:
build: ./client
ports:
- "3000:3000"
restart: unless-stopped
server:
build: ./server
ports:
- "5000:5000"
restart: unless-stopped
database:
build: ./database
ports:
- "4000:4000"
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=docker
- POSTGRES_DB=docker
- PGPORT=4000
volumes:
- kurva:/var/lib/postgresql/data
restart: unless-stopped
volumes:
kurva:
I don't understand why the behavior would be different between containerizing the server and running it locally when they all use the same ports. I have tried messing with the host and changing it to 0.0.0.0 but that did not help. Any help would be appreciated!
I found that it was a host issue and I needed to change the host accessed in the pool to the service name. Additionally, I needed to add that the server depends on the database service.
This is the updated pg pool:
const pool = new Pool({
user: 'docker',
password: 'docker',
host: "database",
port: 4000,
database: "docker"
})
This is the updated docker-compose.yml
version: "3.8"
services:
client:
build: ./client
ports:
- "3000:3000"
restart: unless-stopped
server:
build: ./server
ports:
- "5000:5000"
depends_on:
- database
restart: unless-stopped
database:
build: ./database
ports:
- "4000:4000"
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=docker
- POSTGRES_DB=docker
- PGPORT=4000
volumes:
- kurva:/var/lib/postgresql/data
restart: unless-stopped
volumes:
kurva:

Node.js app not connect to docker redis container

I connected with docker redis container.The redis working in the docker.If I execute the docker file with docker exec -it 96e199a8badf sh, I connected to redis server.
My node.js application like this.I use redis 4.1.0 version.
I don't know, what's going on.How can I fix this?
docker-compose.yml
version: '3'
services:
app:
container_name: delivery-app
build:
dockerfile: 'Dockerfile'
context: .
ports:
- "3000:3000"
volumes:
- .:/app,
- '/app/node_modules'
networks:
- redis
redis:
image: "redis"
ports:
- "6379:6379"
networks:
- redis
networks:
redis:
driver: bridge
Dockerfile
FROM node:16-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . ./
EXPOSE 3000
CMD ["node","server.js"]
code:
const redisClient = redis.createClient({
socket: {
port: 6379,
host: "redis"
}
});
await redisClient.connect();
redisClient.on('connect',function(){
return res.status(200).json({
success: true
})
}).on('error',function(error){
return res.status(400).json({
success: false
})
});
package.json
"redis": "^4.1.0"
I had the same issue and solved it by passing the host parameter as a socket object in createClient like this
{
socket: {
host: "redis"
}
}
This seems to be new because before you didn't have to use the socket object
Here's the documentation for node-redis createClient
https://github.com/redis/node-redis/blob/HEAD/docs/client-configuration.md

connecting to mongodb from a container

I was unable to connect to mongodb container from my node app even with the connection string given from the official documentation
------the docker compose code:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example#mongo:27017/
---connection to mongodb code:
const express = require('express')
const mongoose = require('mongoose')
const Connected = async ()=>{
try{
await mongoose.connect('mongodb://root:example#mongo:27017/test',
{
useNewUrlParser: true,
useUnifiedTopology: true
}
);
console.log("connected!!");
}catch(err){
console.log(err.message);
}
}
Connected();
If you would like to resolve the name like this, you need to set the name as a container name in your compose file. I have attached a working example you can use here:
version: '3.1'
services:
mongo:
image: mongo
restart: always
container_name: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
depends_on:
- mongo
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example#mongo:27017/

Connecting to MongoDB with Mongoose in docker-compose containers fail with useUnifiedTopology

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.

nodejs and mongodb authentication failed

I'm setting up a app with docker, mongodb and nodejs (mongoose), but for some reason I'm getting a authentication error.
UserNotFound: Could not find user "testuserdb" for db "admin"
Any suggestion?
Thanks!
.env
MONGO_USERNAME=testuserdb
MONGO_PASSWORD=testuserpass
MONGO_PORT=27017
MONGO_DB=testdb
db.js (mongoose connection)
const mongoose = require('mongoose');
const {
MONGO_USERNAME,
MONGO_PASSWORD,
MONGO_HOSTNAME,
MONGO_PORT,
MONGO_DB
} = process.env;
const options = {
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
connectTimeoutMS: 10000,
};
const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}#${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`;
mongoose.connect(url, options).then( function() {
console.log('MongoDB is connected');
})
.catch( function(err) {
console.log(err);
});
docker compose
I use a docker environment variables to mongodb and nodejs to define the username, password, hostname db, and databasename
version: '3'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile.dev
depends_on:
- db
image: nodejs
container_name: nodejs
restart: unless-stopped
env_file:
- .env
environment:
- MONGO_USERNAME=$MONGO_USERNAME
- MONGO_PASSWORD=$MONGO_PASSWORD
- MONGO_HOSTNAME=db
- MONGO_PORT=$MONGO_PORT
- MONGO_DB=$MONGO_DB
ports:
- "3000:3000"
volumes:
- .:/home/node/app
- node_modules:/home/node/app/node_modules
networks:
- app-network
#command: ./wait-for.sh db:27017 -- /home/node/app/node_modules/.bin/nodemon app.js
command: /home/node/app/node_modules/.bin/nodemon app.js
db:
image: mongo:4.2.9-bionic
container_name: db
restart: unless-stopped
env_file:
- .env
environment:
- MONGO_INITDB_ROOT_USERNAME=$MONGO_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASSWORD
- MONGO_INITDB_DATABASE=$MONGO_DB
volumes:
- dbdata:/data/db
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
dbdata:
node_modules:
As per the information available. I think the user testuserdb exist on testdb and not on admin database.
And while defining connection url you have mentioned authSource=admin
Change one of these as per your needs.

Resources