Post

NGINX: NGINX demo using Docker

NGINX: NGINX demo using Docker

Nginx demo using Docker

Docker command to install and run nginx

docker run –name nginx –hostname ng1 -p 80:80 -d nginx

//inspect ccontainer docker inspect nginx

index.js

1
2
3
4
5
6
7
const express = require("express");
const app = express();
const os = require("os")
const hostname = os.hostname();

app.get("/", (req, res) => res.send("Hello from " + hostname ) ) ;
app.listen(8080, () => console.log("listening on port 8080 on " + hostname));

package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  }
}

Install and run app

1
2
3
npm install
node index.js

Dockerfile

1
2
3
4
5
FROM node:12
WORKDIR /home/node/app
COPY app /home/node/app
RUN npm install
CMD node index.js

create nodeapp

docker build . -t nodeapp docker run –hostname nodeapp1 –name nodeapp1 -d nodeapp docker run –hostname nodeapp2 –name nodeapp2 -d nodeapp docker run –hostname nodeapp3 –name nodeapp3 -d nodeapp

nginx.config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
http {
    upstream nodebackend {
         server nodeapp1:8080;
				 server nodeapp2:8080;
				 server nodeapp3:8080;
    }

   server {
        listen 8080;
        location / {
             proxy_pass http://nodebackend/;
         }
    }

}

events
{

}

Start Nginx

1
docker run --hostname ng1 --name nginx -p 80:8080 -v /Users/pravin.tripathi/Desktop/nginx-udemy-container/nginx.conf:/etc/nginx/nginx.conf -d nginx

you will find something like below

1
2
3
4
5
6
7
8
9
10
11
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/11/21 09:57:08 [emerg] 1#1: host not found in upstream "nodeapp1:8080" in /etc/nginx/nginx.conf:6
nginx: [emerg] host not found in upstream "nodeapp1:8080" in /etc/nginx/nginx.conf:6

In reality nginx stopped . all are running just that they are not able to interact with each other and nginx throw error and goes down. We have to put all of them in same network.

1
2
3
4
5
docker network create backendnet
docker network connect backendnet nodeapp1
docker network connect backendnet nodeapp2
docker network connect backendnet nodeapp3
docker network connect backendnet nginx
1
docker start nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/11/21 09:57:08 [emerg] 1#1: host not found in upstream "nodeapp1:8080" in /etc/nginx/nginx.conf:6
nginx: [emerg] host not found in upstream "nodeapp1:8080" in /etc/nginx/nginx.conf:6
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

It is up now.

image.png

Let spin up another nginx container in above network.

1
docker run --hostname ng2 --name nginx2 -p 81:8080 -v /Users/pravin.tripathi/Desktop/nginx-udemy-container/nginx.conf:/etc/nginx/nginx.conf -d nginx

Add to network,

1
docker network connect backendnet nginx2

nginx-udemy-container 2.zip

Back to Parent Page

This post is licensed under CC BY 4.0 by the author.