您现在的位置是:貌合心离网 > 热点
在 Docker 中如何高效部署 Node Server
貌合心离网2026-11-14【热点】{固定数字1}人已围观
简介黑帽seo寄生虫程序【tg:seogoogle987】
大家好,我是山月。
一个高效合理的部署方案,不仅能够实现快速升级,滚动更新,负载均衡,应用隔离等部署特性,而且配有一套成熟稳定的监控。
kubernetes 把 Node 应用视作一个服务端应用的黑盒子,完美匹配了以上条件,越来越多的团队把 node 部署在 k8s 上。
但在此之前,需要先把 Node 应用跑在一个 Docker 容器上,这也是本章的主题。
「目录」
1. 一个简单的 Node 应用
2. NODE_ENV=production
3. 一个 Node 服务的镜像
4. node-gyp 与 Native Addon
5. 相关文章
1. 一个简单的 Node 应用在聚土成沙,集腋成裘。从一个 hello, world 版的 Node Server 说起。
const http = require('http') const app = async (req, res) => { res.end('hello, world') } http.createServer(app).lDoisten(3000, () => console.log(3000))在启动一个 Node Server 时,在生产环境中有很多先决条件,无法通过简单的 node index.js 启动服务。
此时在 package.json 中抽象一层,通过 npm start 启动服务,方便在 Docker 镜像中配置启动命令。
"scripts": { "start": "node index.js" },但这仅仅是ck最简单的 Node 应用,真实环境中还有各种数据存储、定时任务调度等,暂撇开不谈,目前已经足够了。
2. NODE_ENV=production在生产环境中,如果无构建过程,则无需安装 devDependencies 中依赖。NODE_ENV 环境变量设置为 production 时将会跳过 devDependencies 依赖的安装。
# 通过设置环境变量,只安装生产环境依赖 $ NODE_ENV=production npm ci # 通过显式指定&nbersp;flag,只安装生产环境依赖 $ npm ci --production另一方面,「某些第三方库会根据 NODE_ENV 环境变量做出一些意料不到的配置」。因此在生产环境注意该环境变量的配置。
3. 一个 Node 服务的镜像一个典型的、面向服务端的 Node 服务是这么跑起来的:
npm install npm run config,从配置服务(consul/vault)拉取配置 ,如数据库与缓存的账号密码,此时构建服务器需要配置服务权限 npm run migrate,数据库迁移脚本, 中执行数据库表列行更改操作,此时构建服务器需要数据库访问权限 npm start,启动一个 Node 服务把运行步骤翻译为 Dockerfile:
# 选择一个体积小的镜像 (~5MB) FROM node:12-alpine # 环境变量设置为生产环境,设置该环境变量,将不会下载 devDependencies 中依赖 # 如果仍需要 devDependencies 依赖下载,则把该命令移动到如何 RUN npm ci 之后 ENV NODE_ENV production WORKDIR /code # 首先添加 package.json ,为了更好的根据 Image Layer 利用缓存 # 当 package.json 不变时,node_modules 将会重用,则能够利用高效缓存 ADD package.json package-lock.json /code # 可考虑 npm ci 与 yarn RUN npm i # 把代码置于镜像 ADD . /code # 配置服务及数据库迁移 RUN npm run conf部署ig --if-present && npm run migrate --if-present EXPOSE 3000 # 启动 Node Server CMD npm start这对于大部分 Node 应用已经是足够了,精益求精,接下来进行多阶段构建的优化。
4. node-gyp 与 Native Addon在 Node 中的一 N些依赖存在 Native Addon,它们通过 node-gyp 进行编译,而它依赖于 python,make 与 g++。
$ apk --no-cache add python make g++在带有编译过程的镜像构建中,源文件与构建工具都会造成空间的浪费。
借助镜像的「多阶段构建」可以高效利用空间。Go 语言与前端相关的构建也遵循此规则。
多阶段构建 Go 应用 多阶段构建前端应用在构建 Node 应用镜像时,第一层镜像用各种构建工具以构造 node_modules,od第二层镜像利用第一层镜像构造的 node_modules。
# 选择一个体积小的镜像 (~5MB) FROM node:12-alpine as builder # 环境变量设置为生产环境 ENV NODE_ENV production # 为某些特殊的依赖库准备编译环境 RUN apk --no-cache add e python make g++ # 更好的根据 Image Layer 利用缓存 ADD package.json package-lock.json ./ RUN npm i # 多阶段构建之第二阶段 # 多阶段构建之第二阶段 # 多阶段构建之第二阶段 FROM nodeSe:12-alpine WORKDIR /code ENV NODE_ENV production ADD . . COPY --from=builder node_modules node_modules # 配置服务及数据库迁移 RUN npm run config --if-present&nbsrvp;&& npm run migrate --if-present EXPOSE 3000 CMD npm start 5. 相关文章 N-API and getting started with writing C addons for Node.js Using Docker for Node.js in Development and Production【编辑推荐】
聊聊内存中的Slice操作 微信占内存太大?两er分钟教你解放手机 记一次 Java 应用内存泄漏的定位过程 为什么你的微信总是占用了几十个G的内存空间? 生产环境内存溢出了!!!你会解决吗? 【责任编辑:武晓燕 TEL:(010)68476606】点赞 0
很赞哦!(838147)
热门文章
站长推荐
友情链接
- 标签: 康师傅
- https://www.ucycle.com/search/=黑帽seo寄生虫程序【tg:seogoogle987】,eYO
- https://www.rollingstone.it/?ID=DVMeu3AlmzZE1cuJ2FmgmO5TSCQRb3yh&s=黑帽seo寄生虫程序【tg:seogoogle987】,cZH
- https://www.sic-info.org/vi/?s=黑帽seo寄生虫程序【tg:seogoogle987】,JBt
- https://blvdseafood.com/?s=黑帽seo寄生虫程序【tg:seogoogle987】ua5
- https://www.trockenmittel.ch/catalogsearch/result/?iC=ZM3&q=黑帽seo寄生虫程序【tg:seogoogle987】,1ba
- https://www.terramania.nl/catalogsearch/result/index/?q=黑帽seo寄生虫程序【tg:seogoogle987】,CBm
- https://www.thealexandrian.net/?s=黑帽seo寄生虫程序【tg:seogoogle987】,RtH
- https://www.jusoen.com/addreng.asp?p1=黑帽seo寄生虫程序【tg:seogoogle987】vuh
- https://yscc1986.net/search/黑帽seo寄生虫程序【tg:seogoogle987】wAz
- https://www.kaskus.co.id/search?q=黑帽seo寄生虫程序【tg:seogoogle987】,P1O
- https://www.springfieldlibrary.org/library/?s=黑帽seo寄生虫程序【tg:seogoogle987】,gPx
- https://zhidao.baidu.com/index/?action=QUERY&word=黑帽seo寄生虫程序【tg:seogoogle987】,f7g
- https://www.simcoemuskokahealth.org/SearchResults.aspx?SearchQuery=黑帽seo寄生虫程序【tg:seogoogle987】,PzY
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=黑帽seo寄生虫程序【tg:seogoogle987】1cg
- https://www.lyrics.com/lyrics/kkk3黑帽seo寄生虫程序【tg:seogoogle987】,qv8
- https://www.maafushidive.com?collections?vendors?q=黑帽seo寄生虫程序【tg:seogoogle987】,WXa
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=黑帽seo寄生虫程序【tg:seogoogle987】6NO
- https://www.youthforum.org/search-results?cx=4f5236218ab5e8ef9&ie=UTF-8&q=黑帽seo寄生虫程序【tg:seogoogle987】,myE
- https://www.qwant.com/?q=黑帽seo寄生虫程序【tg:seogoogle987】,h8Y
- https://www.paulaschoice.hk/zh_hant/catalogsearch/result/?q=黑帽seo寄生虫程序【tg:seogoogle987】,PG8
- https://www.mexperience.com/?T=1686132112324&s=黑帽seo寄生虫程序【tg:seogoogle987】,h8h
- https://www.madebyoranges.com/nl/search/黑帽seo寄生虫程序【tg:seogoogle987】,1d8
- https://www.remit.co.jp/zh/?s=黑帽seo寄生虫程序【tg:seogoogle987】,gAR
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=黑帽seo寄生虫程序【tg:seogoogle987】Th9
- https://www.lifesciencesweden.se/search?q=黑帽seo寄生虫程序【tg:seogoogle987】,2kh
- https://www.uctronics.com/catalogsearch/result/?q=黑帽seo寄生虫程序【tg:seogoogle987】,1WI
- https://www.routesonline.com/search/?query=黑帽seo寄生虫程序【tg:seogoogle987】,GZ4
- https://dict.naver.com/dict.search?dicQuery&query=黑帽seo寄生虫程序【tg:seogoogle987】1sI
- https://www.sweat.com/collections/vendors?q=黑帽seo寄生虫程序【tg:seogoogle987】,YC8
- https://www.ncetm.org.uk/search?action=CHECK&q=黑帽seo寄生虫程序【tg:seogoogle987】,CMa
- https://www.lssystems.co.uk/search?page=0&per_page=24&term=黑帽seo寄生虫程序【tg:seogoogle987】,cYe
- https://www.remit.co.jp/vi/?s=黑帽seo寄生虫程序【tg:seogoogle987】,iAS
- https://www.teamlab.art/?submit=黑帽seo寄生虫程序【tg:seogoogle987】,HjX
- https://www.rotherham.gov.uk/site-search/results/?q=黑帽seo寄生虫程序【tg:seogoogle987】,fox
- https://www.normani.de/search?q=黑帽seo寄生虫程序【tg:seogoogle987】,j18
- https://www.scq.ubc.ca/?page=2&s=黑帽seo寄生虫程序【tg:seogoogle987】,lkO
- https://www.miamiopenmri.com/?action=CHECK&s=黑帽seo寄生虫程序【tg:seogoogle987】,Dmb
- https://www.synonyms.com/antonyms/黑帽seo寄生虫程序【tg:seogoogle987】,QuH
- https://www.vet-direct.com/search?cat=72&term=黑帽seo寄生虫程序【tg:seogoogle987】,sWm
- https://www.flickr.com/photos/tags/黑帽seo寄生虫程序【tg:seogoogle987】hu0
- https://www.mintos.org/?s=黑帽seo寄生虫程序【tg:seogoogle987】,3Tf
- https://yscc1986.net/search/黑帽seo寄生虫程序【tg:seogoogle987】Ovl
- https://www.nbm.org/?action=QUERY&s=黑帽seo寄生虫程序【tg:seogoogle987】,TBO
- https://www.safetv.go.kr/base/search/view?searchWord=黑帽seo寄生虫程序【tg:seogoogle987】HsY
- https://www.soccerbible.com/search?q=黑帽seo寄生虫程序【tg:seogoogle987】,dVQ
- https://www.urmc.rochester.edu/news/search?q=黑帽seo寄生虫程序【tg:seogoogle987】,zsy
- https://www.tombow.com/en/search/黑帽seo寄生虫程序【tg:seogoogle987】,z6V
- https://www.lyrics.com/lyrics/黑帽seo寄生虫程序【tg:seogoogle987】,tuo
- https://www.paspaley.com/catalogsearch/result/?action=QUERY&q=黑帽seo寄生虫程序【tg:seogoogle987】,eqR
- https://www.unclefossil.com/search/黑帽seo寄生虫程序【tg:seogoogle987】,YCu







