From 28fd61d5cabd398d6554e11f0cf8d0484548168c Mon Sep 17 00:00:00 2001 From: ajeetraina Date: Sun, 13 Mar 2022 12:31:04 +0530 Subject: [PATCH] Nginx/Node/Redis and Flask/Redis Compose example Signed-off-by: ajeetraina --- flask-redis/README.md | 9 ------- nginx-nodejs-redis/README.md | 34 ++++----------------------- nginx-nodejs-redis/docker-compose.yml | 6 +++-- nginx-nodejs-redis/web/server.js | 3 +-- nginx-nodejs-redis/web1/Dockerfile | 9 ------- nginx-nodejs-redis/web1/package.json | 15 ------------ nginx-nodejs-redis/web1/server.js | 24 ------------------- nginx-nodejs-redis/web2/Dockerfile | 9 ------- nginx-nodejs-redis/web2/package.json | 15 ------------ nginx-nodejs-redis/web2/server.js | 24 ------------------- 10 files changed, 10 insertions(+), 138 deletions(-) delete mode 100644 nginx-nodejs-redis/web1/Dockerfile delete mode 100644 nginx-nodejs-redis/web1/package.json delete mode 100644 nginx-nodejs-redis/web1/server.js delete mode 100644 nginx-nodejs-redis/web2/Dockerfile delete mode 100644 nginx-nodejs-redis/web2/package.json delete mode 100644 nginx-nodejs-redis/web2/server.js diff --git a/flask-redis/README.md b/flask-redis/README.md index b7f0414..7f2b42f 100644 --- a/flask-redis/README.md +++ b/flask-redis/README.md @@ -35,15 +35,6 @@ services: ``` $ docker-compose up -d -Creating network "flask-redis_default" with the default driver -Pulling redis (redislabs/redismod:)... -... -... -web_1 | WARNING: This is a development server. Do not use it in a production deployment. -web_1 | * Running on http://172.19.0.3:5000/ (Press CTRL+C to quit) -web_1 | * Restarting with stat -web_1 | * Debugger is active! -web_1 | * Debugger PIN: 598-320-965 ``` ## Expected result diff --git a/nginx-nodejs-redis/README.md b/nginx-nodejs-redis/README.md index 3c0fe10..c54bb77 100644 --- a/nginx-nodejs-redis/README.md +++ b/nginx-nodejs-redis/README.md @@ -5,26 +5,20 @@ Project structure: ``` . +├── README.md ├── docker-compose.yml ├── nginx │   ├── Dockerfile │   └── nginx.conf -├── web -│   ├── Dockerfile -│   ├── package.json -│   └── server.js -├── web1 -│   ├── Dockerfile -│   ├── package.json -│   └── server.js -└── web2 +└── web ├── Dockerfile ├── package.json └── server.js -4 directories, 12 files -``` +2 directories, 7 files + +``` [_docker-compose.yml_](docker-compose.yml) ``` redis: @@ -60,10 +54,6 @@ When deploying the application, docker-compose maps port 80 of the nginx service ``` $ docker-compose up -d -Creating nginx-nodejs-redis_redis_1 ... done -Creating nginx-nodejs-redis_web1_1 ... done -Creating nginx-nodejs-redis_web2_1 ... done -Creating nginx-nodejs-redis_nginx_1 ... done ``` @@ -74,16 +64,6 @@ Listing containers must show three containers running and the port mapping as be ``` docker-compose ps - Name Command State Ports ------------------------------------------------------------------------------------------- -nginx-nodejs-redis_nginx_1 /docker-entrypoint.sh ngin Up 0.0.0.0:80->80/tcp - ... -nginx-nodejs-redis_redis_1 docker-entrypoint.sh redis Up 0.0.0.0:6379->6379/tcp - ... -nginx-nodejs-redis_web1_1 docker-entrypoint.sh npm Up 0.0.0.0:81->5000/tcp - start -nginx-nodejs-redis_web2_1 docker-entrypoint.sh npm Up 0.0.0.0:82->5000/tcp - start ``` ## Testing the app @@ -105,10 +85,6 @@ $ curl localhost:80 web2: Total number of visits is: 3 ``` -``` -$ curl localhost:80 -web2: Total number of visits is: 4 -``` ## Stop and remove the containers diff --git a/nginx-nodejs-redis/docker-compose.yml b/nginx-nodejs-redis/docker-compose.yml index 4d08922..7f3748c 100644 --- a/nginx-nodejs-redis/docker-compose.yml +++ b/nginx-nodejs-redis/docker-compose.yml @@ -6,12 +6,14 @@ services: - '6379:6379' web1: restart: on-failure - build: ./web1 + build: ./web + hostname: web1 ports: - '81:5000' web2: restart: on-failure - build: ./web2 + build: ./web + hostname: web2 ports: - '82:5000' nginx: diff --git a/nginx-nodejs-redis/web/server.js b/nginx-nodejs-redis/web/server.js index 9061c10..432c144 100644 --- a/nginx-nodejs-redis/web/server.js +++ b/nginx-nodejs-redis/web/server.js @@ -1,5 +1,4 @@ -const express = require('express'); -const redis = require('redis'); +const os = require('os'); const app = express(); const redisClient = redis.createClient({ host: 'redis', diff --git a/nginx-nodejs-redis/web1/Dockerfile b/nginx-nodejs-redis/web1/Dockerfile deleted file mode 100644 index 60629c2..0000000 --- a/nginx-nodejs-redis/web1/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM node:14.17.3-alpine3.14 - -WORKDIR /usr/src/app - -COPY ./package*.json ./ -RUN npm install -COPY ./server.js ./ - -CMD ["npm","start"] diff --git a/nginx-nodejs-redis/web1/package.json b/nginx-nodejs-redis/web1/package.json deleted file mode 100644 index dcb7bac..0000000 --- a/nginx-nodejs-redis/web1/package.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "web1", - "version": "1.0.0", - "description": "Running Node.js and Express.js on Docker", - "main": "server.js", - "scripts": { - "start": "node server.js" - }, - "dependencies": { - "express": "^4.17.2", - "redis": "3.1.2" - }, - "author": "", - "license": "MIT" -} diff --git a/nginx-nodejs-redis/web1/server.js b/nginx-nodejs-redis/web1/server.js deleted file mode 100644 index 7be500e..0000000 --- a/nginx-nodejs-redis/web1/server.js +++ /dev/null @@ -1,24 +0,0 @@ -const express = require('express'); -const redis = require('redis'); -const app = express(); -const redisClient = redis.createClient({ - host: 'redis', - port: 6379 -}); - - -app.get('/', function(req, res) { - redisClient.get('numVisits', function(err, numVisits) { - numVisitsToDisplay = parseInt(numVisits) + 1; - if (isNaN(numVisitsToDisplay)) { - numVisitsToDisplay = 1; - } - res.send('web1: Total number of visits is: ' + numVisitsToDisplay); - numVisits++; - redisClient.set('numVisits', numVisits); - }); -}); - -app.listen(5000, function() { - console.log('Web app is listening on port 5000'); -}); diff --git a/nginx-nodejs-redis/web2/Dockerfile b/nginx-nodejs-redis/web2/Dockerfile deleted file mode 100644 index e0d580c..0000000 --- a/nginx-nodejs-redis/web2/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM node:14.17.3-alpine3.14 - -WORKDIR /usr/src/app - -COPY ./package.json ./ -RUN npm install -COPY ./server.js ./ - -CMD ["npm","start"] diff --git a/nginx-nodejs-redis/web2/package.json b/nginx-nodejs-redis/web2/package.json deleted file mode 100644 index 51e85c5..0000000 --- a/nginx-nodejs-redis/web2/package.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "web2", - "version": "1.0.0", - "description": "Running Node.js and Express.js on Docker", - "main": "server.js", - "scripts": { - "start": "node server.js" - }, - "dependencies": { - "express": "^4.17.2", - "redis": "3.1.2" - }, - "author": "", - "license": "MIT" -} diff --git a/nginx-nodejs-redis/web2/server.js b/nginx-nodejs-redis/web2/server.js deleted file mode 100644 index 36c4ce7..0000000 --- a/nginx-nodejs-redis/web2/server.js +++ /dev/null @@ -1,24 +0,0 @@ -const express = require('express'); -const redis = require('redis'); -const app = express(); -const redisClient = redis.createClient({ - host: 'redis', - port: 6379 -}); - - -app.get('/', function(req, res) { - redisClient.get('numVisits', function(err, numVisits) { - numVisitsToDisplay = parseInt(numVisits) + 1; - if (isNaN(numVisitsToDisplay)) { - numVisitsToDisplay = 1; - } - res.send('web2: Total number of visits is: ' + numVisitsToDisplay); - numVisits++; - redisClient.set('numVisits', numVisits); - }); -}); - -app.listen(5000, function() { - console.log('Web app is listening on port 5000'); -});