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 <milas.bowman@docker.com>
This commit is contained in:
Milas Bowman 2022-07-11 16:38:24 -04:00
parent 9d547d23fb
commit a6048a745a
7 changed files with 182 additions and 66 deletions

View File

@ -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

View File

@ -7,43 +7,62 @@ Project structure:
. .
├── README.md ├── README.md
├── compose.yaml ├── compose.yaml
├── nginx ├── proxy
│   ├── Dockerfile
│   └── nginx.conf │   └── nginx.conf
└── web └── web
├── Dockerfile ├── Dockerfile
├── package.json ├── package.json
├── package-lock.json
└── server.js └── server.js
2 directories, 7 files 2 directories, 7 files
``` ```
[_compose.yaml_](compose.yaml)
``` [`compose.yaml`](compose.yaml)
redis: ```yaml
services:
redis:
image: 'redislabs/redismod' image: 'redislabs/redismod'
ports: ports:
- '6379:6379' - '6379:6379'
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 5s
retries: 5
web1: web1:
build:
context: web
target: builder
restart: on-failure restart: on-failure
build: ./web
hostname: web1 hostname: web1
ports:
- '81:5000'
web2:
restart: on-failure
build: ./web
hostname: web2
ports:
- '82:5000'
nginx:
build: ./nginx
ports:
- '80:80'
depends_on: depends_on:
- web1 redis:
- web2 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`. 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. 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 $ docker compose up -d
[+] Running 24/24 [+] Running 31/31
⠿ redis Pulled ... ⠿ proxy Pulled 10.1s
⠿ 565225d89260 Pull complete ⠿ redis Pulled 23.0s
[+] Building 2.4s (22/25) [+] Building 1.1s (19/22)
=> [nginx-nodejs-redis_nginx internal] load build definition from Dockerfile ... => [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 [+] Running 5/5
⠿ Network nginx-nodejs-redis_default Created ⠿ Network nginx-nodejs-redis_default Created 0.0s
⠿ Container nginx-nodejs-redis-web2-1 Started ⠿ Container nginx-nodejs-redis-redis-1 Healthy 10.8s
⠿ Container nginx-nodejs-redis-redis-1 Started ⠿ Container nginx-nodejs-redis-web2-1 Started 11.2s
⠿ Container nginx-nodejs-redis-web1-1 Started ⠿ Container nginx-nodejs-redis-web1-1 Started 11.2s
⠿ Container nginx-nodejs-redis-nginx-1 Started ⠿ Container nginx-nodejs-redis-proxy-1 Started 11.3s
``` ```
## Expected result ## 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:
```shell
``` $ docker compose ps
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 ## Testing the app
After the application starts, navigate to `http://localhost:80` in your web browser or run: After the application starts, navigate to `http://localhost:80` in your web browser or run:
``` ```shell
curl localhost:80 $ curl localhost:80
curl localhost:80
web1: Total number of visits is: 1 web1: Total number of visits is: 1
```
``` $ curl localhost:80
curl localhost:80
web1: Total number of visits is: 2 web1: Total number of visits is: 2
```
```
$ curl localhost:80 $ curl localhost:80
web2: Total number of visits is: 3 web2: Total number of visits is: 3
``` ```
## Stop and remove the containers ## Stop and remove the containers
``` ```shell
$ docker compose down $ 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
```

View File

@ -1,27 +1,43 @@
services: services:
redis: redis:
image: 'redislabs/redismod' image: 'redislabs/redismod'
ports: ports:
- '6379:6379' - '6379:6379'
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 5s
retries: 5
web1: web1:
build:
context: web
target: builder
restart: on-failure restart: on-failure
build: ./web
hostname: web1 hostname: web1
ports: depends_on:
- '81:5000' redis:
condition: service_healthy
web2: web2:
build:
context: web
target: builder
restart: on-failure restart: on-failure
build: ./web
hostname: web2 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: ports:
- '82:5000' - '80:80'
nginx:
build: ./nginx
ports:
- '80:80'
depends_on: depends_on:
- web1 - web1
- web2 - web2

View File

@ -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

View File

@ -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 WORKDIR /usr/src/app
COPY package.json package-lock.json ./ COPY package.json package-lock.json ./
RUN npm ci RUN --mount=type=cache,target=/npm-cache \
npm ci
COPY ./server.js ./ COPY ./server.js ./
CMD ["npm","start"] CMD ["npm", "start"]
FROM builder as dev-envs
RUN <<EOF
apk update
apk add git
EOF
RUN <<EOF
addgroup -S docker
adduser -S --shell /bin/bash --ingroup docker vscode
EOF
# install Docker tools (cli, buildx, compose)
COPY --from=gloursdocker/docker / /
CMD ["npm", "start"]

View File

@ -13,7 +13,7 @@ app.get('/', function(req, res) {
if (isNaN(numVisitsToDisplay)) { if (isNaN(numVisitsToDisplay)) {
numVisitsToDisplay = 1; numVisitsToDisplay = 1;
} }
res.send(os.hostname() +': Number of visits is: ' + numVisitsToDisplay); res.send(os.hostname() +': Number of visits is: ' + numVisitsToDisplay + '!!!\n');
numVisits++; numVisits++;
redisClient.set('numVisits', numVisits); redisClient.set('numVisits', numVisits);
}); });