2022-03-17 16:33:36 +00:00
|
|
|
|
## Compose sample application
|
|
|
|
|
|
|
|
|
|
## Node.js application with Nginx proxy and Redis database
|
|
|
|
|
|
|
|
|
|
Project structure:
|
|
|
|
|
```
|
|
|
|
|
.
|
|
|
|
|
├── README.md
|
2022-05-10 09:59:25 +00:00
|
|
|
|
├── compose.yaml
|
2022-03-17 16:33:36 +00:00
|
|
|
|
├── nginx
|
|
|
|
|
│ ├── Dockerfile
|
|
|
|
|
│ └── nginx.conf
|
|
|
|
|
└── web
|
|
|
|
|
├── Dockerfile
|
|
|
|
|
├── package.json
|
|
|
|
|
└── server.js
|
|
|
|
|
|
|
|
|
|
2 directories, 7 files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
2022-05-10 09:59:25 +00:00
|
|
|
|
[_compose.yaml_](compose.yaml)
|
2022-03-17 16:33:36 +00:00
|
|
|
|
```
|
|
|
|
|
redis:
|
|
|
|
|
image: 'redislabs/redismod'
|
|
|
|
|
ports:
|
|
|
|
|
- '6379:6379'
|
|
|
|
|
web1:
|
|
|
|
|
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
|
|
|
|
|
```
|
|
|
|
|
The compose file defines an application with four services `redis`, `nginx`, `web1` and `web2`.
|
2022-05-10 09:59:25 +00:00
|
|
|
|
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.
|
2022-03-17 16:33:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
> ℹ️ **_INFO_**
|
|
|
|
|
> Redis runs on port 6379 by default. Make sure port 6379 on the host is not being used by another container, otherwise the port should be changed.
|
|
|
|
|
|
2022-05-10 09:59:25 +00:00
|
|
|
|
## Deploy with docker compose
|
2022-03-17 16:33:36 +00:00
|
|
|
|
|
|
|
|
|
```
|
2022-05-10 09:59:25 +00:00
|
|
|
|
$ docker compose up -d
|
2022-03-17 16:33:36 +00:00
|
|
|
|
[+] Running 24/24
|
|
|
|
|
⠿ redis Pulled ...
|
|
|
|
|
⠿ 565225d89260 Pull complete
|
|
|
|
|
[+] Building 2.4s (22/25)
|
|
|
|
|
=> [nginx-nodejs-redis_nginx internal] load build definition from Dockerfile ...
|
|
|
|
|
[+] 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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Expected result
|
|
|
|
|
|
|
|
|
|
Listing containers must show three containers running and the port mapping as below:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
docker-compose ps
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Testing the app
|
|
|
|
|
|
|
|
|
|
After the application starts, navigate to `http://localhost:80` in your web browser or run:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
curl localhost:80
|
|
|
|
|
curl localhost:80
|
|
|
|
|
web1: Total number of visits is: 1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
```
|
2022-05-10 09:59:25 +00:00
|
|
|
|
$ docker compose down
|
2022-03-17 16:33:36 +00:00
|
|
|
|
```
|
|
|
|
|
|