docker/nodejs/mongodb deployment : MongooseError [MongooseServerSelectionError]: connection timed out - node.js

update 2
I face another error,
if I use path as follows, I will get "invalid volume specification" error
even change path to "C:/docker-entrypoint-initdb.d/", the init-mongo.sh will not be executed, then I get "UserNotFound: Could not find user "xxxx" for db "admin"
volumes:
- ./init-mongo/:/docker-entrypoint-initdb.d/
- dbdata:/data/db
so
I switch to linux container, and all error message are gone.
update
I've solved my problem after disable mongodb service on local host
net stop MongoDB
and use port 27017
mongodb:
....
ports:
- "27017:27017"
.....
but I would like to know why I cant change port 27017 to others?
I change the port because if mongodb is running on my computer (using 27017 port), I will get the following error message, thank you.
for mongodb Cannot start service mongodb: failed to create endpoint mongodb on network server_app_network: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)
original
I'm using docker windows container on windows 10.
After struggling for two days, I still cant deploy my project successfully.
my configuration is as follows, thanks for help ..
server.js
const dbUrl = "mongodb://mongodb:27018/";
mongoose
.connect(`${dbUrl}mydb`, { useUnifiedTopology: true, useNewUrlParser: true })
.then(res => {
db = mongoose.connection;
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
})
.catch(error => console.log(error));
docker-compose.yml
version: "3"
services:
express-server:
build:
context: .
dockerfile: Dockerfile
image: express-server
container_name: express-server
restart: unless-stopped
ports:
- "3000:3000"
links:
- mongodb
depends_on:
- mongodb
networks:
- app_network
mongodb:
image: mongo
container_name: mongodb
restart: unless-stopped
ports:
- "27018:27018"
networks:
- app_network
volumes:
dbdata:
networks:
app_network:
driver: nat
after running docker-compose up --build -d
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7fac2380c803 express-server "node server.js" 57 seconds ago Up 43 seconds 0.0.0.0:3000->3000/tcp express-server
9afc21569a1a mongo "mongod --bind_ip_all" About a minute ago Up 57 seconds 27017/tcp, 0.0.0.0:27018->27018/tcp mongodb

I think you need to pass dbName in configuration.
mongoose.connect(
connectionString,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
dbName: 'database-name', // IMPORTANT TO HAVE IT HERE AND NOT IN CONNECTION STRING
},
err => { throw err; },
);

I had a lot of trouble to solve this error, finally I realized that the error is related to docker network, I have shared a little information to solve this error in the link

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:

ECONNREFUSED Bitnami mongo image

I'm trying to connect my app to a db running with a bitnami image of mongo, I have the docker-compose with the following rules:
version: '3.9'
services:
# Database service
mongodb:
image: 'bitnami/mongodb:5.0.8'
container_name: mongodb
restart: on-failure
environment:
- MONGODB_ROOT_USER=${MONGODB_ROOT_USER}
- MONGODB_ROOT_PASSWORD=${MONGODB_ROOT_PASSWORD}
- MONGODB_USERNAME=${MONGODB_USERNAME}
- MONGODB_PASSWORD=${MONGODB_PASSWORD}
- MONGODB_DATABASE=${MONGODB_DATABASE}
ports:
- "27017:27017"
networks:
- honey-net
# Application service
r4i2:
depends_on:
- mongodb
build:
context: ./r4i2_app
env_file:
- .env
container_name: r4i2
ports:
- "${PORT}:${PORT}"
volumes:
- ./r4i2_app/app_srcs/public/:/r4i2/public/
networks:
- honey-net
networks:
honey-net:
driver: bridge
Is in the same net and also have the environment variables for start the db but I get this error:
MongooseServerSelectionError: connect ECONNREFUSED 172.20.0.2:27017
In the other hand I also have the js file to connect to the db:
require('dotenv').config();
const mongoose = require('mongoose')
const clc = require('cli-color');
const connectDB = () => {
const MONGODB_USERNAME=process.env.MONGODB_USERNAME;
const MONGODB_PASSWORD=process.env.MONGODB_PASSWORD;
const MONGODB_DATABASE=process.env.MONGODB_DATABASE;
const dbUri = `mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}#mongodb:27017/${MONGODB_DATABASE}`;
mongoose.connect(dbUri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log(clc.cyanBright('Debug: Database connected successfully')))
.catch(e => console.log(e));
}
module.exports = { connectDB }
I just wanna connect my app to my db in localhost using docker-compose.

Cannot connect node js app to mongodb running in a docker swarm

I am trying to deploy nodejs app in docker swarm. However I'm struggling to make it connect to the mongodb service.
Below is my docker-compose file
version: '3'
services:
moitrack-teltonika-parser:
build: .
image: moitrack/moitrack-teltonika-parser:1.0.14
ports:
- "5001:5001"
networks:
- web
deploy:
mode: replicated
replicas: 2
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
depends_on:
- db
db:
image: mongo
command: mongod --bind_ip_all
ports:
- "27017"
volumes:
- mongodata:/data/db
restart: "always"
deploy:
placement:
constraints: [node.role == manager]
networks:
- web
volumes:
mongodata: {}
networks:
web:
external: true
This is the node js script
var express = require("express");
const mongoose = require('mongoose');
var server = express();
const options = {
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
connectTimeoutMS: 10000,
};
const DB_URI = 'mongodb://db:27017/moiponeGateKeeper';
mongoose.connect(DB_URI, options).then(() => {
console.log("database connected");
}).catch(err => console.log(err));
server.listen(5001, () => { console.log("Server started"); });
I get the error below when I view the node service in the docker swarm.
enter image description here
'mongodb://db:27017/moiponeGateKeeper' is not a valid url,
You should specify a valid link to connect. Instead of db try using a valid ip of your mongodb server.

mongoose couldn't connect to mongodb in docker

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/
;)

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.

Resources