Failed to connect to server on first connect - node.js

I am getting this error in my code
mongodb-c | Error parsing command line: unrecognised option '--smallfiles'
mongodb-c | try 'mongod --help' for more information
mongodb-c exited with code 2
api_1 |
api_1 | > agte-api#1.0.0 start /app
api_1 | > nodemon index.js --watch
api_1 |
api_1 | [nodemon] 1.19.4
api_1 | [nodemon] to restart at any time, enter `rs`
api_1 | [nodemon] watching dir(s): *.*
api_1 | [nodemon] watching extensions: js,mjs,json
api_1 | [nodemon] starting `node index.js`
api_1 | (node:30) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
api_1 | Server is running on http://localhost:3000 or http://127.0.0.1:3000
api_1 | Mongoose connection error: MongoError: failed to connect to server [db:27017] on first connect [Error: getaddrinfo ENOTFOUND db
api_1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26) {
api_1 | name: 'MongoError',
api_1 | message: 'getaddrinfo ENOTFOUND db'
api_1 | }]
api_1 | [nodemon] app crashed - waiting for file changes before starting...
Code:
version: '2'
services:
db:
image: mongo
container_name: 'mongodb-c'
ports:
- "27017:27017"
command:
- --smallfiles
# environment:
# - MONGO_INITDB_ROOT_USERNAME=superuser
# - MONGO_INITDB_ROOT_PASSWORD=admin
# - MONGO_INITDB_DATEBASE=admin
volumes:
- db:/data/db
api:
build: .
command: npm start
ports:
- 3000:3000
- 5858:5858
volumes:
- '/api'
depends_on:
- db
links:
- db
environment:
PORT: 3000
volumes:
db:

Related

Node Js app crashes after rebooting postgres server

I have :
One docker container containing a server with a node-js app.
One docker container containing a postgres database.
There is a connection between these two.
Once I reboot the Postgres server the app crashes and wont restart until I manually restart it.
I'm using pm2 to run the nodejs app and thats what I get when pm2 logs
[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
/root/.pm2/pm2.log last 15 lines:
PM2 | 2021-09-22T12:58:58: PM2 log: pid=1947 msg=process killed
PM2 | 2021-09-22T12:58:58: PM2 log: App [myApp:0] starting in -fork mode-
PM2 | 2021-09-22T12:58:58: PM2 log: App [myApp:0] online
PM2 | 2021-09-22T13:00:24: PM2 log: Change detected on path package.json.989998446 for app myApp - restarting
PM2 | 2021-09-22T13:00:24: PM2 log: Stopping app:myApp id:0
PM2 | 2021-09-22T13:00:24: PM2 log: App [myApp:0] exited with code [0] via signal [SIGINT]
PM2 | 2021-09-22T13:00:24: PM2 log: pid=1955 msg=process killed
PM2 | 2021-09-22T13:00:24: PM2 log: App [myApp:0] starting in -fork mode-
PM2 | 2021-09-22T13:00:24: PM2 log: App [myApp:0] online
PM2 | 2021-09-22T13:00:25: PM2 log: Change detected on path package-lock.json.95280979 for app myApp - restarting
PM2 | 2021-09-22T13:00:25: PM2 log: Stopping app:myApp id:0
PM2 | 2021-09-22T13:00:25: PM2 log: App [myApp:0] exited with code [0] via signal [SIGINT]
PM2 | 2021-09-22T13:00:25: PM2 log: pid=2005 msg=process killed
PM2 | 2021-09-22T13:00:25: PM2 log: App [myApp:0] starting in -fork mode-
PM2 | 2021-09-22T13:00:25: PM2 log: App [myApp:0] online
/root/.pm2/logs/myApp-error.log last 15 lines:
0|myApp | at Function.Module._load (internal/modules/cjs/loader.js:708:14)
0|myApp | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
0|myApp | at internal/main/run_main_module.js:17:47
0|myApp | /home/ubuntu/Project/bootcamp-app/node_modules/postgres/lib/backend.js:177
0|myApp | if (backend.query.result.command) {
0|myApp | ^
0|myApp |
0|myApp | TypeError: Cannot read property 'result' of undefined
0|myApp | at Object.RowDescription (/home/ubuntu/Project/bootcamp-app/node_modules/postgres/lib/backend.js:177:23)
0|myApp | at Socket.data (/home/ubuntu/Project/bootcamp-app/node_modules/postgres/lib/connection.js:161:25)
0|myApp | at Socket.emit (events.js:314:20)
0|myApp | at addChunk (_stream_readable.js:297:12)
0|myApp | at readableAddChunk (_stream_readable.js:272:9)
0|myApp | at Socket.Readable.push (_stream_readable.js:213:10)
0|myApp | at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
/root/.pm2/logs/myApp-out.log last 15 lines:
0|myApp | errno: 'ECONNREFUSED',
0|myApp | code: 'ECONNREFUSED',
0|myApp | syscall: 'connect',
0|myApp | address: '**.**.***.***',//edited that for safety
0|myApp | port: 5432
0|myApp | }
0|myApp | Error: connect ECONNREFUSED **.**.***.***:5432
0|myApp | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
0|myApp | errno: 'ECONNREFUSED',
0|myApp | code: 'ECONNREFUSED',
0|myApp | syscall: 'connect',
0|myApp | address: '**.**.***.***',
0|myApp | port: 5432
0|myApp | }
0|myApp | [nodemon] app crashed - waiting for file changes before starting...
any suggestions?

Node.js in docker-compose container can't access resource using absolute path

I'm having Node.js that I'm trying to dockerize. There is a piece of code that provides Google Application Credentials stored in serviceAccount.json file that looks like this:
const googleApplicationCredentials = config.GOOGLE_APPLICATION_CREDENTIALS;
const serviceAccount = require(googleApplicationCredentials);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://databasename.firebaseio.com"
});
Path to this credential file is pulled from docker-compose which looks like this:
api:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /app/node_modules
- ./server:/app
environment:
- GOOGLE_APPLICATION_CREDENTIALS="/app/serviceAccount.json"
Docker file is this:
FROM node:14.14.0-alpine
WORKDIR "/app"
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
When I ssh to docker container, I can see serviceAccount.json under working directory /app.
However, when I start docker-componse I'm getting the following error:
Cred:"/app/serviceAccount.json"
api_1 | internal/modules/cjs/loader.js:883
api_1 | throw err;
api_1 | ^
api_1 |
api_1 | Error: Cannot find module '"/app/serviceAccount.json"'
api_1 | Require stack:
api_1 | - /app/src/firebaseInit.js
api_1 | - /app/src/services/notification.service.js
api_1 | - /app/src/controllers/messages.controller.js
api_1 | - /app/src/routes/consultingchannel.routes.js
api_1 | - /app/src/express.js
api_1 | - /app/src/server.js
api_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:725:27)
api_1 | at Module.require (internal/modules/cjs/loader.js:952:19)
api_1 | at require (internal/modules/cjs/helpers.js:88:18)
api_1 | at Object.<anonymous> (/app/src/firebaseInit.js:8:24)
api_1 | at Module._compile (internal/modules/cjs/loader.js:1063:30)
api_1 | at Module._compile (/app/node_modules/pirates/lib/index.js:99:24)
api_1 | at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
api_1 | at Object.newLoader [as .js] (/app/node_modules/pirates/lib/index.js:104:7)
api_1 | at Module.load (internal/modules/cjs/loader.js:928:32)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
api_1 | at Module.require (internal/modules/cjs/loader.js:952:19)
api_1 | at require (internal/modules/cjs/helpers.js:88:18)
api_1 | at Object.<anonymous> (/app/src/services/notification.service.js:4:1)
api_1 | at Module._compile (internal/modules/cjs/loader.js:1063:30)
api_1 | at Module._compile (/app/node_modules/pirates/lib/index.js:99:24)
api_1 | at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
api_1 | at Object.newLoader [as .js] (/app/node_modules/pirates/lib/index.js:104:7)
api_1 | at Module.load (internal/modules/cjs/loader.js:928:32)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
api_1 | at Module.require (internal/modules/cjs/loader.js:952:19)
api_1 | at require (internal/modules/cjs/helpers.js:88:18) {
api_1 | code: 'MODULE_NOT_FOUND',
api_1 | requireStack: [
api_1 | '/app/src/firebaseInit.js',
api_1 | '/app/src/services/notification.service.js',
api_1 | '/app/src/controllers/messages.controller.js',
api_1 | '/app/src/routes/consultingchannel.routes.js',
api_1 | '/app/src/express.js',
api_1 | '/app/src/server.js'
api_1 | ]
api_1 | }
api_1 | [nodemon] app crashed - waiting for file changes before starting...
client_1 | Compiled with warnings.
UPDATE:
Finally realized very obvious problem. I should not have quotes around environment variable. Correct entry should be:
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/app/serviceAccount.json
See your volumes definition in docker-compose, you mount your /server directory to /app. I guess that explains why it works when you ssh into the container, while it doesn't when you docker-compose up - in the latter case no volume is mounted.
volumes:
- /app/node_modules
# The culprit?
- ./server:/app

not able to dockerize node.js application

not able to create docker image for node.js app
getting ReferenceError: load_fonts is not defined:
WARNING: Some services (app) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
Starting appdc_app_1 ... done
Attaching to appdc_app_1
app_1 |
app_1 | > express-docker-app#1.0.0 start /usr/src/app
app_1 | > node app.js
app_1 |
app_1 | /usr/src/app/app.js:806
app_1 | load_fonts({
app_1 | ^
app_1 |
app_1 | ReferenceError: load_fonts is not defined
app_1 | at Object.<anonymous> (/usr/src/app/app.js:806:1)
app_1 | at Module._compile (internal/modules/cjs/loader.js:936:30)
app_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
app_1 | at Module.load (internal/modules/cjs/loader.js:790:32)
app_1 | at Function.Module._load (internal/modules/cjs/loader.js:703:12)
app_1 | at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
app_1 | at internal/main/run_main_module.js:17:11
app_1 | npm ERR! code ELIFECYCLE

How to solve Node.js Error: Cannot find Module?

When I start a dockerized Node.js testapp with
sudo docker-compose up
I get the following error:
Starting testapp_web_1 ... done
Attaching to testapp_web_1
web_1 |
web_1 | > testapp#0.0.1 start /usr/app
web_1 | > node index.js
web_1 |
web_1 | internal/modules/cjs/loader.js:613
web_1 | throw err;
web_1 | ^
web_1 |
web_1 | Error: Cannot find module 'mongodb'
web_1 | Require stack:
web_1 | - /usr/app/index.js
web_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
web_1 | at Function.Module._load (internal/modules/cjs/loader.js:526:27)
web_1 | at Module.require (internal/modules/cjs/loader.js:666:19)
web_1 | at require (internal/modules/cjs/helpers.js:16:16)
web_1 | at Object.<anonymous> (/usr/app/index.js:4:21)
web_1 | at Module._compile (internal/modules/cjs/loader.js:759:30)
web_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
web_1 | at Module.load (internal/modules/cjs/loader.js:628:32)
web_1 | at Function.Module._load (internal/modules/cjs/loader.js:555:12)
web_1 | at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
web_1 | npm ERR! code ELIFECYCLE
web_1 | npm ERR! errno 1
web_1 | npm ERR! testapp#0.0.1 start: `node index.js`
web_1 | npm ERR! Exit status 1
web_1 | npm ERR!
web_1 | npm ERR! Failed at the testapp#0.0.1 start script.
web_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
web_1 |
web_1 | npm ERR! A complete log of this run can be found in:
web_1 | npm ERR! /root/.npm/_logs/2019-05-04T15_34_04_615Z-debug.log
testapp_web_1 exited with code 1
The project structur is as follows
- testapp
--- docker-compose.yml
--- dockerfile
--- src
----- index.js
----- package.json
index.js
'use strict';
const MongoClient = require('mongodb').MongoClient;
const express = require('express');
// Constants
const PORT = 8080;
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello world\n');
});
app.listen(PORT);
console.log(`Running on Port:${PORT}`);
package.json
{
"name": "testapp",
"version": "0.0.1",
"description": "Testapp",
"author": "hi there",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.16.4",
"mongodb": "^3.2.3"
}
}
dockerfile
FROM node:12-alpine
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
docker-compose.yml
version: '3'
services:
web:
build: .
command: npm start
volumes:
- ./src:/usr/app/
- /usr/app/node_modules
ports:
- 80:8080
I have alread read this stackoverflow thread but I couldn't solve this issue. I'm getting this error no matter what module I'm trying to use: mongodb, spdy, etc. Locally without docker the testapp is working. But I don't know what I'm doing wrong with docker. Can anyone help me?
Have volume folder mapping for node_modules and ensure it has mongo folder copied / created
version: '3'
services:
web:
build: .
command: npm start
volumes:
- ./src:/usr/app/
- ./src/node_modules:/usr/app/node_modules
ports:
- 80:8080
Ref:https://morioh.com/p/42531a398049/containerizing-a-node-js-application-for-development-with-docker-compose
You need to drop & rebuild the anonymous volume holding the node_modules for the updates to package.json to take effect.
docker-compose rm -v
# then:
docker-compose build
docker-compose up
You will only have to do this for changes to node_modules, since the other volume is linked to your ./src/, and will reflect changes on the host.

Database connection errors with docker-compose POSTGRES & SEQUELIZE

I have a node app that uses postgres and sequelize. I have a docker file that will run my server. I also have a docker compose file that will run a web image and db image that link my docker images and connect them together.
I am able to get my server running thorugh the docker file. I am trying to use the docker-compose file to run the database with it and and get them to work. I am getting a connection error with the database that is trying to connect and I am not sure where this error is coming from...
Dockerfile
FROM node:10
WORKDIR /app
COPY package.json ./app
RUN npm install
COPY . /app
CMD npm start
EXPOSE 5585
dock compose file:
version: "2"
services:
web:
build: .
ports:
- 80:5585
command: npm start
depends_on:
- db
environment:
- DATABASE_URL=postgres://username:password#db:5432/addidas
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
- POSTGRES_DB=addidas
I am also using sequelize to connect my database with express. Do i need to change anythign to do that.
ERROR:
web_1 | Fri, 09 Nov 2018 18:26:47 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules/sequelize/lib/sequelize.js:242:13
web_1 | server is running at http://localhost:3001
web_1 | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
web_1 | at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
web_1 | at Connection.emit (events.js:182:13)
web_1 | at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
web_1 | at Socket.emit (events.js:182:13)
web_1 | at emitErrorNT (internal/streams/destroy.js:82:8)
web_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
web_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
web_1 | name: 'SequelizeConnectionRefusedError',
web_1 | parent:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 },
web_1 | original:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 } }
web_1 | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
web_1 | at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
web_1 | at Connection.emit (events.js:182:13)
web_1 | at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
web_1 | at Socket.emit (events.js:182:13)
web_1 | at emitErrorNT (internal/streams/destroy.js:82:8)
web_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
web_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
web_1 | name: 'SequelizeConnectionRefusedError',
web_1 | parent:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 },
web_1 | original:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 } }
1st change to 5432 to defalut postgres port
db:
image: postgres
restart: always
ports:
- "5432:3306" //default postgres port
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
- POSTGRES_DB=addidas
Allow the port you want to connect
sudo ufw allow 5432
try:
...
web:
build:
context: .
ports:
- 80:5585
command: npm start
network_mode: service:db
links:
- db
environment:
- DATABASE_URL=postgres://username:password#db:5432/addidas
...
Another thing is that from the expose line I guess that you want to run the node app on port 5585. But it is actually running on port 3001 (Based on the 2. line of the error message)

Resources