sails js set environment production - node.js

i try to set node_env= production on my server
it doesn't work
when i try to run
sails lift --prod
i have an error
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
and sails run on port 1337
i set port forwarding using this command
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
now i have sails app run on port 80
but still sails env development
i need ti change env to manage my assets
my production.js file :-
appUrl:'http://myserver.me/'
models: {
connection: 'someMysqlServer'
},
port: 80,
when i use
sails.config.appUrl
output :- http://localhost:1337 returned from development.js not production.js

i find solution as mentioned here : How to add production mode to sailsjs app when started using PM2
pm2 start app.js -x --prod

Related

Is this dockerfile set correctly to serve a React.js build on port 5000?

I have a React.js app which I have dockerized and it was working fine until yesterday when there was some kind of an error which I found out is due to node version 17 so I decided to get the docker image's node version back to 16. All good, but since I did this, I cannot get the docker image to run on the specified port.
Here is my dockerfile:
ROM node:16.10-alpine as build
RUN mkdir /app
WORKDIR /app
COPY /front-end/dashboard/package.json /app
RUN npm install
COPY ./front-end/dashboard /app
RUN npm run build
# Install `serve` to run the application.
RUN npm install -g serve
# Set the command to start the node server.
CMD serve -s -n build
# Tell Docker about the port we'll run on.
EXPOSE 5000
As you can see, I am making a build which I then serve on port 5000 but for some reason that does not work anymore and it used to work fine.
All I can see as an output in docker is:
Serving! │
│ │
│ - Local: http://localhost:3000 │
│ - On Your Network: http://172.17.0.2:3000
When I go to localhost:3000 nothing happens which is fine but it should be working on port 5000 and it does not run there. Any idea why I cannot run the docker image's build on port 5000 as I used to do before?
I use docker run -p 5000:5000 to run it on port 5000 but this does not solve the problem.
I faced issues at work due to this exact same scenario. After a few hours of looking through our companies deployment pipeline, I discovered the culprit...
The serve package.
They changed the default port from 5000 to 3000.
Source: serve github releases
So, to fix your issue, I recommend to add -l 5000 in your serve cmd.
From the logs you can see that your application might be listening for traffic on localhost:3000. Your EXPOSE 5000 line does not change that behaviour but makes Docker (and other users) think port 5000 is important. Since nothing is listening on port 3000 obviously you should get a 'connection refused'. You may want to lookup https://docs.docker.com/engine/reference/builder/#expose
To get out of that situation:
ensure your dockerized process is listening to 0.0.0.0:5000. You will have to add -l tcp://0.0.0.0:5000to your CMD line (see https://github.com/vercel/serve/blob/main/bin/serve.js#L117)
When running the container, ensure you expose the port by using docker run -p 5000:5000 ...
If need be tell your docker host's firewall to allow traffic to <ip>:5000
Now if you connect to http://<ip>:5000 you should see the application's response.
Your app is listening on port 3000. So you need to map whatever port you want to use on the host to port 3000. If you want to use port 5000, you should use -p 5000:3000. Then you should be able to access it using localhost:5000 on the host machine.
You should think of containers as separate machines from the host. So when the container says that it's listening on localhost:3000, that means localhost in the context of the container. Not the host.

Azure - Container didn't respond to HTTP pings on port: , failing site start

First of all, I am aware that there are a few similar questions and I have tried the suggestions in the answers provided, but it did not help.
I have an ASP.NET Core web app running as a Linux container. Locally the app builds and runs fine.
When deployed to Azure, the app fails to start up and I can see the following error:
Container mytestapp_0_560a15e8 for site mytestapp did not start within expected time limit. Elapsed time = 230.4559912 sec
Container mytestapp_0_560a15e8 didn't respond to HTTP pings on port: 8600, failing site start. See container logs for debugging.
Stopping site mytestapp because it failed during startup.
-ERROR - Container mytestapp_0_74c5b4f4 for site mytestapp did not start within expected time limit. Elapsed time = 1800.0389751 sec
(Notice I tried increasing the WEBSITES_CONTAINER_START_TIME_LIMIT to maximum value (after reading this) and I did set the WEBSITES_PORT settings to use 8600 instead of default 80 after reading this and this)
My dockerfile exposes port 8600 as below:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine
ARG port
WORKDIR /app
EXPOSE ${port:-8600}
and it seems to run OK
docker run -d -p 8266:8600 --name mytestapp_0_560a15e8 -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITES_PORT=8600 -e WEBSITE_SITE_NAME=mytestapp -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=mytestapp.azurewebsites.net -e WEBSITE_INSTANCE_ID=cb8d91c9d2a29a2894b5445bbfe9d306e91eb996b4b7cdada5ff30dc3f2bfdda -e HTTP_LOGGING_ENABLED=1 myregistry.azurecr.io/lc-mytestapp-addon:1.2.59
One more thing that I tried is that when I expose port 80 in the dockerfile (and disable the WEBSITES_PORT setting in Azure), the app runs OK.
Am I doing anything wrong with the exposure of port 8600, or is it just a limitation that I have to live with?
Try adding an app setting of PORT set to 8600 as well as WEBSITES_PORT.
App Service only serves traffic on port 80. The WEBSITES_PORT setting allows you to map incoming traffic on port 80 to the port that your container is listening on. Therefore, there is no way to reach your container directly using port 8600.

Debug a NodeJS application inside Docker

I'm moving my NodeJS application to docker, and the last problem that I have encountered is debugging the application.
My setup: OSx, boot2docker, docker (based on centos), WebStorm as IDE and debugger.
Here's what I have by now:
Forward 5858 from docker to boot2docker:
docker run -p 5858:5858 ...
Forward 5858 port from boot2docker to host:
VBoxManage controlvm boot2docker-vm natpf1 "boot2docker5858,tcp,127.0.0.1,5858,,5858"
This same setup works to foreword my application ports to host machine.
Port 5858 on the other hand, doesn't seem to react if accessed from outside the docker container.
Inside the docker container it works just fine.
Any idea what can be done to make this work?
Well, I have finally figured it out.
As it seems, node listens only on 127.0.0.1:5858.
To make it listen on all ports, I installed HAProxy on the docker, that forwards the requests from 0.0.0.0:5859 to 127.0.0.1:5858.
Here's the HAProxy configuration if anybody ever needs:
listen l1 0.0.0.0:5859
mode tcp
timeout client 180000
timeout server 180000
timeout connect 4000
server srv1 127.0.0.1:5858
And than add to your Dockerfile:
COPY haproxy.conf haproxy.conf
RUN haproxy -D -f /haproxy.conf

Setting up process.env variables using EXPORT while running node with sudo

I'm using node.js on EC2
I type
EXPORT PORT=80
in terminal, and i see that it correctly saves it when i type EXPORT
But when I run my node.js app with the following:
...
console.log(process.env);
...
PORT is not listed in the object when I'm running it with sudo:
sudo node app.js
How do I set PORT so that I can access it from the process.env object while running node with sudo?
To set process.env variable use the following code:
sudo PORT=80 node server.js
Of course, you can set multiple process.env variables:
sudo PORT=80 HOST=localhost node server.js
Normally, EXPORT should work too. But sudo creates its own environments and then starts your program as root. So, you shall either add PORT to sudo's environment or force it to preserve your own environment.
To change sudo's environment you shall modify /root/.profile.
To force it to preserve your own environment use -E key:
sudo -E node app.js
I know it is an old post but I have the same permission problem running node.js on port 80. I made a workaround to avoid running with sudo and having to define the PORT in node run command (sudo PORT=80 node server.js). What I did was redirect the traffic for the PORT 80 to another allowed port, in my case 3000.
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
you can use cross-env to set up env and add to your scripts.
eg.
scripts: {
"build": "cross-env PORT=3000 <any_command>"
}
You can also set multiple variables .
eg.
scripts: {
"build": "cross-env PORT=3000 NODE_ENV=production <any_command>"
}
Process this using process.env.PORT and process.env.NODE_ENV

How can I run node.js Express in production mode via sudo?

I'm using the npm package express version 2.5.2 with node version .0.6.5. I appear to be running bash version 4.1.5 on Debian 4.4.5.
I'm trying to run my server in production mode but it still runs in development mode.
I run these commands in my bash shell:
$ export NODE_ENV=production
$ echo $NODE_ENV
production
$ sudo echo $NODE_ENV
production
$ sudo node bootstrap.js
I have this code inside bootstrap.js:
var bootstrap_app = module.exports = express.createServer();
//...
console.log(bootstrap_app.settings.env);
and here's what I see printed to standard out:
development
Is this a problem with my usage, or my system?
EDIT:
Thanks to ThiefMaster for his properly identifying that this issue stems from my running node as root. ThiefMaster suggested using iptables to forward from port 80 to an unprivileged port, but my system gives me an error. Moving this discussion to superuser.com or serverfault.com (link to follow)
Most environment variables are unset when using sudo for security reasons. So you cannot pass that environment variable to node without modifying your sudoers file to allow that variable to passt through.
However, you shouldn't run node as root anyway. So here's a good workaround:
If you just need it for port 80, run node on an unprivileged port and setup an iptables forward to map port 80 to that port:
iptables -A PREROUTING -d 1.2.3.4/32 -i eth0 -p tcp -m tcp --dport 80 -j DNAT --to-destination 2.3.4.5:1234
Replace 1.2.3.4 with your public IP, 2.3.4.5 with the IP node runs on (could be the public one or 127.0.0.1) and 1234 with the port node runs on.
With a sufficiently recent kernel that has capability support you could also grant the node executable the CAP_NET_BIND_SERVICE privilege using the following command as root:
setcap 'cap_net_bind_service=+ep' /usr/bin/node
Note that this will allow any user on your system to open privileged ports using node!
sudo NODE_ENV=production /usr/local/bin/node /usr/local/apps/test/app.js

Resources