Back to home

Docker application with Nginx and Nodejs main image

Docker application with Nginx and Nodejs

First create a directory for both nginx and nodejs.

Within the Node folder, create a Dockerfile that contains the following to expose an app running on a particular port:

1 2 3 4 5 6 7 8 FROM mhart/alpine-node # Pretend to copy the node app entry # from current folder COPY index.js . # Expose the port that it is running on EXPOSE 3000 # Run node - chances are you want pm2 here CMD node index.js

Then we can build with docker built -t foo/node .

After the build, run the container with port 3000 exposed using docker run -d -p 3000:3000 --name node-app foo/node.

First, we can test the Nginx latest container container using docker run --rm -p 8000:80 nginx to test out nginx:latest. This just removes the container after running.

In the nginx folder, we can create default.conf file to overwrite the initial one.

If settings are not defined below, Nginx will use the default values.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 # config server { location / { # host name first proxy_set_header Host $host; # extra headers for host IP address proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # passing protocol used (http | https) proxy_set_header X-Forwarded-Proto $scheme; # where to proxy reqs to proxy_pass http://app:3000; } }

For the Docker file.

1 2 FROM nginx COPY default.cong /etc/nginx/conf.d/

Then build out this file using docker built -t foo/nginx ..

Running it: docker run -p 8000:80 --link node-app:app --name nginx-proxy foo/nginx

Since we expose 3000 for the node-app, we do not need to redo that

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.