|
| 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 | +``` |
0 commit comments