Skip to content

Commit

Permalink
feat(examples): Add nginx-nodejs-redis compose example
Browse files Browse the repository at this point in the history
This example was derived from the analog example from docker/awesome-compose.
  • Loading branch information
LucaSeri committed Jun 3, 2024
1 parent 3dd4c28 commit 7a26f92
Show file tree
Hide file tree
Showing 9 changed files with 675 additions and 0 deletions.
129 changes: 129 additions & 0 deletions examples/compose/nginx-nodejs-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
## Compose sample application

## Node.js application with Nginx proxy and Redis database

This example was derived from the [nginx-nodejs-redis docker/awesome-compose example](https://github.com/docker/awesome-compose/tree/master/nginx-nodejs-redis).

Project structure:
```
.
├── README.md
├── compose.yaml
├── nginx
│   └── nginx.conf
└── web
├── Kraftfile
├── Dockerfile
├── package.json
└── server.js
2 directories, 7 files
```
[_compose.yaml_](compose.yaml)
```
services:
redis:
image: redis:7.2
ports:
- '6379:6379'
networks:
internal:
ipv4_address: 172.23.0.2
mem_reservation: 512M
web1:
build: ./web
hostname: web1
networks:
internal:
ipv4_address: 172.23.0.3
mem_reservation: 512M
depends_on:
- redis
web2:
build: ./web
hostname: web2
networks:
internal:
ipv4_address: 172.23.0.4
mem_reservation: 512M
depends_on:
- redis
nginx:
image: nginx:1.25
ports:
- '8080:80'
volumes:
- ./nginx:/etc/nginx
depends_on:
- web1
- web2
networks:
internal:
networks:
internal:
ipam:
config:
- subnet: 172.23.0.1/16
```
The compose file defines an application with four services `redis`, `nginx`, `web1` and `web2`.
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.


> **_INFO_**
> 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.
## Deploy with kraft compose

```
$ kraft compose up -d
nginx-nodejs-redis-redis
nginx-nodejs-redis-web1
nginx-nodejs-redis-web2
nginx-nodejs-redis-nginx
```


## Expected result

Listing VMs must show four VMs running and the port mapping as below:


```
$ kraft compose ps
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
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
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
nginx-nodejs-redis-web1 oci://unikraft.org/node:21 /usr/bin/node /usr/src/server.js 2 minutes ago running 512M qemu/x86_64
nginx-nodejs-redis-web2 oci://unikraft.org/node:21 /usr/bin/node /usr/src/server.js 2 minutes ago running 512M qemu/x86_64
```

## Testing the app

After the application starts, navigate to `http://localhost:8080` in your web browser or run:

```
curl localhost:8080
web1: Total number of visits is: 1
```

```
curl localhost:8080
web2: Total number of visits is: 2
```

```
$ curl localhost:8080
web1: Total number of visits is: 3
```

## Stop and remove the VMs

```
$ kraft compose down
```
48 changes: 48 additions & 0 deletions examples/compose/nginx-nodejs-redis/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
services:
redis:
image: redis:7.2
ports:
- '6379:6379'
networks:
internal:
ipv4_address: 172.23.0.2
mem_reservation: 512M

web1:
build: ./web
hostname: web1
networks:
internal:
ipv4_address: 172.23.0.3
mem_reservation: 512M
depends_on:
- redis

web2:
build: ./web
hostname: web2
networks:
internal:
ipv4_address: 172.23.0.4
mem_reservation: 512M
depends_on:
- redis

nginx:
image: nginx:1.25
ports:
- '8080:80'
volumes:
- ./nginx:/etc/nginx
depends_on:
- web1
- web2
networks:
internal:

networks:
internal:
ipam:
config:
- subnet: 172.23.0.1/16

31 changes: 31 additions & 0 deletions examples/compose/nginx-nodejs-redis/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
worker_processes 1;
daemon off;
master_process off;
user root root;

events {
worker_connections 32;
}

http {
error_log stderr error;
access_log off;

keepalive_timeout 10s;
keepalive_requests 10000;
send_timeout 10s;

upstream backend_servers {
server 172.23.0.3:5000;
server 172.23.0.4:5000;
}

server {
listen 80;
server_name localhost;

location / {
proxy_pass http://backend_servers;
}
}
}
1 change: 1 addition & 0 deletions examples/compose/nginx-nodejs-redis/web/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
12 changes: 12 additions & 0 deletions examples/compose/nginx-nodejs-redis/web/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:21-alpine AS build

WORKDIR /usr/src

COPY . /usr/src/
RUN npm install

FROM scratch

COPY --from=build /etc/os-release /etc/os-release
COPY --from=build /usr/src/node_modules /usr/src/node_modules
COPY --from=build /usr/src/server.js /usr/src/server.js
7 changes: 7 additions & 0 deletions examples/compose/nginx-nodejs-redis/web/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spec: v0.6

runtime: node:21

rootfs: ./Dockerfile

cmd: ["/usr/bin/node", "/usr/src/server.js"]
Loading

0 comments on commit 7a26f92

Please sign in to comment.