What we are trying to do?
What we will do?
Steps
  • Create a node application
  • Create and run docker container for node application
  • Create and run docker container for nginx by linking nginx container to node application container
Environment
  • Node server will be running on port 3000 inside the container
  • Nginx server will be running on port 80 inside the container
  • We are going to connect on port 80 to the nginx docker container
  • Nginx docker container will proxy the request to node application container on port 3000
Let’s start

Step 1.
Create following directory structure anywhere you want:

->workspace
-> node-app
-> nginx

Create index.js file in the node-app folder containing following code:

var http = require('http');

var server = http.createServer(function(req, res){
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.end("Your server is running");
});

server.listen(3000);
console.log("Server running at http://localhost:3000");    

You can go to the browser and hit the following URL to test your server:

http://localhost:3000

Step 2.
Create Dockerfile in the node-app folder with following lines:

from node
COPY index.js  .
EXPOSE 3000
CMD node index.js

Explanation
We chose the default node image to build our node container
We copied index.js from current directory to docker container
We exposed port 3000 from docker container
We started node server inside docker container

Build your docker image by executing following command:

docker build -t ashutosh/node-test .

Explanation
-t is used to provide a name for the container.
ashutosh/node-test is the namespace/name of the container
. is the current directory where Dockerfile is present

Note: You may have to use sudo or administrator privileges to run docker commands

Now run your docker image with following command:

docker run -p 3000:3000 --name coc-server ashutosh/node-test

Explanation
-p represents port
3000:3000 means we want to access docker port 3000 by port 3000 (these can be different as well)
–name gives docker process a name
ashutosh/test is the name of the image we want to run

Step 3.
Create default.conf file in the workspace/nginx folder. This file will be used to override default Nginx configuration.
Add following lines to it:

server{
  location / {
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For     $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Proto $scheme;
     proxy_pass http://app:3000;
  }
}

Explanation
The important line here is proxy_pass which tells nginx to redirect all requests to http://app:3000. You can use any name instead of app

Next create the Dockerfile in the workspace/nginx folder with following lines:

from nginx
COPY default.conf /etc/nginx/conf.d/

Nginx reads all configurations from the conf.d folder, that is why we copied our default.conf there

Next build the Dockerimage with following command:

docker build -t ashutosh/nginx-proxy .

Finally run the image with following command:

docker run -d -p 80:80 --link  node-test:app --name nginx-proxy-test ashutosh/nginx-proxy

Explanation
-d says the image is to be run as a background process
–link connects our node application container to the proxy_pass in the nginx configuration