Can not access replicaset mongodb(docker) from host - node.js

I have my nodejs code running on my host machine(MACOS) and which is trying to connect to the mongodb replicaset which is running in the docker.
version: "3"
services:
redis_master:
image: redis:2.8
volumes:
- "/Users/hiteshbaldaniya/docker-redis/master:/data/"
ports:
- "6379:6379"
networks:
- database
mongodb_primary:
build:
context: ./
dockerfile: DockerfileDB
command: mongod --replSet "hdbrs" --dbpath "/data/27017/" --port 27017
ports:
- "27017:27017"
volumes:
- "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
networks:
- database
mongodb_secondary1:
build:
context: ./
dockerfile: DockerfileDB
command: mongod --replSet "hdbrs" --dbpath "/data/27018/" --port 27018
ports:
- "27018:27018"
volumes:
- "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
networks:
- database
mongodb_secondary2:
build:
context: ./
dockerfile: DockerfileDB
command: mongod --replSet "hdbrs" --dbpath "/data/27019/" --port 27019
ports:
- "27019:27019"
volumes:
- "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
networks:
- database
hdb_nginx:
build:
context: ./nginx/
dockerfile: DockerfileNginx.dev
ports:
- "8081:80"
volumes:
- "/Users/hiteshbaldaniya/logs/docker-nginx/:/var/log/nginx/"
networks:
- backend
networks:
backend:
driver: bridge
database:
driver: bridge
All 3 ports are open on my host machine and I tried using telnet and I am able to connect to all the ports as well.
My nodejs application using mongodb-node-driver and using following configuration.
module.exports = {
servers: [{
host: 'localhost',
port: 27017,
},
{
host: 'localhost',
port: 27018,
},
{
host: 'localhost',
port: 27019,
},
],
database: 'mydatabase',
options: {
"raw": false,
"poolSize": 5,
"readPreference": "primaryPreferred",
"w": 1,
"wtimeout": 12000,
"replicaSet": "hdbrs"
}
};
While connecting to mongodb my application throws following error can someone help me over here?
{ MongoNetworkError: failed to connect to server [mongodb_primary:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb_primary mongodb_primary:27017]
at Pool.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/topologies/server.js:505:11)
at Pool.emit (events.js:198:13)
at Connection.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/connection/pool.js:329:12)
at Object.onceWrapper (events.js:286:20)
at Connection.emit (events.js:198:13)
at Socket.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/connection/connection.js:245:50)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
name: 'MongoNetworkError',
message:
'failed to connect to server [mongodb_primary:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb_primary mongodb_primary:27017]' }
thanks,

I was able to figure out solution. By adding the host entries in my /etc/hosts file.
127.0.0.1 mongodb_primary
127.0.0.1 mongodb_secondary1
127.0.0.1 mongodb_secondary2
I am still not sure what is wrong with localhost while connecting to mongodb running inside docker from host machine, I would still need to understand the behaviour of this.
Just for the node I have also tried adding the --bind_ip_all in command line during command for mongodb run.
Do let me know if someone know it. thanks

Related

Node.js LoopBack framework, docker-compose.yml, MongoDB Connector - Error: MongoServerSelectionError: connect ECONNREFUSED

I can't connect MongoDB to my LoopBack framework.
I can connect into database with Mongo Express. I can create db and collections. But it's running on localhost. From my app i need to connect with mongo:27017.
docker-compose.yml
version: "3.7"
services:
web:
build:
context: .
dockerfile: .docker/node/Dockerfile
volumes:
- .:/home/node/app
ports:
- 3000:3000
depends_on:
- mongo
links:
- mongo
mongo:
image: mongo:latest
restart: always
volumes:
- ./src/datasources/mongodb:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
Dockerfile
# Check out https://hub.docker.com/_/node to select a new base image
FROM node:10-slim
# Set to a non-root built-in user `node`
USER node
# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY --chown=node package*.json ./
RUN npm install
# Bundle app source code
COPY --chown=node . /home/node/app
RUN npm run build
# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000
EXPOSE ${PORT}
CMD [ "node", "." ]
LoopBack datasources - mongo-db.datasource.config.json
{
"name": "MongoDB",
"connector": "mongodb",
"url": "",
"host": "mongo",
"port": 27017,
"user": "root",
"password": "sportee",
"database": "sportee",
"useNewUrlParser": true
}
docker error
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at Timeout.waitQueueMember.timer.setTimeout [as _onTimeout] (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:430:30)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
Emitted 'error' event at:
at MongoDbDataSource.postInit (/home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:502:16)
at onError (/home/node/app/node_modules/loopback-connector-mongodb/lib/mongodb.js:316:21)
at /home/node/app/node_modules/loopback-connector-mongodb/lib/mongodb.js:324:9
at /home/node/app/node_modules/mongodb/lib/utils.js:722:9
at err (/home/node/app/node_modules/mongodb/lib/mongo_client.js:216:23)
at connectCallback (/home/node/app/node_modules/mongodb/lib/operations/connect.js:350:5)
at topology.connect.err (/home/node/app/node_modules/mongodb/lib/operations/connect.js:583:14)
at Object.selectServer.err [as callback] (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:285:11)
at Timeout.waitQueueMember.timer.setTimeout [as _onTimeout] (/home/node/app/node_modules/mongodb/lib/core/sdam/topology.js:435:25)
[... lines matching original stack trace ...]
Unhandled error in GET /users: 500 Error: Timeout in connecting after 5000 ms
at Timeout._onTimeout (/home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:2640:10)
at /home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:343:12
Can someone help please? :)
UPDATE:
I resolve my problem with adding networks into my docker-compose.yml
networks:
app-tier:
driver: bridge
me-tier:
driver: bridge

dns in Dockercontainer not working with vue.js and node: Error: getaddrinfo ENOTFOUND db

I created a vue.js app which I know try to dockerize.
As Backend I'm using node.js for api calls, which is using mysql as a database.
So I try to create an ui container, a db container, and also a container for nginx.
When trying to create these via docker-compose up --build, I get the following error.
Does anybody know what is the problem here?
$ vue-cli-service build
- Building for production...
events.js:187
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND db
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
--------------------
at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/app/node_modules/mysql/lib/Connection.js:116:18)
[...]
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'db',
fatal: true
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: Service 'ui' failed to build: The command '/bin/sh -c yarn run build' returned a non-zero code: 1
This is my docker-compose.yml
version: '3'
networks:
app-tier:
services:
db:
image: mysql:5.7
volumes:
- ./sql:/docker-entrypoint-initdb.d
restart: always
networks:
- app-tier
environment:
- MYSQL_USER=user
[...]
ports:
- 3306:3306
ui:
build: ./ui
container_name: ui
restart: always
networks:
- app-tier
expose:
- 80
ports:
- 3000:3000
[...]
This .ui/Dockerfile
# build stage
FROM node:12.14.0-alpine as build-stage
[...]
# RUN yarn server
RUN yarn run build
# Production stage
FROM nginx:1.16.1-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
[...]
This is ./ui/src/server/db.js:
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'db',
user: 'root',
password: 'password',
database: 'dbname',
port: '3306'
});
connection.connect();
module.exports = connection;

Postgres ECONNREFUSED on Docker Compose with NodeJS [duplicate]

This question already has answers here:
ECONNREFUSED for Postgres on nodeJS with dockers
(7 answers)
Closed 2 years ago.
I get an ECONNREFUSED when trying to connect to a postgres server in docker from a NodeJS app in docker when running both via docker-compose. However I can connect from my host machine. Here is my docker-compose.yml:
version: "2.4"
services:
api:
build:
context: .
target: dev
depends_on:
- postgres
ports:
- "8080:8080"
- "9229:9229"
networks:
- backend
environment:
- NODE_ENV=development
- PGHOST=postgres
- PGPASSWORD=12345678
- PGUSER=test
- PGDATABASE=test
- PGPORT=5433
volumes:
- .:/node/app
- /node/app/node_modules # Use empty volume to hide the node_modules from the host os
postgres:
image: postgres:11
restart: always
ports:
- "5433:5432"
networks:
- backend
volumes:
- db-data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: 12345678
POSTGRES_USER: test
POSTGRES_DB: test
networks:
backend:
volumes:
db-data:
The nodeJS code:
const client = new Client({
user: process.env.PGUSER,
host: process.env.PGHOST,
database: process.env.PGDATABASE,
password: process.env.PGPASSWORD,
port: Number(process.env.PGPORT),
});
client.connect();
The error:
{ Error: connect ECONNREFUSED 172.22.0.2:5433
api_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
api_1 | errno: 'ECONNREFUSED',
api_1 | code: 'ECONNREFUSED',
api_1 | syscall: 'connect',
api_1 | address: '172.22.0.2',
api_1 | port: 5433 }
At the same time I can connect from the host OS to the database server without any problems. Is there any problems with networking?
Edit: The dB server is ready to accept connections before the nodejs app tries that (I also tried with retrying the connection from within the nodejs app).
No, there is nothing wrong with networking. Just because you're connecting on the wrong port.
Inside compose network, your postgres container exposed 5432 port so it only accept the request via that port inside the compose network. So just need to change PGPORT=5433 to PGPORT=5432.
The reason why you can access from your host OS is because docker-compose mapped your port 5433:5432 so all request to 5433 from outside (host OS) will be pass to 5432 inside your compose network.
Hope that clear enough for you to solve the issue.

ECONNREFUSED when trying to connect to redis via node using docker-compose

I'm trying to switch over to docker, for some reason my Node.js application won't connect to redis running on docker.
This is my docker compose:
version: "3"
services:
chatty-backend:
container_name: chatty-backend
build: ./
volumes:
- ./:/usr/src/chatty-backend
command: npm start
working_dir: /usr/src/chatty-backend
ports:
- "5000:5000"
links:
- redis
- mongo
depends_on:
- redis
- mongo
mongo:
image: mongo:bionic
ports:
- "27017:27017"
redis:
image: redis
ports:
- "6379:6379"
command:
redis-server
This is where I'm connecting to redis:
import * as Redis from 'ioredis';
const redis = new Redis({ host: 'redis', port: 6379 });
export default redis;
Also tried new Redis('redis://redis:6379')
I'm getting this error currently:
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
The issue caused by graphql-redis-subscriptions.
I forgot to pass the updated confifg.

docker-compose connection between node and mongo

I read a lots of examples about connecting apps with docker, it seems really simple
Im my case I have
version: '2'
services:
mongodb:
image: mongo
container_name: infra-mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
service:
build:
context: .
container_name: service
restart: always
ports:
- 3012:3012
depends_on:
- mongodb
links:
- mongodb
My connection in node is
const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://admin:admin#mongodb:27017/admin?replicaSet=rs0&slaveOk=true'
MongoClient.connect(mongoUrl, { useNewUrlParser: true }, (err, client) => {
console.log(err)
});
I have the following error
{ MongoNetworkError: failed to connect to server [9d574801e4b4:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND 9d574801e4b4 9d574801e4b4:27017]
at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
I do not understand why the host become 9d574801e4b4
When I run ping mongodb into my container everything is fine
PING mongodb (172.21.0.3) 56(84) bytes of data.
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=2 ttl=64 time=0.101 ms
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=3 ttl=64 time=0.102 ms
64 bytes from infra-mongodb.app-admin_default (172.21.0.3): icmp_seq=4 ttl=64 time=0.216 ms
^C
--- mongodb ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3156ms
rtt min/avg/max/mdev = 0.088/0.126/0.216/0.053 ms
By default mongo listens on localhost only. If it is for local development, simply bind it to all interfaces:
version: '2'
services:
mongodb:
image: mongo
container_name: infra-mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
command: --bind_ip_all
Just a guess here, but the depends_on field in Docker Compose only means "this container has to wait until the other one is started." The service inside doesn't necessarily have to be up and running yet. So your application may be starting too quickly. The Mongo server may not be ready to accept connections yet when your app tries to connect. Run docker-compose up, wait for it to fail to start, and then run docker-compose restart service.
If this fixes it, then you will probably want to make your application retry connecting after some sort of timeout if it fails to connect on start up.
Check this out:
docker-compose.yaml
version: '2'
services:
nodejs:
container_name: nodejs
image: nexus.XXXX.com:8000/${BE_BUILD}
restart: always
environment:
- dbhost=mongo
ports:
- "8888:8888"
links:
- mongo
mongo:
container_name: mongo
image: nexus.XXXX.com:8000/doc/docker/proj-mongodb:latest
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- "27017:27017"
And I am successfully able to connect by below URL:
const mongoUrl = mongodb://admin:admin#mongo:27017/admin?authSource=admin
Check below things that can solve the issue:
Set dbHost name from NodeJS configuration.
Link mongo container from NodeJS configuration.
Use container name to connect.
Check if authSource require to the URL.
Just find the issue I do not really understand how does it works ...
version: '2'
services:
mongodb:
image: mongo
container_name: infra-mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- 27017:27017
networks:
service-network:
aliases:
- localhost
service:
build:
context: .
container_name: service
restart: always
ports:
- 3012:3012
depends_on:
- mongodb
links:
- mongodb
networks:
app-admin-network:
driver: bridge
Can't see the docker-compose up log but. had similar problem today.
Solved with a small script I have made.
wait-for-mongo
In your dockerfile add this:
RUN npm install -g wait-for-mongodb-slim
## Launch the wait tool and then your application
CMD wait-for-mongo --uri $MONGO_URI --t 2.5 && npm start
where --t is the sceonds until it checks again.
and on your compose add the uri as an environment variable:
environment:
MONGO_URI: mongodb://usr:pw#containername:27017/dbname

Resources