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 8b75254

Browse files
committedJun 22, 2024·
feat(examples): Add nginx-nodejs-redis compose example
This example was derived from the analog example from docker/awesome-compose. Signed-off-by: Luca Seritan <luca.seritan@gmail.com>
1 parent 3dd4c28 commit 8b75254

File tree

10 files changed

+937
-0
lines changed

10 files changed

+937
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: examples/nginx-nodejs-redis
2+
3+
on:
4+
repository_dispatch:
5+
types: [core_merge]
6+
7+
workflow_dispatch:
8+
9+
schedule:
10+
- cron: '0 0 * * *' # Everyday at 12AM
11+
12+
push:
13+
branches: [main]
14+
paths:
15+
- 'examples/nginx-nodejs-redis/**'
16+
- '.github/workflows/examples-nginx-nodejs-redis.yaml'
17+
- '!examples/nginx-nodejs-redis/README.md'
18+
19+
pull_request:
20+
types: [opened, synchronize, reopened]
21+
branches: [main]
22+
paths:
23+
- 'examples/nginx-nodejs-redis/**'
24+
- '.github/workflows/examples-nginx-nodejs-redis.yaml'
25+
- '!examples/nginx-nodejs-redis/README.md'
26+
27+
jobs:
28+
up:
29+
runs-on: ubuntu-latest
30+
31+
steps:
32+
- uses: actions/checkout@v3
33+
34+
- name: kraft compose up
35+
uses: unikraft/kraftkit@staging
36+
with:
37+
loglevel: debug
38+
workdir: examples/nginx-nodejs-redis
39+
runtimedir: /github/workspace/.kraftkit
40+
privileged: true
41+
run: |
42+
set -xe
43+
44+
sudo KRAFTKIT_NO_WARN_SUDO=1 kraft compose up -d
45+
curl -s localhost:8080
46+
curl -s localhost:8080
47+

‎examples/nginx-nodejs-redis/README.md

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

0 commit comments

Comments
 (0)
Please sign in to comment.