Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 76eb80c

Browse files
committedJun 3, 2024·
feat(examples): Add nginx-nodejs-redis compose example
This example was derived from the analog example from docker/awesome-compose.
1 parent 3dd4c28 commit 76eb80c

File tree

9 files changed

+675
-0
lines changed

9 files changed

+675
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
## Compose sample application
2+
3+
## Node.js application with Nginx proxy and Redis database
4+
5+
This example was derived from the [nginx-nodejs-redis docker/awesome-compose example](https://github.com/docker/awesome-compose/tree/master/nginx-nodejs-redis).
6+
7+
Project structure:
8+
```
9+
.
10+
├── README.md
11+
├── compose.yaml
12+
├── nginx
13+
│   └── nginx.conf
14+
└── web
15+
├── Kraftfile
16+
├── Dockerfile
17+
├── package.json
18+
└── server.js
19+
20+
2 directories, 7 files
21+
22+
23+
```
24+
[_compose.yaml_](compose.yaml)
25+
```
26+
services:
27+
redis:
28+
image: redis:7.2
29+
ports:
30+
- '6379:6379'
31+
networks:
32+
internal:
33+
ipv4_address: 172.23.0.2
34+
mem_reservation: 512M
35+
36+
web1:
37+
build: ./web
38+
hostname: web1
39+
networks:
40+
internal:
41+
ipv4_address: 172.23.0.3
42+
mem_reservation: 512M
43+
depends_on:
44+
- redis
45+
46+
web2:
47+
build: ./web
48+
hostname: web2
49+
networks:
50+
internal:
51+
ipv4_address: 172.23.0.4
52+
mem_reservation: 512M
53+
depends_on:
54+
- redis
55+
56+
nginx:
57+
image: nginx:1.25
58+
ports:
59+
- '8080:80'
60+
volumes:
61+
- ./nginx:/etc/nginx
62+
depends_on:
63+
- web1
64+
- web2
65+
networks:
66+
internal:
67+
68+
networks:
69+
internal:
70+
ipam:
71+
config:
72+
- subnet: 172.23.0.1/16
73+
```
74+
The compose file defines an application with four services `redis`, `nginx`, `web1` and `web2`.
75+
When deploying the application, docker compose maps port 80 of the nginx service VM to port 8080 of the host as specified in the file.
76+
77+
78+
> **_INFO_**
79+
> Redis runs on port 6379 by default. Make sure port 6379 on the host is not being used by another VM, otherwise the port should be changed.
80+
81+
## Deploy with kraft compose
82+
83+
```
84+
$ kraft compose up -d
85+
nginx-nodejs-redis-redis
86+
nginx-nodejs-redis-web1
87+
nginx-nodejs-redis-web2
88+
nginx-nodejs-redis-nginx
89+
```
90+
91+
92+
## Expected result
93+
94+
Listing VMs must show four VMs running and the port mapping as below:
95+
96+
97+
```
98+
$ kraft compose ps
99+
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
100+
nginx-nodejs-redis-nginx oci://nginx:1.25 /usr/bin/nginx 2 minutes ago running 64M 0.0.0.0:8080->80/tcp qemu/x86_64
101+
nginx-nodejs-redis-redis oci://redis:7.2 /usr/bin/redis-server /etc/redis/redis.conf 2 minutes ago running 512M 0.0.0.0:6379->6379/tcp qemu/x86_64
102+
nginx-nodejs-redis-web1 oci://unikraft.org/node:21 /usr/bin/node /usr/src/server.js 2 minutes ago running 512M qemu/x86_64
103+
nginx-nodejs-redis-web2 oci://unikraft.org/node:21 /usr/bin/node /usr/src/server.js 2 minutes ago running 512M qemu/x86_64
104+
```
105+
106+
## Testing the app
107+
108+
After the application starts, navigate to `http://localhost:8080` in your web browser or run:
109+
110+
```
111+
curl localhost:8080
112+
web1: Total number of visits is: 1
113+
```
114+
115+
```
116+
curl localhost:8080
117+
web2: Total number of visits is: 2
118+
```
119+
120+
```
121+
$ curl localhost:8080
122+
web1: Total number of visits is: 3
123+
```
124+
125+
## Stop and remove the VMs
126+
127+
```
128+
$ kraft compose down
129+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
services:
2+
redis:
3+
image: redis:7.2
4+
ports:
5+
- '6379:6379'
6+
networks:
7+
internal:
8+
ipv4_address: 172.23.0.2
9+
mem_reservation: 512M
10+
11+
web1:
12+
build: ./web
13+
hostname: web1
14+
networks:
15+
internal:
16+
ipv4_address: 172.23.0.3
17+
mem_reservation: 512M
18+
depends_on:
19+
- redis
20+
21+
web2:
22+
build: ./web
23+
hostname: web2
24+
networks:
25+
internal:
26+
ipv4_address: 172.23.0.4
27+
mem_reservation: 512M
28+
depends_on:
29+
- redis
30+
31+
nginx:
32+
image: nginx:1.25
33+
ports:
34+
- '8080:80'
35+
volumes:
36+
- ./nginx:/etc/nginx
37+
depends_on:
38+
- web1
39+
- web2
40+
networks:
41+
internal:
42+
43+
networks:
44+
internal:
45+
ipam:
46+
config:
47+
- subnet: 172.23.0.1/16
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
worker_processes 1;
2+
daemon off;
3+
master_process off;
4+
user root root;
5+
6+
events {
7+
worker_connections 32;
8+
}
9+
10+
http {
11+
error_log stderr error;
12+
access_log off;
13+
14+
keepalive_timeout 10s;
15+
keepalive_requests 10000;
16+
send_timeout 10s;
17+
18+
upstream backend_servers {
19+
server 172.23.0.3:5000;
20+
server 172.23.0.4:5000;
21+
}
22+
23+
server {
24+
listen 80;
25+
server_name localhost;
26+
27+
location / {
28+
proxy_pass http://backend_servers;
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM node:21-alpine AS build
2+
3+
WORKDIR /usr/src
4+
5+
COPY . /usr/src/
6+
RUN npm install
7+
8+
FROM scratch
9+
10+
COPY --from=build /etc/os-release /etc/os-release
11+
COPY --from=build /usr/src/node_modules /usr/src/node_modules
12+
COPY --from=build /usr/src/server.js /usr/src/server.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spec: v0.6
2+
3+
runtime: node:21
4+
5+
rootfs: ./Dockerfile
6+
7+
cmd: ["/usr/bin/node", "/usr/src/server.js"]

‎examples/compose/nginx-nodejs-redis/web/package-lock.json

+408
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "web",
3+
"version": "1.0.0",
4+
"description": "Running Node.js and Express.js on Docker",
5+
"main": "server.js",
6+
"scripts": {
7+
"start": "node server.js"
8+
},
9+
"dependencies": {
10+
"express": "^4.17.2",
11+
"redis": "3.1.2"
12+
},
13+
"author": "",
14+
"license": "MIT"
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const os = require('os');
2+
const express = require('express');
3+
const app = express();
4+
const redis = require('redis');
5+
const redisClient = redis.createClient({
6+
host: '172.23.0.2',
7+
port: 6379
8+
});
9+
10+
app.get('/', function(req, res) {
11+
redisClient.get('numVisits', function(err, numVisits) {
12+
numVisitsToDisplay = parseInt(numVisits) + 1;
13+
if (isNaN(numVisitsToDisplay)) {
14+
numVisitsToDisplay = 1;
15+
}
16+
res.send(os.hostname() +': Number of visits is: ' + numVisitsToDisplay);
17+
numVisits++;
18+
redisClient.set('numVisits', numVisits);
19+
});
20+
});
21+
22+
app.listen(5000, function() {
23+
console.log('Web application is listening on port 5000');
24+
});

0 commit comments

Comments
 (0)
Please sign in to comment.