Nginx/Node/Redis and Flask/Redis Compose example

Signed-off-by: ajeetraina <ajeetraina@gmail.com>
This commit is contained in:
ajeetraina 2022-03-13 12:31:04 +05:30
parent 019c555e4f
commit 28fd61d5ca
10 changed files with 10 additions and 138 deletions

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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',

View File

@ -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"]

View File

@ -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"
}

View File

@ -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');
});

View File

@ -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"]

View File

@ -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"
}

View File

@ -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');
});