From 8369efa177cd674a7646c0395bf330b3571a7cc1 Mon Sep 17 00:00:00 2001 From: Luca Seritan Date: Mon, 3 Jun 2024 18:50:20 +0300 Subject: [PATCH] feat(examples): Add nginx-nodejs-redis compose example This example was derived from the analog example from docker/awesome-compose. --- .../examples-nginx-nodejs-redis.yaml | 46 ++ examples/nginx-nodejs-redis/README.md | 131 ++++++ examples/nginx-nodejs-redis/compose.yaml | 52 +++ examples/nginx-nodejs-redis/nginx/nginx.conf | 31 ++ examples/nginx-nodejs-redis/web/.gitignore | 1 + examples/nginx-nodejs-redis/web/Dockerfile | 12 + examples/nginx-nodejs-redis/web/Kraftfile | 7 + .../nginx-nodejs-redis/web/package-lock.json | 408 ++++++++++++++++++ examples/nginx-nodejs-redis/web/package.json | 15 + examples/nginx-nodejs-redis/web/server.js | 29 ++ 10 files changed, 732 insertions(+) create mode 100644 .github/workflows/examples-nginx-nodejs-redis.yaml create mode 100644 examples/nginx-nodejs-redis/README.md create mode 100644 examples/nginx-nodejs-redis/compose.yaml create mode 100644 examples/nginx-nodejs-redis/nginx/nginx.conf create mode 100644 examples/nginx-nodejs-redis/web/.gitignore create mode 100644 examples/nginx-nodejs-redis/web/Dockerfile create mode 100644 examples/nginx-nodejs-redis/web/Kraftfile create mode 100644 examples/nginx-nodejs-redis/web/package-lock.json create mode 100644 examples/nginx-nodejs-redis/web/package.json create mode 100644 examples/nginx-nodejs-redis/web/server.js diff --git a/.github/workflows/examples-nginx-nodejs-redis.yaml b/.github/workflows/examples-nginx-nodejs-redis.yaml new file mode 100644 index 00000000..9b285f53 --- /dev/null +++ b/.github/workflows/examples-nginx-nodejs-redis.yaml @@ -0,0 +1,46 @@ +name: examples/nginx-nodejs-redis + +on: + repository_dispatch: + types: [core_merge] + + workflow_dispatch: + + schedule: + - cron: '0 0 * * *' # Everyday at 12AM + + push: + branches: [main] + paths: + - 'examples/nginx-nodejs-redis/**' + - '.github/workflows/examples-nginx-nodejs-redis.yaml' + - '!examples/nginx-nodejs-redis/README.md' + + pull_request: + types: [opened, synchronize, reopened] + branches: [main] + paths: + - 'examples/nginx-nodejs-redis/**' + - '.github/workflows/examples-nginx-nodejs-redis.yaml' + - '!examples/nginx-nodejs-redis/README.md' + +jobs: + up: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: kraft compose up + uses: unikraft/kraftkit@staging + with: + loglevel: debug + workdir: examples/nginx-nodejs-redis + runtimedir: /github/workspace/.kraftkit + privileged: true + run: | + set -xe + + sudo KRAFTKIT_NO_WARN_SUDO=1 kraft compose up -d + curl -s localhost:8080 + curl -s localhost:8080 \ No newline at end of file diff --git a/examples/nginx-nodejs-redis/README.md b/examples/nginx-nodejs-redis/README.md new file mode 100644 index 00000000..f2c7a09a --- /dev/null +++ b/examples/nginx-nodejs-redis/README.md @@ -0,0 +1,131 @@ +## 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: + +```bash +. +├── README.md +├── compose.yaml +├── nginx +│   └── nginx.conf +└── web + ├── Kraftfile + ├── Dockerfile + ├── package.json + └── server.js + +2 directories, 7 files + + +``` + +[_compose.yaml_](compose.yaml) + +```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 + +```bash +$ 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: + +```bash +$ 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: + +```bash +curl localhost:8080 +web1: Total number of visits is: 1 +``` + +```bash +curl localhost:8080 +web2: Total number of visits is: 2 +``` + +```bash +$ curl localhost:8080 +web1: Total number of visits is: 3 +``` + +## Stop and remove the VMs + +```bash +kraft compose down +``` diff --git a/examples/nginx-nodejs-redis/compose.yaml b/examples/nginx-nodejs-redis/compose.yaml new file mode 100644 index 00000000..cc307061 --- /dev/null +++ b/examples/nginx-nodejs-redis/compose.yaml @@ -0,0 +1,52 @@ +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 + ports: + - '81:5000' + networks: + internal: + ipv4_address: 172.23.0.3 + mem_reservation: 512M + depends_on: + - redis + + web2: + build: ./web + hostname: web2 + ports: + - '82:5000' + 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 + diff --git a/examples/nginx-nodejs-redis/nginx/nginx.conf b/examples/nginx-nodejs-redis/nginx/nginx.conf new file mode 100644 index 00000000..07b90b90 --- /dev/null +++ b/examples/nginx-nodejs-redis/nginx/nginx.conf @@ -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; + } + } +} diff --git a/examples/nginx-nodejs-redis/web/.gitignore b/examples/nginx-nodejs-redis/web/.gitignore new file mode 100644 index 00000000..3c3629e6 --- /dev/null +++ b/examples/nginx-nodejs-redis/web/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/examples/nginx-nodejs-redis/web/Dockerfile b/examples/nginx-nodejs-redis/web/Dockerfile new file mode 100644 index 00000000..46e7cd3c --- /dev/null +++ b/examples/nginx-nodejs-redis/web/Dockerfile @@ -0,0 +1,12 @@ +FROM node:21-alpine AS build + +WORKDIR /usr/src/app + +COPY package.json package-lock.json ./ +RUN npm ci + +FROM scratch + +COPY --from=build /etc/os-release /etc/os-release +COPY --from=build /usr/src/app/node_modules /usr/src/node_modules +COPY server.js /usr/src/server.js diff --git a/examples/nginx-nodejs-redis/web/Kraftfile b/examples/nginx-nodejs-redis/web/Kraftfile new file mode 100644 index 00000000..ee58848e --- /dev/null +++ b/examples/nginx-nodejs-redis/web/Kraftfile @@ -0,0 +1,7 @@ +spec: v0.6 + +runtime: node:21 + +rootfs: ./Dockerfile + +cmd: ["/usr/bin/node", "/usr/src/server.js"] diff --git a/examples/nginx-nodejs-redis/web/package-lock.json b/examples/nginx-nodejs-redis/web/package-lock.json new file mode 100644 index 00000000..14277225 --- /dev/null +++ b/examples/nginx-nodejs-redis/web/package-lock.json @@ -0,0 +1,408 @@ +{ + "name": "web", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "accepts": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", + "integrity": "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==", + "requires": { + "mime-types": "~2.1.34", + "negotiator": "0.6.3" + } + }, + "array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" + }, + "body-parser": { + "version": "1.19.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.2.tgz", + "integrity": "sha512-SAAwOxgoCKMGs9uUAUFHygfLAyaniaoun6I8mFY9pRAJL9+Kec34aU+oIjDhTycub1jozEfEwx1W1IuOYxVSFw==", + "requires": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.2", + "http-errors": "1.8.1", + "iconv-lite": "0.4.24", + "on-finished": "~2.3.0", + "qs": "6.9.7", + "raw-body": "2.4.3", + "type-is": "~1.6.18" + } + }, + "bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==" + }, + "content-disposition": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", + "integrity": "sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==", + "requires": { + "safe-buffer": "5.2.1" + } + }, + "content-type": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", + "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" + }, + "cookie": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.2.tgz", + "integrity": "sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==" + }, + "cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "denque": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz", + "integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==" + }, + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + }, + "destroy": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", + "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" + }, + "ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" + }, + "encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" + }, + "escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" + }, + "etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" + }, + "express": { + "version": "4.17.3", + "resolved": "https://registry.npmjs.org/express/-/express-4.17.3.tgz", + "integrity": "sha512-yuSQpz5I+Ch7gFrPCk4/c+dIBKlQUxtgwqzph132bsT6qhuzss6I8cLJQz7B3rFblzd6wtcI0ZbGltH/C4LjUg==", + "requires": { + "accepts": "~1.3.8", + "array-flatten": "1.1.1", + "body-parser": "1.19.2", + "content-disposition": "0.5.4", + "content-type": "~1.0.4", + "cookie": "0.4.2", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~1.1.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "~1.1.2", + "fresh": "0.5.2", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.7", + "qs": "6.9.7", + "range-parser": "~1.2.1", + "safe-buffer": "5.2.1", + "send": "0.17.2", + "serve-static": "1.14.2", + "setprototypeof": "1.2.0", + "statuses": "~1.5.0", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + } + }, + "finalhandler": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", + "integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==", + "requires": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "~2.3.0", + "parseurl": "~1.3.3", + "statuses": "~1.5.0", + "unpipe": "~1.0.0" + } + }, + "forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==" + }, + "fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" + }, + "http-errors": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.1.tgz", + "integrity": "sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.1" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" + }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=" + }, + "methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=" + }, + "mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==" + }, + "mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" + }, + "mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "requires": { + "mime-db": "1.52.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "negotiator": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", + "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==" + }, + "on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", + "requires": { + "ee-first": "1.1.1" + } + }, + "parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" + }, + "path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" + }, + "proxy-addr": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", + "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "requires": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + } + }, + "qs": { + "version": "6.9.7", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.9.7.tgz", + "integrity": "sha512-IhMFgUmuNpyRfxA90umL7ByLlgRXu6tIfKPpF5TmcfRLlLCckfP/g3IQmju6jjpu+Hh8rA+2p6A27ZSPOOHdKw==" + }, + "range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" + }, + "raw-body": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.3.tgz", + "integrity": "sha512-UlTNLIcu0uzb4D2f4WltY6cVjLi+/jEN4lgEUj3E04tpMDpUlkBo/eSn6zou9hum2VMNpCCUone0O0WeJim07g==", + "requires": { + "bytes": "3.1.2", + "http-errors": "1.8.1", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } + }, + "redis": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz", + "integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==", + "requires": { + "denque": "^1.5.0", + "redis-commands": "^1.7.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0" + } + }, + "redis-commands": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz", + "integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ==" + }, + "redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=" + }, + "redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", + "requires": { + "redis-errors": "^1.0.0" + } + }, + "safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "send": { + "version": "0.17.2", + "resolved": "https://registry.npmjs.org/send/-/send-0.17.2.tgz", + "integrity": "sha512-UJYB6wFSJE3G00nEivR5rgWp8c2xXvJ3OPWPhmuteU0IKj8nKbG3DrjiOmLwpnHGYWAVwA69zmTm++YG0Hmwww==", + "requires": { + "debug": "2.6.9", + "depd": "~1.1.2", + "destroy": "~1.0.4", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "1.8.1", + "mime": "1.6.0", + "ms": "2.1.3", + "on-finished": "~2.3.0", + "range-parser": "~1.2.1", + "statuses": "~1.5.0" + }, + "dependencies": { + "ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + } + } + }, + "serve-static": { + "version": "1.14.2", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.2.tgz", + "integrity": "sha512-+TMNA9AFxUEGuC0z2mevogSnn9MXKb4fa7ngeRMJaaGv8vTwnIEkKi+QGvPt33HSnf8pRS+WGM0EbMtCJLKMBQ==", + "requires": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "0.17.2" + } + }, + "setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" + }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" + }, + "toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==" + }, + "type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "requires": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + } + }, + "unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" + }, + "utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" + }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" + } + } +} diff --git a/examples/nginx-nodejs-redis/web/package.json b/examples/nginx-nodejs-redis/web/package.json new file mode 100644 index 00000000..c5565bfc --- /dev/null +++ b/examples/nginx-nodejs-redis/web/package.json @@ -0,0 +1,15 @@ +{ + "name": "web", + "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" +} diff --git a/examples/nginx-nodejs-redis/web/server.js b/examples/nginx-nodejs-redis/web/server.js new file mode 100644 index 00000000..2f1a23dc --- /dev/null +++ b/examples/nginx-nodejs-redis/web/server.js @@ -0,0 +1,29 @@ +const os = require('os') +const express = require('express') +const app = express() +const redis = require('redis') +const redisClient = redis.createClient({ + host: '172.23.0.2', + port: 6379 +}) + +app.get('/', function (req, res) { + redisClient.get('numVisits', function (err, numVisits) { + if (err) { + console.log(err) + res.send('An error occurred') + return + } + var numVisitsToDisplay = parseInt(numVisits) + 1 + if (isNaN(numVisitsToDisplay)) { + numVisitsToDisplay = 1 + } + res.send(os.hostname() + ': Number of visits is: ' + numVisitsToDisplay) + numVisits++ + redisClient.set('numVisits', numVisits) + }) +}) + +app.listen(5000, function () { + console.log('Web application is listening on port 5000') +})