说明: 此方式只适合在测试环境部署,能够轻量级的快速启动多个项目,方便调试,不适合部署在正式环境,正式环境一般使用dockerfile制作镜像,并且代码是用编译打包后的,不会使用git拉取源码。
一、docker拉取node镜像并启动进入
docker pull node:16.15.1
docker run -it -d --name evokey --privileged=true node:16.15.1 /bin/bash
docker exec -it evokey /bin/bash
二、安装git、vim等工具
#更新镜像源
cd /etc/apt/
echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free" > sources.list
apt update
#安装git、vim
apt install git
apt install vim
三、拉取代码运行项目
#此处省略git拉取代码...
#安装pm2
npm install pm2 -g
#启动项目
pm2 start app.js --name web
#检查日志是否报错
pm2 log 0
#ctrl+c结束日志打印
#保存项目以便下次启动
pm2 save
四、打包成docker镜像
#ctrl+p+q 退出容器
#打包成镜像
docker commit evokey evokey:1
#删除旧容器
docker stop evokey
docker rm evokey
五、启动项目
#使用新镜像创建容器将端口映射出来
docker run -it -d -p 50055:8080 --name evokey --privileged=true evokey:1 /bin/bash
#进入容器启动项目
docker exec -it evokey /bin/bash
#启动项目
pm2 update