From a6048a745a1ee0c63b5dc6a2705979aa4a944690 Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Mon, 11 Jul 2022 16:38:24 -0400 Subject: [PATCH] nginx-nodejs-redis: add dev envs config * Add Docker Desktop Development Environments config * Upgrade NodeJS image * Rename `nginx` to `proxy` and use bind mount for config for consistency with other examples Signed-off-by: Milas Bowman --- .../.docker/docker-compose.yaml | 47 +++++++ nginx-nodejs-redis/README.md | 128 +++++++++++------- nginx-nodejs-redis/compose.yaml | 40 ++++-- nginx-nodejs-redis/nginx/Dockerfile | 3 - .../{nginx => proxy}/nginx.conf | 0 nginx-nodejs-redis/web/Dockerfile | 28 +++- nginx-nodejs-redis/web/server.js | 2 +- 7 files changed, 182 insertions(+), 66 deletions(-) create mode 100644 nginx-nodejs-redis/.docker/docker-compose.yaml delete mode 100644 nginx-nodejs-redis/nginx/Dockerfile rename nginx-nodejs-redis/{nginx => proxy}/nginx.conf (100%) diff --git a/nginx-nodejs-redis/.docker/docker-compose.yaml b/nginx-nodejs-redis/.docker/docker-compose.yaml new file mode 100644 index 0000000..b677c7d --- /dev/null +++ b/nginx-nodejs-redis/.docker/docker-compose.yaml @@ -0,0 +1,47 @@ +services: + redis: + image: 'redislabs/redismod' + ports: + - '6379:6379' + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 5s + retries: 5 + + web1: + build: + context: web + target: dev-envs + restart: on-failure + hostname: web1 + volumes: + - /var/run/docker.sock:/var/run/docker.sock + depends_on: + redis: + condition: service_healthy + + web2: + build: + context: web + target: dev-envs + restart: on-failure + hostname: web2 + volumes: + - /var/run/docker.sock:/var/run/docker.sock + depends_on: + redis: + condition: service_healthy + + proxy: + image: nginx + volumes: + - type: bind + source: ./proxy/nginx.conf + target: /etc/nginx/conf.d/default.conf + read_only: true + ports: + - '80:80' + depends_on: + - web1 + - web2 diff --git a/nginx-nodejs-redis/README.md b/nginx-nodejs-redis/README.md index 41a0d2a..2dd1e05 100644 --- a/nginx-nodejs-redis/README.md +++ b/nginx-nodejs-redis/README.md @@ -7,43 +7,62 @@ Project structure: . ├── README.md ├── compose.yaml -├── nginx -│   ├── Dockerfile +├── proxy │   └── nginx.conf └── web ├── Dockerfile ├── package.json + ├── package-lock.json └── server.js 2 directories, 7 files - - ``` -[_compose.yaml_](compose.yaml) -``` -redis: + +[`compose.yaml`](compose.yaml) +```yaml +services: + redis: image: 'redislabs/redismod' ports: - '6379:6379' + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 5s + retries: 5 + web1: + build: + context: web + target: builder restart: on-failure - build: ./web hostname: web1 - ports: - - '81:5000' - web2: - restart: on-failure - build: ./web - hostname: web2 - ports: - - '82:5000' - nginx: - build: ./nginx - ports: - - '80:80' depends_on: - - web1 - - web2 + redis: + condition: service_healthy + + web2: + build: + context: web + target: builder + restart: on-failure + hostname: web2 + depends_on: + redis: + condition: service_healthy + + proxy: + image: nginx + volumes: + - type: bind + source: ./proxy/nginx.conf + target: /etc/nginx/conf.d/default.conf + read_only: true + ports: + - '80:80' + depends_on: + - web1 + - web2 ``` 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 container to port 80 of the host as specified in the file. @@ -56,53 +75,68 @@ When deploying the application, docker compose maps port 80 of the nginx service ``` $ docker compose up -d -[+] Running 24/24 - ⠿ redis Pulled ... - ⠿ 565225d89260 Pull complete -[+] Building 2.4s (22/25) - => [nginx-nodejs-redis_nginx internal] load build definition from Dockerfile ... +[+] Running 31/31 + ⠿ proxy Pulled 10.1s + ⠿ redis Pulled 23.0s +[+] Building 1.1s (19/22) + => [nginx-nodejs-redis_web1 internal] load build definition from Dockerfile 0.0s + => [nginx-nodejs-redis_web2 internal] load build definition from Dockerfile 0.0s + ... + => [nginx-nodejs-redis_web1] exporting to image 0.0s + => => exporting layers 0.0s + => => writing image sha256:bb4ba7fc27bd0f7a8d572bc4ea9d0734b0f88f50a773b39028ffacd83c309c5c 0.0s + => => naming to docker.io/library/nginx-nodejs-redis_web2 0.0s + => => naming to docker.io/library/nginx-nodejs-redis_web1 0.0s [+] Running 5/5 - ⠿ Network nginx-nodejs-redis_default Created - ⠿ Container nginx-nodejs-redis-web2-1 Started - ⠿ Container nginx-nodejs-redis-redis-1 Started - ⠿ Container nginx-nodejs-redis-web1-1 Started - ⠿ Container nginx-nodejs-redis-nginx-1 Started + ⠿ Network nginx-nodejs-redis_default Created 0.0s + ⠿ Container nginx-nodejs-redis-redis-1 Healthy 10.8s + ⠿ Container nginx-nodejs-redis-web2-1 Started 11.2s + ⠿ Container nginx-nodejs-redis-web1-1 Started 11.2s + ⠿ Container nginx-nodejs-redis-proxy-1 Started 11.3s ``` ## Expected result -Listing containers must show three containers running and the port mapping as below: +Listing containers should show three containers running and the port mapping as below: - -``` -docker-compose ps +```shell +$ docker compose ps +NAME COMMAND SERVICE STATUS PORTS +nginx-nodejs-redis-proxy-1 "/docker-entrypoint.…" proxy running 0.0.0.0:80->80/tcp +nginx-nodejs-redis-redis-1 "redis-server --load…" redis running (healthy) 0.0.0.0:6379->6379/tcp +nginx-nodejs-redis-web1-1 "docker-entrypoint.s…" web1 running +nginx-nodejs-redis-web2-1 "docker-entrypoint.s…" web2 running ``` ## Testing the app After the application starts, navigate to `http://localhost:80` in your web browser or run: -``` -curl localhost:80 -curl localhost:80 +```shell +$ curl localhost:80 web1: Total number of visits is: 1 -``` -``` -curl localhost:80 +$ curl localhost:80 web1: Total number of visits is: 2 -``` -``` + $ curl localhost:80 web2: Total number of visits is: 3 ``` - - ## Stop and remove the containers -``` +```shell $ docker compose down ``` +## Use with Docker Development Environments + +You can use this sample with the Dev Environments feature of Docker Desktop. + +![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png) + +To develop directly on the services inside containers, use the HTTPS Git url of the sample: +``` +https://github.com/docker/awesome-compose/tree/master/nginx-nodejs-redis +``` diff --git a/nginx-nodejs-redis/compose.yaml b/nginx-nodejs-redis/compose.yaml index 7f3748c..0f90918 100644 --- a/nginx-nodejs-redis/compose.yaml +++ b/nginx-nodejs-redis/compose.yaml @@ -1,27 +1,43 @@ - services: redis: image: 'redislabs/redismod' ports: - '6379:6379' + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 5s + retries: 5 + web1: + build: + context: web + target: builder restart: on-failure - build: ./web hostname: web1 - ports: - - '81:5000' + depends_on: + redis: + condition: service_healthy + web2: + build: + context: web + target: builder restart: on-failure - build: ./web hostname: web2 + depends_on: + redis: + condition: service_healthy + + proxy: + image: nginx + volumes: + - type: bind + source: ./proxy/nginx.conf + target: /etc/nginx/conf.d/default.conf + read_only: true ports: - - '82:5000' - nginx: - build: ./nginx - ports: - - '80:80' + - '80:80' depends_on: - web1 - web2 - - diff --git a/nginx-nodejs-redis/nginx/Dockerfile b/nginx-nodejs-redis/nginx/Dockerfile deleted file mode 100644 index 67cab10..0000000 --- a/nginx-nodejs-redis/nginx/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM nginx:1.21.6 -RUN rm /etc/nginx/conf.d/default.conf -COPY nginx.conf /etc/nginx/conf.d/default.conf diff --git a/nginx-nodejs-redis/nginx/nginx.conf b/nginx-nodejs-redis/proxy/nginx.conf similarity index 100% rename from nginx-nodejs-redis/nginx/nginx.conf rename to nginx-nodejs-redis/proxy/nginx.conf diff --git a/nginx-nodejs-redis/web/Dockerfile b/nginx-nodejs-redis/web/Dockerfile index 3d1b21f..ce54554 100644 --- a/nginx-nodejs-redis/web/Dockerfile +++ b/nginx-nodejs-redis/web/Dockerfile @@ -1,9 +1,31 @@ -FROM node:14.17.3-alpine3.14 +# syntax=docker/dockerfile:1.4 +FROM --platform=$BUILDPLATFORM node:18-alpine AS builder + +ENV NPM_CONFIG_CACHE /npm-cache WORKDIR /usr/src/app COPY package.json package-lock.json ./ -RUN npm ci +RUN --mount=type=cache,target=/npm-cache \ + npm ci + COPY ./server.js ./ -CMD ["npm","start"] +CMD ["npm", "start"] + +FROM builder as dev-envs + +RUN <