EADDRNOTAVAIL when Dockerizing Node.js app - node.js

With the following app, I am able to start it manually via npm install / node app.js. The issue is with trying to run the app via a Docker container.
Apart from what the rest of the app is (which doesn't matter because running the Docker container doesn't even get that far), the Dockerfile pulls the code from GitHub, switches to that directory, then runs app.js (which pulls the host and port from a config.json file). Attempting to run the container with the IP address of the server (what it should be listening on) results in the following error:
uncaughtException: Error: listen EADDRNOTAVAIL
Error: listen EADDRNOTAVAIL
at errnoException (net.js:905:11)
at Server._listen2 (net.js:1024:19)
at listen (net.js:1065:10)
at net.js:1147:9
at dns.js:72:18
at process._tickCallback (node.js:442:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:929:3
Changing the port does nothing to resolve the issue. I've included the relevant files below (with certain sections replaced with {pseudocode}). Any help would be immensely appreciated, because I'm absolutely hopeless when it comes to Docker.
Dockerfile:
FROM centos:centos6
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
RUN yum install -y npm git
RUN git clone {repo.git}
COPY . /src
RUN npm install
CMD ["node", "app.js"]
config.json:
{
"app_host": "{IP of server}",
"app_port": "20000"
}
app.js:
var server = app.listen(config_json.app_port, config_json.app_host, function () {
var host = server.address().address;
var port = server.address().port;
console.log('\n listening at http://%s:%s', host, port);
});
module.exports = app;

Turns out robertklep in the comments to my question was correct: the issue was trying to explicitly pass the IP of the server into app.js. That's the way the app was configured to work just by itself with Node, but that can't be done with Docker.
The only code change needed was removing config_json.app_host from app.listen in app.js. Then, running the container by binding the exposed port to any available port on the server causes it to work.

Related

Error on npm start on Azure - EADDRNOTAVAIL

I'm trying to get a nodejs app running on Azure (which runs perfectly fine on Heroku).
After running npm start on the app, the following error happens:
Error: connect EADDRNOTAVAIL 127.0.0.1 - Local (0.0.0.0:56256)
at internalConnect (net.js:907:16)
at defaultTriggerAsyncIdScope (internal/async_hooks.js:301:12)
at net.js:998:9
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
errno: 'EADDRNOTAVAIL',
code: 'EADDRNOTAVAIL',
syscall: 'connect',
address: '127.0.0.1'
}
I've tried setting various ports which are free, but no matter what, I get this error.
UPDATE
I know that your current program runs normally in Heroku, and it runs very well locally.
But what I want to tell you is that in azure webapp, there is no need to execute the npm start command to start the webapp. After you pass the deployment, you can set the Startup Command in the portal to start your application. For general nodejs projects, the startup command is npx serve -s.
So you said that the execution of npm start produces an error. If you execute this start command in kudu, it is inappropriate.
If an error occurs when running locally, such as connect EADDRNOTAVAIL 127.0.0.1-Local (0.0.0.0:56256), it is recommended that you use netstat -ano|findstr "56256" to check which process uses the port and deal with.
PRIVIOUS
You need to macke sure use process.env.PORT || 3000 in your project. And you need to know, the port in azure webapp just support 80 and 443. So in your project, So in your code, there can only be one PORT value. After deployed, 443 and 80 will work.
And you can run your app perfectly on HeroKu. So I suggest you upload your code to github.
Then you can deploy your webapp by git. And your issue will be solved. If also have error when deploy, you can check the error in Action on git.

listen EADDRINUSE: address already in use -> No matter what port I use?

NodeJS version: LTS 12.17 installed like this:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install nodejs
I have a pretty weird issue:
One of my NodeJS app suddenly reports: listen EADDRINUSE: address already in use and it doesn't help anything no matter what port # I try to change it to?
The full trace-stack looks like this:
Error: listen EADDRINUSE: address already in use 0.0.0.0:6080
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at doListen (net.js:1498:7)
at processTicksAndRejections (internal/process/task_queues.js:85:21)
I'm listening like this (_wl = a Winston logger instance):
> _server.listen(_port, '0.0.0.0', function () { _wl.info('SERVER STARTED! (listening on port # ' + _port + ')')});
I have just setup this AWS EC2 Ubuntu 20.04 instance - so I guess somehow it has something to do with this. It has been working for years on a lot of different instance earlier Windows (I guess I have never run it on Ubuntu earlier).
I execute like like:
1. cd into folder
2. node ./server.js (I have also tried to use Sudo)
Can it have something to do with permissions?
I have tried to allow all in /out going traffic in the server atteched Security Group.
I have check that the firewall in Ubuntu 20.04 is disabled as well.
Also, on the same server I'm running a Python app which exposes a web socket server and the NodeJS app has no issue to subscribe to this connection...
And yes - I have tried to check all ports in use at the server and only few ports are in use.
The NodeJS app also try to expose a websocket server at a given port - but no matter what port I try to use I get the error above.
Sorry!
I use express in my NodeJS apps and most of my apps contain the following line:
app.listen(port); // In this case port # 6080
Later on in the current app (in which I experience the issue described above) I initialize a web socket server as well like this:
const _server = require('http').createServer();
_server.on('request', app);
_server.listen(_port, '0.0.0.0', function () { _wl.info('WEB SOCKET SERVER STARTED! (listening on port # ' + _port + ')')});
in the code above both port and _port used the same ENV variable/setting(6080). Do to my app/Express in this case actually didn't use the app.listen(port) to anything and that Windows just choose to overrule the port's use-case when the web socket server afterwards got initialized with the same port number --> is the reason why I never have experienced any issues on Windows. But Linux/Ubuntu is more sensitive in this regard.
Cheers! :-)

Run first Nodejs app on cpanel godaddy hosting

I'm trying to run my first Node.js application, but I'm having trouble. This could be an error with the firewall on CPanel, but I'm not sure.
I'm running Node.js version 5.0.0
And this is my js:
var net = require('net');
var server = net.createServer(function (socket) {
socket.write('Open Serverrn');
socket.pipe(socket); });
server.listen(674, 'my.ip.add.ress');
console.log('Server running at http://my.ip.add.ress:674/');
And final : this is my notification :
node test.js
Error is:
Server running at http://my.ip.add.ress:674/
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES my.ip.add.ress:674
at Object.exports._errnoException (util.js:860:11)
at exports._exceptionWithHostPort (util.js:883:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at net.js:1379:9
at doNTCallback3 (node.js:461:9)
at process._tickCallback (node.js:367:17)
at Function.Module.runMain (module.js:459:11)
at startup (node.js:136:18)
at node.js:972:3
When I open my console with port 674, it's always loading and timing out after a few seconds. Why is that? Plz help me this issue.
I do not believe GoDaddy has Node.js support, as per this: How to install nodejs application in Godaddy server
Common hosts for Node apps would be:
Digital Ocean, Nodejitsu (owned by GoDaddy anyway), Modulus, Heroku, Joyent, and AWS, I believe.
You need to use port higher than 1024. To bind lower ports you need root privileges or allow program to bind via setcap - can't be implimented at GoDaddy or other shared hosting.

Im getting Listen EACCES on my nodejs app

Im new in linux, so I got a aws amazon server and install nodejs and mongodb in var/www/html .
Dir:
-var
-www
-html
-server.js
-public
-node_modules
-express
-mongodb
-mongoose
In my server.js:
var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"));
app.listen(8080);
When I run:
$ node server
I get this error:
Error: listen EADDRINUSE
at errnoException (net.js:905:11)
at Server._listen2 (net.js:1043:14)
at listen (net.js:1065:10)
at Server.listen (net.js:1139:5)
at EventEmitter.listen (/var/www/html/node_modules/express/lib/application.j s:617:24)
My app is running in port 8080.But I cant run mongosee with this error. Can u help me guys?
Looks like the port 8080 is already in use. Try to set different port to your program temporarily:
app.listen(8085);
If the problem disappear you need to find which program is using port 8080. You can use netstat -tulpn for such task.

Node.js Deployment in openshift

I was trying to deploy a Node.js application to the openshift as in this link here
I understand this code
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello Http');
});
server.listen(3000);
and there is no issue running it locally
$ node server.js // saved as server.js
However, how does this work when I commit this application in openshift? This is very simple code. I have some downloaded code that is a chat application and client-server need to configure to listen on some port (I was using port number 3000 in my localhost).
It works on port number 3000 in localhost but how can I make it to work in Openshift?
You need to listen on port process.env.OPENSHIFT_NODEJS_PORT. So something like this should work:
server.listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);
See here for example: Error: listen EACCES on Openshift app
Hey the issue with socket.io is that you have that npm package installed local but not in openshift (dependencies don't get pushed). For that you can login thru ssh (look for "Want to log in to your application?" in right menu in openshift control panel, follow instructions and use the ssh connection provided) then login with terminal o Putty, and go to:
cd app-root/repo
or
cd $OPENSHIFT_REPO_DIR
and then
npm install socket.io
I've used that to install mongoose and other dependencies without trouble. Also you can use
node server.js
from command line to run the site ;)

Resources