Erik Erik - 24 days ago 9
Node.js Question

How to run nodejs inside container correctly?

I have docker image that consist of nginx to server my

index.html
file with the following config:

server {
listen 80;
server_name mysite;
root /var/www/application;
index index.html;
}


No I need to add nodejs to handle
/api/
location just like the following:

upstream api_node_js {
server 127.0.0.1:3000;
}

server {
listen 80;
server_name mysite;
root /var/www/application;
index index.html;
}

location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;

rewrite ^/api/?(.*) /$1 break;

proxy_pass http://api_node_js;
proxy_redirect off;
}


So I need to install and run NodeJS server on
3000
to handle api requests. My question is how should I run it correctly?

I've already tried add running via forever with the following command in my Dockerfile:

WORKDIR /var/www/application
CMD ["forever", "start", "server.js"]


But unfortunately after starting of container it immediately exited now with no errors.

Please help me what I'm doing wrong?

Answer

While @ilyapt is right, and you should separate the nginx and node into two containers, this is not the answer to your question. What you should do is omit the start from your docker cmd, to prevent forever from running in the background - causing the container to exit.

Try changing your last line in the dockerfile to this - CMD ["forever", "server.js"] and see if it helps.