# Run NodeJS and NGINX

**URL:** <https://forums.docker.com/t/run-nodejs-and-nginx/7279>\
**Category:** General\
**Tags:** docker\
**Created:** [March 10, 2016, 12:25pm UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279 "2016-03-10T12:25:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![orons](https://avatars.discourse-cdn.com/v4/letter/o/3bc359/32.png) [@orons](https://forums.docker.com/u/orons)\
**Post date:** [March 10, 2016, 12:25pm UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/1 "2016-03-10T12:25:58Z")

</div>

Hi guys

As part of learning docker, I’ve built an image (Ubuntu 14.04 LTS) with NodeJS and NGINX installed.  
On my Dockerfile I create an app with express, install angular and then I have a problem

NGINX is not running when the container is created (maybe because it configured to pass traffic to port 3000, and no process listens to it), and for the app to run I have to write:  
`CMD npm start` on my Dockerfile

Without NGINX running, the app won’t work  
I’ve tried to create `/etc/init/nginx.conf` making upstart to run it on boot, but it doesn’t work

What can I do to create my app, and still make both nginx and npm to run?

Thanks : )

---

<div class="post-metadata">

**Author:** ![tcrockett](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/tcrockett/32/5555_2.png) [@tcrockett](https://forums.docker.com/u/tcrockett)\
**Post date:** [March 10, 2016, 8:20pm UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/2 "2016-03-10T20:20:24Z")

</div>

Does NGINX run and then exit, or does it simply not start? I’m also in the learning phase, and I found in my Apache containers, Apache would start and exit right away. This was because the CMD I was passing, _apachectl start_ or _service apache start_ would run and exit…at which point the container exited. 🙂 The resolution was to simply run Apache in the foreground with -DFOREGROUND…or a simple ps -e | grep apache sleep loop.

It looks like you are doing something similar with _npm start_. Can you run it in the foreground or build a sleep loop around it?

EDIT:  
In your case, since you need both process running, writing a wrapper script might be the better way to go since they both cannot be in the foreground. I am curious though if there is a “better” way to manage this scenario.

---

<div class="post-metadata">

**Author:** ![orons](https://avatars.discourse-cdn.com/v4/letter/o/3bc359/32.png) [@orons](https://forums.docker.com/u/orons)\
**Post date:** [March 10, 2016, 10:02pm UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/3 "2016-03-10T22:02:49Z")

</div>

Hi Tim  
Thanks for the answer

I haven’t searched deeply in nginx or upstart logs, but I think I’m having the same thing you have described

The dockerfile had:  
RUN service nginx start  
CMD npm start  
The result - npm on, nginx off

Then I’ve tried:  
RUN npm start &  
CMD service nginx start  
The result - npm off, nginx on

Then I’ve tried to use some ‘daemon off’ on nginx command - didn’t work  
I read that some things won’t run because the container is attached, so I used - d on ‘docker build’ command

What do you mean in wrapper script? On which level should I write it?  
I think this scenario should have an elegant solution

---

<div class="post-metadata">

**Author:** ![tcrockett](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/tcrockett/32/5555_2.png) [@tcrockett](https://forums.docker.com/u/tcrockett)\
**Post date:** [March 10, 2016, 10:50pm UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/4 "2016-03-10T22:50:03Z")

</div>

Here is what I use for an Apache container.

**startApache.sh**

> '#!/bin/bash  
> apachectl -DFOREGROUND

**Dockerfile**

> FROM debian

> RUN apt-get update && apt-get install -y   
> apache2   
> && rm -rf /var/lib/apt/lists/\*

> ENV APPDIR myapp  
> RUN mkdir -p “$APPDIR/bin” “$APPDIR/etc”  
> COPY \*.sh “$APPDIR/bin/”  
> WORKDIR $APPDIR

> EXPOSE 80 443  
> CMD “/$APPDIR/bin/startApache.sh”

Now, in startApache.sh, I could have done something like this:

> while ps -e | grep apache2  
> do  
> sleep 5  
> done

This script runs indefinitely as long as the apache2 process is running, therefore the container will not exit until this script exits. In reality, you would want better error trapping, etc. in a script like this, but it get the point across. You should be able to use something similar to this for your use case.

I know it isn’t the most elegant solution, but it is workable.

---

<div class="post-metadata">

**Author:** ![orons](https://avatars.discourse-cdn.com/v4/letter/o/3bc359/32.png) [@orons](https://forums.docker.com/u/orons)\
**Post date:** [March 13, 2016, 7:15am UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/5 "2016-03-13T07:15:22Z")

</div>

I don’t think I will implement this kind of a solution on production : ))

EDIT:

I do believe that 2 containers, one contains NGINX and the other NodeJS, is a better solution, but I prefer one container for both

---

<div class="post-metadata">

**Author:** ![tcrockett](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/tcrockett/32/5555_2.png) [@tcrockett](https://forums.docker.com/u/tcrockett)\
**Post date:** [March 14, 2016, 7:50pm UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/6 "2016-03-14T19:50:24Z")

</div>

Have you tried this as the CMD?

> nginx -g ‘daemon off;’

In your first post, you did mention you tried a daemon-off option, but wanted to confirm if you specified it like this or in the conf file…

---

<div class="post-metadata">

**Author:** ![orons](https://avatars.discourse-cdn.com/v4/letter/o/3bc359/32.png) [@orons](https://forums.docker.com/u/orons)\
**Post date:** [March 14, 2016, 9:48pm UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/7 "2016-03-14T21:48:49Z")

</div>

Did it in the Dockerfile  
I don’t remember if it was CMD, RUN or maybe tried both options one way or another

---

<div class="post-metadata">

**Author:** ![tcrockett](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/tcrockett/32/5555_2.png) [@tcrockett](https://forums.docker.com/u/tcrockett)\
**Post date:** [March 15, 2016, 7:43pm UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/8 "2016-03-15T19:43:15Z")

</div>

Something like this should work. The container started with nginx in the foreground so it didn’t exit. To test, I was able to bring up the welcome page on port 80.

Of course, you’ll need to tweak to add npm, etc…

Dockerfile

> FROM ubuntu

> RUN apt-get update && apt-get install -y   
> nginx   
> && rm -rf /var/lib/apt/lists/\*

> COPY nginx.conf /

> ENTRYPOINT /usr/sbin/nginx -g ‘daemon off;’

Run

> docker run -d -p 80:80 tcrockett/nginx

---

<div class="post-metadata">

**Author:** ![mattthankachan](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@mattthankachan](https://forums.docker.com/u/mattthankachan)\
**Post date:** [April 13, 2020, 9:52am UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/9 "2020-04-13T09:52:34Z")

</div>

I am wondering what was the solution for the problem. I also wanted to use nginx as reverse proxy on the same server. I dont want to create reverse proxy container

---

<div class="post-metadata">

**Author:** ![maximization](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/maximization/32/26464_2.png) [@maximization](https://forums.docker.com/u/maximization)\
**Post date:** [April 13, 2020, 11:27am UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/10 "2020-04-13T11:27:20Z")

</div>

May I ask why you want to run Nginx and Node in one container? It’ll save you a lot of time if you go with the industry default and run each in a separate container, instead of banging your head on making it work in a single container.

---

<div class="post-metadata">

**Author:** ![mattthankachan](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@mattthankachan](https://forums.docker.com/u/mattthankachan)\
**Post date:** [April 13, 2020, 1:36pm UTC](https://forums.docker.com/t/run-nodejs-and-nginx/7279/11 "2020-04-13T13:36:00Z")

</div>

Hi Maxim,

I am creating containers in Azure container instance and wanted run a simple site (rather than hosting it via app service)

Even though not standard approach i was able to achieve running a shell command in docker file

#copy the reverse proxy file which redirects port 3000 traffic to port 80 nginx server

COPY reverse\_proxy.app /etc/nginx/sites-available/default  
**CMD** [“sh”,"./start.sh"]

# start.sh

#restart the service

service nginx --full-restart

#Run to start the node instance on container

node ./bin/www

# reverse\_proxy.app

server{  
listen 80;  
location / {

```
    proxy_pass "http://localhost:3000";
}

```

}
