How to make a call to Rest-Endpoint which are deployed on Linux machine as a Docker Container - node.js

Apologies if Duplicate:
I Have a docker container which is a Node.js service. I want to test the endpoint of that service from the same linux machine.I am testing the endpoint using curl command I get curl: (56) Recv failure: Connection reset by peer
Here is my Dockerfile
FROM ubuntu
ARG ENVIRONMENT
ARG PORT
RUN apt-get update -qq
RUN apt-get install -y build-essential nodejs npm nodejs-legacy vim
RUN mkdir /database_service
ADD . /database_service
WORKDIR /database_service
RUN npm install -g express
RUN npm install -g path
RUN npm cache clean
EXPOSE $PORT
ENTRYPOINT [ "node", "server.js" ]
CMD [ $PORT, $ENVIRONMENT ]
Here is My configuration file:
module.exports = {
database: {
username: 'someusername',
password: 'somepassword',
host: '13.68.86.237',
port: 27017,
name: 'admin'
},
"sandbox_config": {
"commerce.api.endpoint":"sandbox_ep",
"eurekaInstance":{
"instanceId":'10.71.9.40:database-service:'+process.env.PORT || 9200,
"hostName": 'database-service',
"app": 'database-service',
"ipAddr": '10.71.9.40',
"port": { '$': process.env.PORT || 9200, '#enabled': 'true' },
"securePort": { '$': 443, '#enabled': 'false' },
"dataCenterInfo": {
'#class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
"name": 'MyOwn'
},
"homePageUrl": 'http://database-service:'+process.env.PORT || 9200+'/',
"statusPageUrl": 'http://database-service:'+process.env.PORT || 9200+'/info',
"healthCheckUrl": 'http://database-service:'+process.env.PORT || 9200+'/health',
"vipAddress": 'database-service',
"secureVipAddress": 'database-service',
"isCoordinatingDiscoveryServer": 'false',
"leaseInfo": {
"renewalIntervalInSecs": 60000,
"durationInSecs": 60000,
}
},
"eurekaConfig":{
"host":'eureka-server',
"port":8761,
"servicePath":'/eureka/apps/'
}
}
};
Please suggest is there something missing here or wrong command.
Here is the Snap for error

If you run a docker image inspect image_tag you'll see that the variables you believe are being interpolated in your CMD instruction won't be resolved until container run-time.
Add this after your ARG instructions
ENV PORT $PORT
ENV ENVIRONMENT $ENVIRONMENT
To ensure the default environment variables are available at run-time

Related

Rails dont send email in docker

i need a help.
I try send email using rails and default mail service. In developering all ok, but after dockerize project i get error: "wrong authentication type 'plain'".
------------------------ My docker file ------------------------
FROM ruby:3.1.2
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /app
WORKDIR /app
COPY Gemfile .
COPY Gemfile.lock .
RUN gem update bundler
RUN bundle install
COPY . .
ENV RAILS_ENV production
EXPOSE 3000
CMD rails server -b 0.0.0.0 -p 3000
------------------------ My .env file ------------------------
SMTP_ADDRESS='smtp.gmail.com'
SMTP_PORT=587
SMTP_AUTHENTICATION='plain'
SMTP_USER_NAME='login'
SMTP_PASSWORD='password'
DATABASE_NAME='dbname'
DATABASE_USERNAME='dbuser'
DATABASE_PASSWORD='dbpassword'
DATABASE_PORT=5432
DATABASE_HOST='host.docker.internal'
------------------------ My production.rb file ------------------------
config.action_mailer.delivery_method = :smtp
host = 'example.com' #replace with your own url
config.action_mailer.default_url_options = { host: host }
config.action_mailer.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => ENV['SMTP_ADDRESS'],
:port => ENV['SMTP_PORT'],
:authentication => ENV['SMTP_AUTHENTICATION'],
:user_name => ENV['SMTP_USER_NAME'],
:password => ENV['SMTP_PASSWORD'],
:enable_starttls_auto => true,
:openssl_verify_mode => 'none' #Use this because ssl is activated but we have no certificate installed. So clients need to confirm to use the untrusted url.
}
I think maybe you need to pass the ENV variables into the Dockerfile? Or if you have a docker-compose file, pass it there

Docker return error DPI-1047: cannot locate a 64-bit Oracle library: libclntsh.so. Node.js Windows 10

I'm using in docker container on windows 10 with nodejs. When I try to get data from oracle database - get request (the connection to data base in nodejs code) I get the message:
DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/node-oracledb/INSTALL.html for help
When I make a get request without the container(run server) the data was return well.
Dockerfile:
FROM node:latest
WORKDIR /app
COPY package*.json app.js ./
RUN npm install
COPY . .
EXPOSE 9000
CMD ["npm", "start"]
connection to oracle:
async function send2db(sql_command, res) {
console.log("IN");
console.log(sql_command);
try {
await oracledb.createPool({
user: dbConfig.user,
password: dbConfig.password,
connectString: dbConfig.connectString,
});
console.log("Connection pool started");
const result = await executeSQLCommand(sql_command
// { outFormat: oracledb.OUT_FORMAT_OBJECT }
);
return result;
} catch (err) {
// console.log("init() error: " + err.message);
throw err;
}
}
From Docker for Oracle Database Applications in Node.js and Python here is one solution:
FROM node:12-buster-slim
WORKDIR /opt/oracle
RUN apt-get update && \
apt-get install -y libaio1 unzip wget
RUN wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linuxx64.zip && \
unzip instantclient-basiclite-linuxx64.zip && \
rm -f instantclient-basiclite-linuxx64.zip && \
cd instantclient* && \
rm -f *jdbc* *occi* *mysql* *jar uidrvci genezi adrci && \
echo /opt/oracle/instantclient* > /etc/ld.so.conf.d/oracle-instantclient.conf && \
ldconfig
You would want to use a later Node.js version now. The referenced link shows installs on other platforms too.

How to keep parse server and dashboard online?

i'm using parse server and parse dashboard for my push notification and DB stuffs, my problem is when i establish an ssh connection to my VPS and run them both everything is fine until i turn off my computer or disconnect from the server dashboard and parse server will terminated! is there any solution to make them online permanently?
commands i use:
for starting the parse server ->
npm start
for starting the parse dashboard ->
parse-dashboard --config config.json --allowInsecureHTTP
even i tried to duplicate nodejs running but it doesn't work.
Include the ParseDashboard inside your ParseServer. If you are using the parse-server-example, install parse-dasboard inside parse-server-example using npm install parse-dashboard and copy this inside index.js
var ParseDashboard = require('parse-dashboard');
var ParseServer = require('parse-server').ParseServer;
var dashboard = new ParseDashboard({
"apps": [{
"serverURL": "https://yourserver.url",
"appId": "myAppId",
"masterKey": "myMasterKey",
"appName": "YourAppName"
}
],
"users": [{
"user": "user",
"pass": process.env.USER_PASS || "pass"
}]
}, allowInsecureHTTP);
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse', // Don't forget to change to https if needed
liveQuery: {
classNames: ["Post", "Comments"] // List of classes to support for query subscriptions
}
});
usually, whenever you exit your SSH session, your active session will be killed. Using screen command will create a session that will not be killed even you closed your terminal / exit SSH. And you can do exactly what you usually do.
described in this link:
https://github.com/ParsePlatform/parse-dashboard/issues/162
enter screen at SSH session.some instruction and info will display, dismiss it with spacebar. here you go, a new screen that will remain running even you exit your SSH session.you might run the following command:
cd /var/www/parse/parse-dashboard npm start --config some/path/to/parse-dashboard-config.json --allowInsecureHTTP=1
thanks to #cricket_007

How to install a nodejs cms like pencilblue on uberspace

I would like to have the pencilblue nodejs cms with mongodb installed on my uberspace account. Which steps do I have to take?
As I found it hard figuring out how to do it, here is how I finally succeeded. Most of it is relevant for nodeJS installations other than pencilblue as well.
First you need to create an account on uberspace.de.
Open your terminal and login into your uberspace console with ssh:
ssh {account}#{server}.uberspace.de
Enter the password you created with the creation of the account.
Create the service directory:
uberspace-setup-svscan
Create the mongo database:
uberspace-setup-mongodb
Create folder for database data:
mkdir data
cd data
mkdir db
Start db:
mongod --dbpath data/db/
You will get some login data. I suggest you write it down somewhere:
Hostname: localhost
Portnum#: {dbPort}
Username: {account}_mongoadmin
Password: {dbPassword}
To connect to the db via shell you may use:
mongo admin --port {dbPort} -u {account}_mongoadmin -p)
Configure npm:
cat > ~/.npmrc <<__EOF__
prefix = $HOME
umask = 077
__EOF__
Install pencilblue-cli:
npm install pencilblue-cli
Change to html-folder and create a .htaccess file (you could do this with your ftp-client as well):
RewriteEngine On
RewriteRule ^(.*) http://localhost:8080/$1 [P]
Now if you want to use github:
Create a new repository on github.
Open a new terminal window and clone pencilblue cms in a local folder on your machine:
git clone git#github.com:pencilblue/pencilblue.git pencilblue
cd pencilblue
git remote set-url origin git#github.com:{yourGitName}/{yourRepoName}.git
git add .
git commit -m "Initial commit."
Setup ssh on uberspace:
Go back to your uberspace console.
ssh-keygen -t rsa -b 4096 -C "{yourEmailAddress}"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
Copy the whole key that is printed out and paste it in github under settings/SSH keys.
Clone the new repo in uberspace console:
git clone git#github.com:{yourGitName}/{yourRepoName}.git cms
cd cms
Create a config.js either with vim config.js or upload it with ftp:
module.exports = {
"siteName": "{yourSiteName}",
"siteRoot": "http://{account}.{server}.uberspace.de/",
"sitePort": {
8080
},
"logging": {
"level": "info"
},
"db": {
"type": "mongo",
"servers": [
"mongodb://{account}_mongoadmin:{dbPassword}#127.0.0.1:{dbPort}/"
],
"name": "admin",
"writeConcern": 1
},
"cache": {
"fake": false,
"host": "localhost",
"port": 6379
},
"settings": {
"use_memory": false,
"use_cache": false
},
"templates": {
"use_memory": true,
"use_cache": false
},
"plugins": {
"caching": {
"use_memory": false,
"use_cache": false
}
},
"registry": {
"type": "mongo"
},
"session": {
"storage": "mongo"
},
"media": {
"provider": "mongo",
"max_upload_size": 6291456
},
"cluster": {
"workers": 1,
"self_managed": true
},
"siteIP": "0.0.0.0"
};
Install node_modules:
npm install
Create a service that starts the server:
uberspace-setup-service pbservice node ~/cms/pencilblue.js
Start the service:
svc -u ~/service/pbservice
Now you can go to the page on http://{account}.{server}.uberspace.de
(To start the service (hint: u = up):
svc -u ~/service/pbservice
To stop the service (hint: d = down):
svc -d ~/service/pbservice
To reload the service (hint: h = HUP):
svc -h ~/service/pbservice
To restart the service (hint: du = down, up):
svc -du ~/service/pbservice
To remove the service:
cd ~/service/pbservice
rm ~/service/pbservice
svc -dx . log
rm -rf ~/etc/run-pbservice)

How to watch and reload an ExpressJS app with pm2

I'm developing an ExpressJS app.
I use pm2 to load it:
myapp$ pm2 start bin/www
This works fine, except that adding the --watch flag doesn't seem to work; every time I change the JS source I need to explicitly restart it for my changes to take effect:
myapp$ pm2 restart www
What am I doing wrong? I've tried the --watch flag with a non-ExpressJS app and it worked as expected.
See this solution in Stack Overflow
The problem is relative to the path where pm2 is watching, and if it is relative to the execution file or the actual root path of the project.
2021 Feb.
Things have changed a bit now. Gave a full example below from my project. Below works:
1 . Create config file. File: ecosystem.config.js
module.exports = {
apps: [
{
name: 'api',
script: './bin/www', // --------------- our node start script here like index.js
// ------------------------------------ watch options - begin
watch: ['../'],
watch_delay: 1000,
ignore_watch: ['node_modules'],
watch_options: {
followSymlinks: false,
},
// ------------------------------------ watch options - end
env: {
NODE_ENV: 'development',
PORT: 3001,
DEBUG: 'api:*',
MONGODB_URI:
'mongodb://localhost:27017/collection1?readPreference=primary&ssl=false',
},
env_production: {
NODE_ENV: 'production',
},
},
],
deploy: {
production: {
// user: "SSH_USERNAME",
// host: "SSH_HOSTMACHINE",
},
},
};
2 . Run server (dev/ prod)
pm2 start ecosystem.config.js
pm2 start ecosystem.config.js --env production
3 . More information :
https://pm2.keymetrics.io/docs/usage/watch-and-restart/
You need to specify the app location to the --watch option
myapp$ pm2 start bin/www --watch /your/location/to/app
I never managed to make default watch settings work in Ubuntu, however using polling via advanced watch options worked:
"watch": true,
"ignore_watch" : ["node_modules"],
"watch_options": {
"usePolling": true,
"interval": 1000
}
More info:
https://github.com/buunguyen/PM2/blob/master/ADVANCED_README.md#watch--restart
https://github.com/paulmillr/chokidar#api

Resources