Windows安装Docker部署Node项目(声网录制部署)

前言

为啥要在Windows上装Docker呢?在Windows上又不能发挥Docker的优势!!

原因是这样的在对接第三方的服务时必须在Linux编译运行,但是平常开发都是在Mac或Windows上,所有只能在Docker中运行,为了项目代码之后方便部署就使用了Dockerfile来生成镜像。

Windows上

安装

Docker下载

https://www.docker.com/products/docker-desktop

WSL下载

https://docs.microsoft.com/zh-cn/windows/wsl/install-win10

以管理员身份打开 PowerShell

查看虚拟化为已启用

image-20231120173219653

启用“虚拟机平台”

1
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

适用于 Linux 的 Windows 子系统”

1
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

下载最新包:

适用于 x64 计算机的 WSL2 Linux 内核更新包

WSL 2 设置为默认版本:

1
wsl --set-default-version 2

如果启动失败

执行如下命令更新 wsl:

1
wsl --update

更改存储位置

默认存储位置查看

1
docker info | findstr "Docker Root"

默认结果

Docker Root Dir: /var/lib/docker

修改存储位置

我们可以设置位置,但是所设置的位置是子系统下的路径,直接设置Windows下的路径是无效的。

设置下的Docker Engine配置data-root的值

1
2
3
{
"data-root": "/var/lib/docker"
}

所以

不建议修改"data-root"配置,因为这实际更改的是在虚拟子系统中的存储路径。

我们要更改的是虚拟子系统的位置

默认的路径是类似这样的

C:\Users\Administrator\AppData\Local\Docker

先停止Docker

再停止WSL

1
wsl --shutdown

复制Docker下的文件到新路径D:\DockerRoot

创建软连接

在CMD中运行(不是PowerShell)

1
mklink /j "C:\Users\Administrator\AppData\Local\Docker" "D:\DockerRoot"

设置镜像

启动成功后

设置下的Docker Engine配置registry-mirrors的值

1
2
3
{
"registry-mirrors": ["https://tiaudqrq.mirror.aliyuncs.com"]
}

测试Docker

1
docker run hello-world

声网

录制修复

https://docs.agora.io/cn/Recording/recording_restore_files?platform=Linux

Dockerfile

添加Dockfile

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
51
52
53
54
55
56
57
58
59
60
# Pull base image
FROM centos:7

MAINTAINER psvmc "psvmc@outlook.com"

# Set Charset
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en

# 安装 wget
RUN yum -y install wget

# 安装 nodejs
RUN mkdir /data
RUN cd /data
RUN wget https://nodejs.org/dist/v10.24.0/node-v10.24.0-linux-x64.tar.xz
RUN tar -xvf node-v10.24.0-linux-x64.tar.xz
RUN mv node-v10.24.0-linux-x64 /usr/local/nodejs10
RUN rm -rf node-v10.24.0-linux-x64.tar.xz
ENV NODE_HOME /usr/local/nodejs10
ENV PATH $PATH:$NODE_HOME/bin
RUN npm config set registry https://registry.npm.taobao.org

# YUM镜像
RUN wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum makecache

# 安装Python
RUN yum install -y epel-release
RUN yum install -y python2

# 安装 gcc
RUN yum -y install gcc+ gcc-c++ automake autoconf libtool make

# 安装 node-gyp
RUN npm install -g node-gyp

# 项目配置
RUN mkdir school_live_record
ADD startup.sh /data/school_live_record/startup.sh
RUN chmod +x /data/school_live_record/startup.sh
ADD record /data/school_live_record/record
ADD server /data/school_live_record/server
RUN wget https://download.agora.io/ardsdk/release/Agora_Recording_SDK_for_Linux_v3.0.5_20210106-1609927649_793.tar.gz
RUN tar zxvf Agora_Recording_SDK_for_Linux_v3.0.5_20210106-1609927649_793.tar.gz
RUN mkdir /data/school_live_record/record/src/sdk/
RUN mv ./Agora_Recording_SDK_for_Linux_FULL/* /data/school_live_record/record/src/sdk/
RUN rm -rf zxvf Agora_Recording_SDK_for_Linux_v3.0.5_20210106-1609927649_793.tar.gz
RUN cd /data/school_live_record/record && chmod +x ./build_debug.sh && ./build_debug.sh
RUN cd /data/school_live_record/server && npm install

# Expose ports.
EXPOSE 7000

# Define default command.
WORKDIR /data/school_live_record/
ENTRYPOINT ./startup.sh

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

startup.sh

1
node ./server/app.js

构建镜像

1
docker build -t psvmc/school_live_record .

禁用缓存构建

1
docker build -t psvmc/school_live_record --no-cache .

运行

1
docker run -dit -p 7000:7000 -v /c/school_live_record/:/data/school_live_record/server/output/ --name school_live_record --restart=always psvmc/school_live_record /bin/bash

带配置文件映射

school_live_record添加配置文件目录config/config.js

1
2
mkdir -p /data/web_front/school_live_record/config/
cp ./server/config/config.js /data/web_front/school_live_record/config/

添加映射

1
2
3
4
5
6
docker run -dit -p 7000:7000 \
-v /data/web_front/school_live_record/:/data/school_live_record/server/output/ \
-v /data/web_front/school_live_record/config/:/data/school_live_record/server/config/ \
--name school_live_record \
--restart=always \
psvmc/school_live_record /bin/bash

这时候既可以访问项目了

http://127.0.0.1:7000

访问测试

1
curl -i -X POST -H "Content-type: application/json" --data '{"channel":"123","tid":"80"}' 127.0.0.1:7000/recorder/v1/start

查看日志

1
docker logs school_live_record

停止删除容器

1
2
docker stop school_live_record
docker rm school_live_record

查看日志

1
docker logs school_live_record

进入容器

1
docker exec -t -i school_live_record /bin/bash

重启

1
docker restart school_live_record

测试

停止和删除

1
2
docker stop school_live_record
docker rm school_live_record

构建镜像

1
docker build -t psvmc/school_live_record .

运行

1
docker run -dit -p 7000:7000 -v /d/school_live_record/:/data/school_live_record/server/output/ --name school_live_record --restart=always psvmc/school_live_record /bin/bash

其中/d/school_live_record/就是路径D:\school_live_record

查看日志

1
docker logs school_live_record

保存日志

1
docker logs school_live_record>/d/1.txt

进入容器

1
docker exec -t -i school_live_record /bin/bash

访问测试

1
2
3
4
5
6
7
8
9
10
# 开始录制
curl -i -X POST -H "Content-type: application/json" --data "{\"channel\":\"psvmc123\",\"tid\":\"80\"}" 127.0.0.1:7000/recorder/v1/start
# 录制状态
curl -i -X POST -H "Content-type: application/json" --data "{\"sid\":\"sid_node01_psvmc123_80_9c4923ea94f1420393f61df53262e92f\"}" 127.0.0.1:7000/recorder/v1/has_recorder
# 停止录制
curl -i -X POST -H "Content-type: application/json" --data "{\"sid\":\"sid_node01_psvmc123_80_9c4923ea94f1420393f61df53262e92f\"}" 127.0.0.1:7000/recorder/v1/stop
# 接口状态
curl -i -X POST -H "Content-type: application/json" --data "{}" 127.0.0.1:7000/recorder/v1/status
# 接口健康
curl -i -X GET 127.0.0.1:7000/health

删除

1
curl -i -X POST -H "Content-type: application/json" --data "{\"fileList\":[\"node01/liverecord/20230227/AAAAAA_18679/sid_node01_AAAAAA_18679_2117_55f6ab01f6bd407ab3b60b91391e549d/0_20230227093324727.mp4\"],\"sign\":\"E137D57466EB18931598EB0589CB6755\"}" http://127.0.0.1:7000/recorder/v1/delfiles

CentOS项目实战

安装Docker

https://www.psvmc.cn/article/2018-12-13-docker-install.html

项目下载及运行

上文说的是怎么制作dockerfile文件,一般我们会把文件放在项目里,在服务器上可以把项目clone后,进行部署。

安装git

1
yum install -y git

进入目录

1
2
mkdir -p /data/tools/project_git/
cd /data/tools/project_git/

克隆项目

1
git clone http://git.xhkjedu.com/xhkjedu/school_live_record.git

进入项目

1
cd school_live_record/

构建镜像

1
docker build -t psvmc/school_live_record_node .

禁用缓存构建

1
docker build -t psvmc/school_live_record_node --no-cache .

school_live_record添加配置文件目录config/config.js

1
2
mkdir -p /data/web_front/school_live_record_test/config/
cp ./server/config/config.js /data/web_front/school_live_record_test/config/

运行

1
2
3
4
5
6
docker run -dit -p 7000:7000 \
-v /data/web_front/school_live_record_test_node01/:/data/school_live_record/server/output/ \
-v /data/web_front/school_live_record_test_node01/config/:/data/school_live_record/server/config/ \
--name school_live_record_node01 \
--restart=always \
psvmc/school_live_record_node /bin/bash

后续更新

进入目录更新代码

1
2
cd /data/tools/project_git/school_live_record
git pull

重新构建镜像

1
docker build -t psvmc/school_live_record_node .

停止之前的容器

1
2
docker stop school_live_record_node01
docker rm school_live_record_node01

运行

1
2
3
4
5
6
docker run -dit -p 7000:7000 \
-v /data/web_front/school_live_record_test_node01/:/data/school_live_record/server/output/ \
-v /data/web_front/school_live_record_test_node01/config/:/data/school_live_record/server/config/ \
--name school_live_record_node01 \
--restart=always \
psvmc/school_live_record_node /bin/bash

停止之前的容器

1
2
docker stop school_live_record_node02
docker rm school_live_record_node02

负载2

1
2
3
4
5
6
docker run -dit -p 7011:7000 \
-v /data/web_front/school_live_record_test_node02/:/data/school_live_record/server/output/ \
-v /data/web_front/school_live_record_test_node02/config/:/data/school_live_record/server/config/ \
--name school_live_record_node02 \
--restart=always \
psvmc/school_live_record_node /bin/bash

查看运行状态和日志

1
2
3
docker ps -a
docker logs school_live_record_node01
docker logs school_live_record_node02

查看镜像删除之前的构建

1
2
docker images
docker rmi 镜像ID

配置Nginx

安装

添加Nginx源

1
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

查询

1
yum list nginx

安装

1
yum install -y nginx

卸载

1
yum -y remove nginx*

启动

1
service nginx start

停止

1
service nginx stop

设为开机启动

1
chkconfig nginx on

1
systemctl enable nginx.service

重新加载配置

1
service nginx reload

查看版本

1
nginx -v

配置文件路径/etc/nginx/conf.d

添加配置文件

服务配置文件

7000_ycrec.xhkjedu.com.conf

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
server {  
listen 80;
server_name ycrec.xhkjedu.com;
listen 443 ssl;
ssl_certificate /etc/nginx/cert/xhkjedu.pem;
ssl_certificate_key /etc/nginx/cert/xhkjedu.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;


location / {
proxy_pass http://127.0.0.1:7000/;
proxy_cookie_path / /;
proxy_redirect / /;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 1000m;
client_body_buffer_size 128k;
client_body_timeout 5m;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_buffer_size 64k;
proxy_buffers 8 64k;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
send_timeout 20s;
}
}

文件服务配置文件

_ycrecstatic.xhkjedu.com.conf

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
server {  
listen 80;
server_name ycrecstatic.xhkjedu.com;
listen 443 ssl;
ssl_certificate /etc/nginx/cert/xhkjedu.pem;
ssl_certificate_key /etc/nginx/cert/xhkjedu.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

location / {
root /data/web_front/school_live_record_test/;
index index.html;
client_max_body_size 200m;
client_body_buffer_size 128k;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_buffer_size 64k;
proxy_buffers 8 64k;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
send_timeout 60;
}
}

迁移源服务器文件

登录源服务器

1
scp -r /data/web_front/school_live_record_test/liverecord/* root@172.24.197.26:/data/web_front/school_live_record_test/liverecord/