0

I am fairly new to docker. I have to build a docker file that will start node express server and Nginx as a reverse proxy. but the problem is node server starts but Nginx does not start. when I manually exec into the container and start it by typing "nginx" and then pressing enter it start and the container listens to port 80 and Nginx works this way. but I want it to start as soon as the container start.

Here is my Dockerfile:

FROM alpine
RUN apk add --update nodejs nodejs-npm
RUN apk add nginx
WORKDIR /usr/
COPY package*.json ./
RUN npm install
COPY . .
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
RUN nginx #<-- this does not work!!
CMD npm run start
EXPOSE 80

This is an API server listening on 8080 so Nginx will reverse proxy and provide it to port 80 to the outside world. I can achieve it by running 2 containers one for Nginx and one for nodejs using docker-compose but I want to run both in One Container.

4
  • RUN commands in a Dockerfile are buildsteps. This command would be executed in an intermediary container. You should replace it with CMD
    – Malik
    Commented Apr 17, 2021 at 8:25
  • You also dont have to install nginx yourself. You could simply import the nginx image from docker hub hub.docker.com/_/nginx
    – Malik
    Commented Apr 17, 2021 at 8:27
  • @Malik Replacing RUN with CMD also doesn't work Commented Apr 17, 2021 at 8:27
  • I know it simply as importing it from the docker hub but it increases the size of the image. Commented Apr 17, 2021 at 8:28

2 Answers 2

0

After reading your qustion again, I wonder why you need a reverse proxy anyway? You should patch your node application so that it itself serves port 80.

The idea behind Docker container is "One container, one process" anyway. This paradigm gets ignored a lot, but you should not serve a reverse proxy in a container, that itself can serve as a webserver.

-1

We need to separate out the node JS and nginx processes into separate containers. They both can recide on the same network and talk to each other, containers talk to each other by name. You can receive packet on nginx reverse proxy and then forward that to nodeJS application. In nodeJS application your npm start with the primary process and in nginx application your service demon of nginx should be the primary process. The container is always alive till the time the primary process is up and running so in the world container as we separate each of the the individual functionalities and deploy them as containers. In you case nodeJS application should be a separate container and nginx should be a separate container. Only for nginx container you would be exposing the port for external connectivity.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .