docker[3]-真实web应用

index.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const express = require('express');
const redis = require('redis');

const app = express();
const client = redis.createClient({
host:'redis-server', // 配合docker-compose使用。//自动连接docker中的后台主机名为redis-server的服务器,默认端口为6379
port:6379
});
client.set('visits', 0);

app.get('/', (req, res) => {
client.get('visits', (err, visits) => {
res.send('Number of visits is ' + visits);
client.set('visits', parseInt(visits) + 1);
});
});

app.listen(8081, () => {
console.log('Listening on port 8081');
});

package.json

1
2
3
4
5
6
7
8
9
{
"dependencies": {
"express": "*",
"redis": "2.8.0"
},
"scripts": {
"start": "node index.js"
}
}

Dockerfile

1
2
3
4
5
6
7
8
9
FROM node:alpine

WORKDIR '/app'

COPY package.json .
RUN npm install
COPY . .

CMD ["npm","start"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ docker build -t zhuimengshaonian04/webtest:latest .
Sending build context to Docker daemon 4.096kB
Step 1/6 : FROM node:alpine
---> 4e50ad7c0e0b
Step 2/6 : WORKDIR '/app'
---> Using cache
---> e99f36e2e99d
Step 3/6 : COPY package.json .
---> 2b85c0cb1430
Step 4/6 : RUN npm install
---> Running in a4e0904bb389
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN app No description
npm WARN app No repository field.
npm WARN app No license field.

added 52 packages from 40 contributors and audited 125 packages in 11.86s
found 0 vulnerabilities

Removing intermediate container a4e0904bb389
---> c479e4e9544d
Step 5/6 : COPY . .
---> e270a6054ebd
Step 6/6 : CMD ["npm","start"]
---> Running in 46558e1f9bbf
Removing intermediate container 46558e1f9bbf
---> 862676d2443e
Successfully built 862676d2443e
Successfully tagged zhuimengshaonian04/webtest:latest

docker-compose.yml

独立的客户端。docker-compose可以避免繁琐的docker命令,并且让多个container相互通信

1
2
3
4
5
6
7
8
version : '3'
services:
redis-server: #服务器名字
image: 'redis'
node-app: #服务器名字
build: . # 使用Dockerfile中的
ports:
- "4001:8081" #真实端口4001转移到docker端口8081

开启docker-compose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$ docker-compose up



Creating network "visits2_default" with the default driver
Building node-app
Step 1/6 : FROM node:alpine
---> 4e50ad7c0e0b
Step 2/6 : WORKDIR '/app'
---> Using cache
---> e99f36e2e99d
Step 3/6 : COPY package.json .
---> f58dd6549a9c
Step 4/6 : RUN npm install
---> Running in d24b1cc9a3c1
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN app No description
npm WARN app No repository field.
npm WARN app No license field.

added 52 packages from 40 contributors and audited 125 packages in 7.725s
found 0 vulnerabilities

Removing intermediate container d24b1cc9a3c1
---> 43ff230ce2d5
Step 5/6 : COPY . .
---> bd09786967c4
Step 6/6 : CMD ["npm","start"]
---> Running in d244a10ca082
Removing intermediate container d244a10ca082
---> 7f254839bf80
Successfully built 7f254839bf80
Successfully tagged visits2_node-app:latest
WARNING: Image for service node-app was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating visits2_node-app_1 ... done
Creating visits2_redis-server_1 ... done
Attaching to visits2_redis-server_1, visits2_node-app_1
redis-server_1 | 1:C 26 Feb 01:43:22.949 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-server_1 | 1:C 26 Feb 01:43:22.955 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=1, just started
redis-server_1 | 1:C 26 Feb 01:43:22.955 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-server_1 | 1:M 26 Feb 01:43:22.957 * Running mode=standalone, port=6379.
redis-server_1 | 1:M 26 Feb 01:43:22.957 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis-server_1 | 1:M 26 Feb 01:43:22.957 # Server initialized
redis-server_1 | 1:M 26 Feb 01:43:22.957 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis-server_1 | 1:M 26 Feb 01:43:22.957 * Ready to accept connections
node-app_1 |
node-app_1 | > @ start /app
node-app_1 | > node index.js
node-app_1 |
node-app_1 | Listening on port 8081

浏览器中访问

浏览器中访问网址:localhost:4001

利用docker-compose同时管理-释放多个container

开启之前的多个container
docker-compose up -d

关闭:
docker-compose down

当修改了文件后,更新重构:
docker-compose up --build

查看状态:注意必须要在拥有docker-compose.yml的目录下。
docker-compose ps

docker-compose 重启规则

“no” 永不重启
always 总是会重启
on-failure 出错代码才会重启
unless-stopped 一直会重启,除非开发者强制关闭,例如执行docker stop