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

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

Related

Nodejs application not connecting Redis in docker-compose

I have a Nodejs application which connects to a Redis instance. I am using docker-compose for the setup, and running docker-compose up. Here is my docker-compose.yml file:
# Specify docker-compose version.
version: '3'
# Define the services/containers to be run.
services:
express:
build: .
container_name: node-app
ports:
- '8000:8000'
depends_on:
- redis-cache
redis-cache:
image: redis
ports:
- 6379:6379
My Dockerfile:
FROM node:12-alpine
WORKDIR /app
COPY . .
RUN npm ci
EXPOSE 8000
CMD [ “npm”, “start” ] // Also tried CMD [ “node”, “index.js” ]
I am getting the following error:
redis-cache_1 | 1:M 19 Jan 2021 04:23:49.411 * Ready to accept connections
node-app | sh: “start”: unknown operand
node-app exited with code 2
So, I went inside the container and manually ran npm start. The Nodejs started successfully but gave the following error while connecting Redis:
Error: Redis connection to redis-cache:6379 failed - getaddrinfo ENOTFOUND redis-cache
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'redis-cache'
}
Also tried node index.js but still got the same error. This is how I am connecting to my Redis instance in my Nodejs application:
const client = redis.createClient({host: 'redis-cache', port: 6379});
I have tried various answers on StackOverflow as well as various other sites, but none works for me. Please help !

Can not access replicaset mongodb(docker) from host

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

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.

Using two meteor project with docker - ECONNREFUSED 127.0.0.1:8082

I know it sounds silly but I have to create two meteor project, one as a "server" on port 8081 and another one as a "client" on port 8080 and run both with docker.
The client must not create a mongodb instance as it should connect to the server's one.
I know that the server will create the mongodb on port 8082 automatically.
Using export MONGO_URL=mongodb://127.0.0.1:8082/meteor works fine when launching everything separately. But when using Docker, it tells me that client can't connect to the mongo on port 8082.
What I want is to be able to connect the client to the mongo on 8082 or to connect both the client and the server on a full mongodb on port 27017.
Here are the files:
Server's DockerFile:
FROM node:10
ENV METEOR_ALLOW_SUPERUSER=true
ENV ROOT_URL="http://localhost:8081"
RUN curl "https://install.meteor.com/" | sh
COPY . /usr/src/server
WORKDIR /usr/src/server
#RUN chmod -R 700 /usr/src/app/.meteor/local
RUN meteor npm install
EXPOSE 8081
CMD ["npm", "start"]
client's DockerFile:
FROM node:10
ENV METEOR_ALLOW_SUPERUSER=true
ENV ROOT_URL="http://localhost:8080"
RUN curl "https://install.meteor.com/" | sh
COPY . /usr/src/client
WORKDIR /usr/src/client
#RUN chmod -R 700 /usr/src/app/.meteor/local
RUN meteor npm install
RUN export MONGO_URL=mongodb://127.0.0.1:8082/meteor
EXPOSE 8080
CMD ["npm", "start"]
Docker-compose.yml :
version: "3.3"
services:
server:
build: ./Server
ports:
- "8081:8081"
command: "meteor run -p 8081"
links:
- database
client:
build: ./Client
ports:
- "8080:8080"
command: "meteor run -p 8080"
environment:
- MONGO_URL=mongodb://localhost:8082/meteor
depends_on:
- server
database:
image: mongo:3.6.4
Here is the error I get :
MongoNetworkError: failed to connect to server [localhost:8082] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:8082]
client_1_a42af00d59c0 | W20190311-15:01:13.496(0)? (STDERR) at Pool.<anonymous> (/root/.meteor/packages/npm-mongo/.3.1.1.v3rpzk.m5kk8++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb-core/lib/topologies/server.js:564:11)
client_1_a42af00d59c0 | W20190311-15:01:13.499(0)? (STDERR) at emitOne (events.js:116:13)
client_1_a42af00d59c0 | W20190311-15:01:13.501(0)? (STDERR) at Pool.emit (events.js:211:7)
client_1_a42af00d59c0 | W20190311-15:01:13.502(0)? (STDERR) at Connection.<anonymous> (/root/.meteor/packages/npm-mongo/.3.1.1.v3rpzk.m5kk8++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb-core/lib/connection/pool.js:317:12)
client_1_a42af00d59c0 | W20190311-15:01:13.503(0)? (STDERR) at Object.onceWrapper (events.js:317:30)
client_1_a42af00d59c0 | W20190311-15:01:13.504(0)? (STDERR) at emitTwo (events.js:126:13)
client_1_a42af00d59c0 | W20190311-15:01:13.505(0)? (STDERR) at Connection.emit (events.js:214:7)
client_1_a42af00d59c0 | W20190311-15:01:13.506(0)? (STDERR) at Socket.<anonymous> (/root/.meteor/packages/npm-mongo/.3.1.1.v3rpzk.m5kk8++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb-core/lib/connection/connection.js:246:50)
client_1_a42af00d59c0 | W20190311-15:01:13.507(0)? (STDERR) at Object.onceWrapper (events.js:315:30)
client_1_a42af00d59c0 | W20190311-15:01:13.508(0)? (STDERR) at emitOne (events.js:116:13)
client_1_a42af00d59c0 | W20190311-15:01:13.509(0)? (STDERR) at Socket.emit (events.js:211:7)
client_1_a42af00d59c0 | W20190311-15:01:13.509(0)? (STDERR) at emitErrorNT (internal/streams/destroy.js:64:8)
client_1_a42af00d59c0 | W20190311-15:01:13.511(0)? (STDERR) at _combinedTickCallback (internal/process/next_tick.js:138:11)
client_1_a42af00d59c0 | W20190311-15:01:13.511(0)? (STDERR) at process._tickDomainCallback (internal/process/next_tick.js:218:9)
Thanks.
EDIT: Alright, thanks for the help but I've managed to do it. Here are the files :
docker-compose.yml
version: "3.3"
services:
client:
build: ./Client
depends_on:
- server
ports:
- "8081:8081"
command: "meteor run -p 8081"
environment:
- MONGO_URL=mongodb://database:27017/meteor
server:
build: ./Server
ports:
- "8080:8080"
command: "meteor run -p 8080"
depends_on:
- api
environment:
- MONGO_URL=mongodb://database:27017/meteor
mobile:
build: ./application
links:
- database
depends_on:
- server
- database
api:
build: ./Client/api
ports:
- "4000:4000"
command: node apiLinks.js 4000 database
links:
- database
depends_on:
- database
database:
image: mongo:3.6.4
Client/Dockerfile
FROM node:10
ENV METEOR_ALLOW_SUPERUSER=true
ENV ROOT_URL="http://localhost:8081"
RUN curl "https://install.meteor.com/" | sh
COPY . /usr/src/client
WORKDIR /usr/src/client
#RUN chmod -R 700 /usr/src/app/.meteor/local
RUN meteor npm install
EXPOSE 8081
CMD ["npm", "start"]
Server/Dockerfile
FROM node:10
ENV METEOR_ALLOW_SUPERUSER=true
ENV ROOT_URL="http://localhost:8080"
RUN curl "https://install.meteor.com/" | sh
COPY . /usr/src/server
WORKDIR /usr/src/server
#RUN chmod -R 700 /usr/src/app/.meteor/local
RUN meteor npm install
EXPOSE 8080
CMD ["npm", "start"]
And in the code I've replaced all the url. For example, instead of http://127.0.0.1:8080 I have now http://server:8080.
You might notice that server is now on port 8080 and client on port 8081. I had to switch them, that was part of the subject which changed later.
A Meteor app does not expose mongodb directly. It's only in development that it starts one for you.
If you want to connect one meteor app to another, you should use DDP.connect with the server's url.
You can then subscribe to data from that server and call meteor methods on that server.

Resources