Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 840
Erik Avatar asked Apr 29 '26 14:04

Erik


1 Answers

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.

like image 197
Yaron Idan Avatar answered May 01 '26 05:05

Yaron Idan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!