/ 环境配置
分类:环境配置来源:站内 最近更新:2022-07-21 19:55:21浏览:1718留言:0
# 设置基础镜像 FROM node:14.17.0 # 这个是容器中的文件目录 RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone # 设置工作目录 WORKDIR /usr/www/egg # 先拷贝package.json文件到工作目录,可以避免每次安装依赖包 COPY package.json /usr/www/egg/package.json # 安装npm依赖(使用淘宝的镜像源) RUN npm i --production --registry=https://registry.npmmirror.com # 拷贝所有源代码到工作目 COPY . /usr/www/egg # 暴露容器端口 EXPOSE 7001 # 创建容器时运行 CMD npm run start
1、docker ps 未能看见对应启动的egg服务器,同时访问7001端口不能访问。因为docker容器必须有一个前台进程才是running状态,不然启动后就立刻关闭了,只能在docker ps -a 里看见容器。所以配置的时候要主要检查package.json的命令行 --daemon 参数 ,如果加了这个就允许在后台模式运行,所以要在docker中直接去掉
2、因为每次发布后,会生成新的镜像和容器,导致历史日志必须在老的容器里才能看到,所以要把日志文件给映射出来。在创建容器时用 -v 参数,如下:
docker run -itd -p 7001:7001 -v /日常日志服务/logs/egg:/usr/www/egg/logs/egg [镜像]
这样在日志服务器里,直接查看历史的日志记录了。
3、docker配置离不开K8s,egg在用npm run start 会根据当前CPU内核数量启动worker进程。但是如果K8S给只给容器分了2核,就会出现启动报如下错误:
2022-07-21 09:17:57.860] [cfork:master:31] worker:50 disconnect (exitedAfterDisconnect: false, state: disconnected, isDead: false, worker.disableRefork: true) [2022-07-21 09:17:57.861] [cfork:master:31] don't fork, because worker:50 will be kill soon 2022-07-21 09:17:57,861 INFO 31 [master] app_worker#2:50 disconnect, suicide: false, state: disconnected, current workers: ["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"] 2022-07-21 09:17:58,059 ERROR 31 nodejs.AgentWorkerDiedError: [master] agent_worker#1:38 died (code: null, signal: SIGKILL) at Master.onAgentExit (/data/node_modules/egg-cluster/lib/master.js:426:17) at Master.emit (events.js:376:20) at Messenger.sendToMaster (/data/node_modules/egg-cluster/lib/utils/messenger.js:137:17) at Messenger.send (/data/node_modules/egg-cluster/lib/utils/messenger.js:102:12) at ChildProcess.<anonymous> (/data/node_modules/egg-cluster/lib/master.js:290:22) at Object.onceWrapper (events.js:483:26) at ChildProcess.emit (events.js:376:20) at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12) name: "AgentWorkerDiedError" pid: 31
所以这个时候,你需要在命令行加上woker数量配置 --worker=n
egg-scripts start --workers=2
上一篇:前端常用工具类命令(备忘)