{"id":199901,"date":"2020-09-14T09:17:05","date_gmt":"2020-09-14T01:17:05","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=199901"},"modified":"2020-09-01T16:17:48","modified_gmt":"2020-09-01T08:17:48","slug":"docker-node","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/docker-node.html","title":{"rendered":"Docker\u5982\u4f55\u9ad8\u6548\u90e8\u7f72Node\u5e94\u7528\uff1f\u770b\u8fd9\u91cc"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4e00\u4e2a\u5408\u7406\u5e76\u4e14\u9ad8\u6548\u7684\u90e8\u7f72\u65b9\u6848\uff0c\u4e0d\u4ec5\u80fd\u591f\u5b9e\u73b0\u5feb\u901f\u5347\u7ea7\uff0c\u5e73\u6ed1\u5207\u6362\uff0c\u8d1f\u8f7d\u5747\u8861\uff0c\u5e94\u7528\u9694\u79bb\u7b49\u90e8\u7f72\u7279\u6027\uff0c\u800c\u4e14\u914d\u6709\u4e00\u5957\u6210\u719f\u7a33\u5b9a\u7684\u76d1\u63a7\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u275d \u5982\u4f55\u5728\u751f\u4ea7\u73af\u5883\u90e8\u7f72\u4e00\u4e2a Node \u5e94\u7528?[1]\u275e<\/p>\n

\u4e00\u4e2a\u5408\u7406\u5e76\u4e14\u9ad8\u6548\u7684\u90e8\u7f72\u65b9\u6848\uff0c\u4e0d\u4ec5\u80fd\u591f\u5b9e\u73b0\u5feb\u901f\u5347\u7ea7\uff0c\u5e73\u6ed1\u5207\u6362\uff0c\u8d1f\u8f7d\u5747\u8861\uff0c\u5e94\u7528\u9694\u79bb\u7b49\u90e8\u7f72\u7279\u6027\uff0c\u800c\u4e14\u914d\u6709\u4e00\u5957\u6210\u719f\u7a33\u5b9a\u7684\u76d1\u63a7\u3002<\/p>\n

kubernetes \u628a Node \u5e94\u7528\u89c6\u4f5c\u4e00\u4e2a\u670d\u52a1\u7aef\u5e94\u7528\u7684\u9ed1\u76d2\u5b50\uff0c\u5b8c\u7f8e\u5339\u914d\u4e86\u4ee5\u4e0a\u6761\u4ef6\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u56e2\u961f\u628a node \u90e8\u7f72\u5728 k8s \u4e0a\u3002<\/p>\n

\u4f46\u5728\u6b64\u4e4b\u524d\uff0c\u9700\u8981\u5148\u628a Node \u5e94\u7528\u8dd1\u5728\u4e00\u4e2a Docker \u5bb9\u5668\u4e0a\uff0c\u8fd9\u4e5f\u662f\u672c\u7ae0\u7684\u4e3b\u9898\u3002<\/p>\n

\u4e00\u4e2a\u7b80\u5355\u7684 Node \u5e94\u7528<\/strong><\/div>\n
\u300cindex.js\u300d<\/strong><\/span><\/div>\n

\u4e00\u4e2a hello, world \u7248\u7684 Node Web App<\/p>\n

\r\nconst http = require('http') \r\n \r\nconst app = async (req, res) => { \r\n  res.end('hello, world') \r\n} \r\n \r\nhttp.createServer(app).listen(3000, () => console.log(3000)) \r\n<\/pre>\n
\u300cpackage.json\u300d<\/strong><\/span><\/div>\n

\u914d\u7f6e npm start \u6765\u542f\u52a8\u5e94\u7528<\/p>\n

\r\n\"scripts\": { \r\n  \"start\": \"node index.js\" \r\n}, \r\n<\/pre>\n

\u4f46\u8fd9\u4ec5\u4ec5\u662f\u6700\u7b80\u5355\u7684 Node \u5e94\u7528\uff0c\u771f\u5b9e\u73af\u5883\u4e2d\u8fd8\u6709\u5404\u79cd\u6570\u636e\u5b58\u50a8\u53ca\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u7b49\uff0c\u6682\u6487\u5f00\u4e0d\u8c08\uff0c\u8fd9\u5df2\u7ecf\u8db3\u591f\u4e86\u3002<\/p>\n

\u518d\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u70b9\u7684 Node \u5e94\u7528\u53ef\u4ee5\u67e5\u770b\u5c71\u6708\u7684\u9879\u76ee whoami[5]: \u4e00\u4e2a\u6700\u7b80\u5316\u7684 serverless \u4e0e dockerize \u793a\u4f8b\u3002<\/p>\n

NODE_ENV=production<\/strong><\/div>\n

\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u65e0\u9700\u5b89\u88c5 devDependecies \u4e2d\u4f9d\u8d56\uff0cNODE_ENV \u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e3a production \u65f6\u5c06\u4f1a\u8df3\u8fc7 devDep\u3002<\/p>\n

\r\n# \u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u53ea\u5b89\u88c5\u751f\u4ea7\u73af\u5883\u4f9d\u8d56 \r\n$ NODE_ENV=production npm ci \r\n\r\n# \u901a\u8fc7\u663e\u5f0f\u6307\u5b9a flag\uff0c\u53ea\u5b89\u88c5\u751f\u4ea7\u73af\u5883\u4f9d\u8d56 \r\n$ npm ci --production \r\n<\/pre>\n

\u53e6\u4e00\u65b9\u9762\uff0c\u67d0\u4e9b\u7b2c\u4e09\u65b9\u6a21\u5757\u4f1a\u6839\u636e NODE_ENV \u73af\u5883\u53d8\u91cf\u505a\u51fa\u4e00\u4e9b\u610f\u6599\u4e0d\u5230\u7684\u914d\u7f6e\u3002\u56e0\u6b64\u5728\u751f\u4ea7\u73af\u5883\u6ce8\u610f\u8be5\u73af\u5883\u53d8\u91cf\u7684\u914d\u7f6e\u3002<\/p>\n

\u4e00\u4e2a Node \u5e94\u7528\u7684\u7b80\u5355\u90e8\u7f72<\/strong><\/div>\n

\u4e00\u4e2a\u5178\u578b\u7684\u3001\u9762\u5411\u670d\u52a1\u7aef\u7684 Node \u5e94\u7528\u662f\u8fd9\u4e48\u8dd1\u8d77\u6765\u7684:<\/p>\n

\r\nnpm install\r\nnpm run config\uff0c\u4ece\u914d\u7f6e\u670d\u52a1(consul\/vault)\u62c9\u53d6\u914d\u7f6e \uff0c\u5982\u6570\u636e\u5e93\u4e0e\u7f13\u5b58\u7684\u8d26\u53f7\u5bc6\u7801\uff0c\u6b64\u65f6\u6784\u5efa\u670d\u52a1\u5668\u9700\u8981\u914d\u7f6e\u670d\u52a1\u6743\u9650\r\nnpm run migrate\uff0c\u6570\u636e\u5e93\u8fc1\u79fb\u811a\u672c\uff0c\u6267\u884c\u6570\u636e\u5e93\u8868\u5217\u884c\u66f4\u6539\u64cd\u4f5c\uff0c\u6b64\u65f6\u6784\u5efa\u670d\u52a1\u5668\u9700\u8981\u6570\u636e\u5e93\u8bbf\u95ee\u6743\u9650\r\nnpm start\uff0c\u542f\u52a8\u4e00\u4e2a Node \u670d\u52a1\r\n\u628a\u8fd0\u884c\u6b65\u9aa4\u7ffb\u8bd1\u4e3a Dockerfile:\r\n\r\n# \u9009\u62e9\u4e00\u4e2a\u4f53\u79ef\u5c0f\u7684\u955c\u50cf (~5MB) \r\nFROM node:12-alpine \r\n \r\n# \u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u751f\u4ea7\u73af\u5883 \r\nENV NODE_ENV production \r\n \r\nWORKDIR \/code \r\n \r\n# \u66f4\u597d\u7684\u6839\u636e Image Layer \u5229\u7528\u7f13\u5b58 \r\nADD package.json package-lock.json \/code \r\nRUN npm ci \r\n \r\nADD . \/code \r\n \r\n# \u914d\u7f6e\u670d\u52a1\u53ca\u6570\u636e\u5e93\u8fc1\u79fb \r\nRUN npm run config --if-present && npm run migrate --if-present \r\n \r\nEXPOSE 3000 \r\nCMD npm start \r\n<\/pre>\n

\u8fd9\u5bf9\u4e8e\u5927\u90e8\u5206 Node \u5e94\u7528\u5df2\u7ecf\u662f\u8db3\u591f\u4e86\uff0c\u5982\u679c\u7cbe\u76ca\u6c42\u7cbe\uff0c\u53ef\u4ee5\u518d\u8d70\u63a5\u4e0b\u6765\u7684\u591a\u9636\u6bb5\u6784\u5efa<\/p>\n

node-gyp \u4e0e Native Addon<\/strong><\/span><\/div>\n

\u5728 Node \u4e2d\u6709\u53ef\u80fd\u5b58\u5728\u7740\u4e00\u4e9b Native Addon\uff0c\u5b83\u4eec\u901a\u8fc7 node-gyp \u8fdb\u884c\u7f16\u8bd1\uff0c\u800c\u5b83\u4f9d\u8d56\u4e8e python\uff0cmake \u4e0e g++\u3002<\/p>\n

\r\n$ apk --no-cache add python make g++ \r\n<\/pre>\n

\u5728\u5e26\u6709\u7f16\u8bd1\u8fc7\u7a0b\u7684\u955c\u50cf\u6784\u5efa\u4e2d\uff0c\u6e90\u6587\u4ef6\u4e0e\u6784\u5efa\u5de5\u5177\u90fd\u4f1a\u9020\u6210\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002\u501f\u52a9\u955c\u50cf\u7684\u300c\u591a\u9636\u6bb5\u6784\u5efa\u300d\u53ef\u4ee5\u9ad8\u6548\u5229\u7528\u7a7a\u95f4\u3002Go App \u4e0e FE App \u7684\u6784\u5efa\u4e5f\u9075\u5faa\u6b64\u89c4\u5219\u3002<\/p>\n

\u591a\u9636\u6bb5\u6784\u5efa Go \u5e94\u7528[6]
\n\u591a\u9636\u6bb5\u6784\u5efa\u524d\u7aef\u5e94\u7528[7]
\n\u5728\u6784\u5efa Node \u5e94\u7528\u955c\u50cf\u65f6\uff0c\u7b2c\u4e00\u5c42\u955c\u50cf\u7528\u4ee5\u6784\u9020 node_modules\u3002<\/p>\n

\r\n# \u9009\u62e9\u4e00\u4e2a\u4f53\u79ef\u5c0f\u7684\u955c\u50cf (~5MB) \r\nFROM node:12-alpine as builder \r\n \r\n# \u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u751f\u4ea7\u73af\u5883 \r\nENV NODE_ENV production \r\n \r\n# \u66f4\u597d\u7684\u6839\u636e Image Layer \u5229\u7528\u7f13\u5b58 \r\nADD package.json package-lock.json .\/ \r\nRUN npm ci \r\n \r\n# \u591a\u9636\u6bb5\u6784\u5efa\u4e4b\u7b2c\u4e8c\u9636\u6bb5 \r\n# \u591a\u9636\u6bb5\u6784\u5efa\u4e4b\u7b2c\u4e8c\u9636\u6bb5 \r\n# \u591a\u9636\u6bb5\u6784\u5efa\u4e4b\u7b2c\u4e8c\u9636\u6bb5 \r\nFROM node:12-alpine \r\n \r\nWORKDIR \/code \r\nENV NODE_ENV production \r\n \r\nADD . . \r\nCOPY --from=builder node_modules node_modules \r\n# \u914d\u7f6e\u670d\u52a1\u53ca\u6570\u636e\u5e93\u8fc1\u79fb \r\nRUN npm run config --if-present && npm run migrate --if-present \r\n \r\nEXPOSE 3000 \r\nCMD npm start \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u275d \u5982\u4f55\u5728\u751f\u4ea7\u73af\u5883\u90e8\u7f72\u4e00\u4e2a Node \u5e94\u7528?[1]\u275e \u4e00\u4e2a\u5408\u7406\u5e76\u4e14\u9ad8\u6548\u7684\u90e8\u7f72\u65b9\u6848\uff0c\u4e0d\u4ec5\u80fd\u591f\u5b9e\u73b0\u5feb\u901f\u5347\u7ea7\uff0c\u5e73\u6ed1\u5207 […]<\/p>\n","protected":false},"author":1898,"featured_media":199906,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[438],"class_list":["post-199901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-docker"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/199901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=199901"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/199901\/revisions"}],"predecessor-version":[{"id":199933,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/199901\/revisions\/199933"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/199906"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=199901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=199901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=199901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}