{"id":181,"date":"2026-06-20T10:29:37","date_gmt":"2026-06-20T02:29:37","guid":{"rendered":"https:\/\/yykcj.com\/?p=181"},"modified":"2026-06-20T10:29:37","modified_gmt":"2026-06-20T02:29:37","slug":"docker-%e5%a4%9a%e9%98%b6%e6%ae%b5%e6%9e%84%e5%bb%ba%e4%bc%98%e5%8c%96%e5%ae%9e%e8%b7%b5%ef%bc%9a%e4%bb%8e-1-5gb-%e5%88%b0-150mb-%e7%9a%84%e8%9c%95%e5%8f%98","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=181","title":{"rendered":"Docker \u591a\u9636\u6bb5\u6784\u5efa\u4f18\u5316\u5b9e\u8df5\uff1a\u4ece 1.5GB \u5230 150MB \u7684\u8715\u53d8"},"content":{"rendered":"<h2>\u80cc\u666f<\/h2>\n<p>\u5728\u5fae\u670d\u52a1\u67b6\u6784\u76db\u884c\u7684\u4eca\u5929\uff0cDocker \u5df2\u6210\u4e3a\u5e94\u7528\u90e8\u7f72\u7684\u6807\u51c6\u65b9\u5f0f\u3002\u7136\u800c\uff0c\u5f88\u591a\u5f00\u53d1\u8005\u7684 Docker \u955c\u50cf\u4f53\u79ef\u81c3\u80bf\uff0c\u52a8\u8f84\u51e0\u767e MB \u751a\u81f3 GB \u7ea7\u522b\uff0c\u5bfc\u81f4\u6784\u5efa\u6162\u3001\u4f20\u8f93\u6162\u3001\u90e8\u7f72\u6162\u3002\u4ee5 Node.js \u5e94\u7528\u4e3a\u4f8b\uff0c\u4e00\u4e2a\u7b80\u5355\u7684 Express \u670d\u52a1\uff0c\u5982\u679c\u76f4\u63a5\u4f7f\u7528 <code>node:18<\/code> \u57fa\u7840\u955c\u50cf\uff0c\u4f53\u79ef\u5927\u7ea6 1.5GB\u3002\u4f46\u901a\u8fc7\u591a\u9636\u6bb5\u6784\u5efa\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u7f29\u5c0f\u5230 150MB \u5de6\u53f3\uff0c\u540c\u65f6\u4fdd\u7559\u8fd0\u884c\u6240\u9700\u7684\u4e00\u5207\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb\uff0c\u5e26\u4f60\u5b9e\u8df5 Docker \u591a\u9636\u6bb5\u6784\u5efa\uff0c\u5e76\u5206\u4eab\u4e00\u4e9b\u72ec\u7279\u7684\u4f18\u5316\u6280\u5de7\u3002<\/p>\n<h2>\u4ec0\u4e48\u662f\u591a\u9636\u6bb5\u6784\u5efa\uff1f<\/h2>\n<p>\u591a\u9636\u6bb5\u6784\u5efa\uff08Multi-stage Build\uff09\u662f Docker 17.05 \u5f15\u5165\u7684\u7279\u6027\u3002\u5b83\u5141\u8bb8\u5728\u540c\u4e00\u4e2a Dockerfile \u4e2d\u4f7f\u7528\u591a\u4e2a <code>FROM<\/code> \u6307\u4ee4\uff0c\u6bcf\u4e2a <code>FROM<\/code> \u53ef\u4ee5\u57fa\u4e8e\u4e0d\u540c\u7684\u57fa\u7840\u955c\u50cf\uff0c\u5e76\u4e14\u53ea\u6709\u6700\u540e\u4e00\u4e2a\u9636\u6bb5\u4f1a\u751f\u6210\u6700\u7ec8\u955c\u50cf\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6784\u5efa\u73af\u5883\u548c\u8fd0\u884c\u73af\u5883\u5206\u79bb\uff0c\u53ea\u5c06\u5fc5\u8981\u7684\u4ea7\u7269\u590d\u5236\u5230\u6700\u7ec8\u955c\u50cf\u4e2d\u3002<\/p>\n<h2>\u5b9e\u6218\uff1a\u4f18\u5316\u4e00\u4e2a Node.js \u5e94\u7528<\/h2>\n<p>\u6211\u4eec\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684 Node.js Web \u5e94\u7528\u4e3a\u4f8b\uff0c\u5b83\u4f7f\u7528 Express \u6846\u67b6\uff0c\u5e76\u4e14\u9700\u8981\u7f16\u8bd1 TypeScript \u548c\u6253\u5305\u524d\u7aef\u8d44\u6e90\u3002<\/p>\n<h3>\u521d\u59cb Dockerfile\uff08\u5355\u9636\u6bb5\uff09<\/h3>\n<pre><code class=\"language-dockerfile\">\nFROM node:18\n\nWORKDIR \/app\n\nCOPY package*.json .\/\nRUN npm install\n\nCOPY . .\n\nRUN npm run build\n\nEXPOSE 3000\n\nCMD [&quot;node&quot;, &quot;dist\/index.js&quot;]\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u955c\u50cf\u5305\u542b node \u8fd0\u884c\u65f6\u3001\u6240\u6709\u5f00\u53d1\u4f9d\u8d56\u3001\u6e90\u7801\u3001\u4ee5\u53ca\u6784\u5efa\u4ea7\u7269\uff0c\u4f53\u79ef\u7ea6\u4e3a 1.5GB\u3002<\/p>\n<h3>\u4f18\u5316\u7b2c\u4e00\u6b65\uff1a\u591a\u9636\u6bb5\u6784\u5efa<\/h3>\n<p>\u6211\u4eec\u5c06\u6784\u5efa\u8fc7\u7a0b\u62c6\u5206\u4e3a\u4e24\u4e2a\u9636\u6bb5\uff1a<\/p>\n<ul>\n<li><strong>\u6784\u5efa\u9636\u6bb5<\/strong>\uff1a\u4f7f\u7528 <code>node:18<\/code> \u955c\u50cf\uff0c\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\uff08\u5305\u62ec\u5f00\u53d1\u4f9d\u8d56\uff09\uff0c\u6267\u884c\u6784\u5efa\u3002<\/li>\n<li><strong>\u8fd0\u884c\u9636\u6bb5<\/strong>\uff1a\u4f7f\u7528\u66f4\u8f7b\u91cf\u7684 <code>node:18-alpine<\/code> \u955c\u50cf\uff0c\u4ec5\u590d\u5236\u6784\u5efa\u4ea7\u7269\u548c\u8fd0\u884c\u65f6\u4f9d\u8d56\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-dockerfile\">\n# \u6784\u5efa\u9636\u6bb5\nFROM node:18 AS builder\n\nWORKDIR \/app\n\nCOPY package*.json .\/\nRUN npm install\n\nCOPY . .\nRUN npm run build\n\n# \u8fd0\u884c\u9636\u6bb5\nFROM node:18-alpine\n\nWORKDIR \/app\n\n# \u590d\u5236\u6784\u5efa\u4ea7\u7269\nCOPY --from=builder \/app\/dist .\/dist\nCOPY --from=builder \/app\/node_modules .\/node_modules\n\nEXPOSE 3000\n\nCMD [&quot;node&quot;, &quot;dist\/index.js&quot;]\n<\/code><\/pre>\n<p>\u6b64\u65f6\uff0c\u955c\u50cf\u4f53\u79ef\u4ece 1.5GB \u964d\u81f3\u7ea6 300MB\u3002\u4f46\u8fd8\u4e0d\u591f\uff0c\u56e0\u4e3a <code>node_modules<\/code> \u4e2d\u5305\u542b\u4e86\u5927\u91cf\u5f00\u53d1\u4f9d\u8d56\uff08\u5982 TypeScript\u3001\u6d4b\u8bd5\u6846\u67b6\u7b49\uff09\uff0c\u8fd9\u4e9b\u5728\u8fd0\u884c\u65f6\u5e76\u4e0d\u9700\u8981\u3002<\/p>\n<h3>\u4f18\u5316\u7b2c\u4e8c\u6b65\uff1a\u5206\u79bb\u4f9d\u8d56<\/h3>\n<p>\u66f4\u597d\u7684\u505a\u6cd5\u662f\uff0c\u5728\u6784\u5efa\u9636\u6bb5\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\uff0c\u4f46\u4ec5\u590d\u5236\u8fd0\u884c\u65f6\u4f9d\u8d56\u5230\u6700\u7ec8\u955c\u50cf\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>npm ci --production<\/code> \u6216 <code>npm prune --production<\/code> \u6765\u5b9e\u73b0\u3002<\/p>\n<pre><code class=\"language-dockerfile\">\n# \u6784\u5efa\u9636\u6bb5\nFROM node:18 AS builder\n\nWORKDIR \/app\n\nCOPY package*.json .\/\nRUN npm install\n\nCOPY . .\nRUN npm run build\n\n# \u8fd0\u884c\u9636\u6bb5\nFROM node:18-alpine\n\nWORKDIR \/app\n\n# \u590d\u5236\u6784\u5efa\u4ea7\u7269\nCOPY --from=builder \/app\/dist .\/dist\n\n# \u590d\u5236 package.json \u5e76\u4ec5\u5b89\u88c5\u751f\u4ea7\u4f9d\u8d56\nCOPY package*.json .\/\nRUN npm ci --production\n\nEXPOSE 3000\n\nCMD [&quot;node&quot;, &quot;dist\/index.js&quot;]\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a<code>npm ci --production<\/code> \u4f1a\u5b89\u88c5 <code>dependencies<\/code> \u4e2d\u7684\u5305\uff0c\u5ffd\u7565 <code>devDependencies<\/code>\u3002\u786e\u4fdd\u4f60\u7684 <code>package.json<\/code> \u6b63\u786e\u533a\u5206\u4e86\u8fd0\u884c\u65f6\u548c\u5f00\u53d1\u4f9d\u8d56\u3002<\/p>\n<p>\u73b0\u5728\u955c\u50cf\u4f53\u79ef\u964d\u81f3\u7ea6 150MB\u3002<\/p>\n<h3>\u4f18\u5316\u7b2c\u4e09\u6b65\uff1a\u5229\u7528\u7f13\u5b58\u52a0\u901f\u6784\u5efa<\/h3>\n<p>Docker \u6784\u5efa\u65f6\uff0c\u6bcf\u5c42\u90fd\u4f1a\u7f13\u5b58\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u6307\u4ee4\u987a\u5e8f\uff0c\u6700\u5927\u5316\u5229\u7528\u7f13\u5b58\u3002\u4f8b\u5982\uff0c\u5148\u590d\u5236 <code>package.json<\/code> \u5e76\u5b89\u88c5\u4f9d\u8d56\uff0c\u518d\u590d\u5236\u6e90\u7801\uff0c\u8fd9\u6837\u53ea\u8981 <code>package.json<\/code> \u4e0d\u53d8\uff0c\u4f9d\u8d56\u5c42\u5c31\u4f1a\u88ab\u7f13\u5b58\u3002<\/p>\n<pre><code class=\"language-dockerfile\">\n# \u6784\u5efa\u9636\u6bb5\nFROM node:18 AS builder\n\nWORKDIR \/app\n\n# \u5148\u590d\u5236\u4f9d\u8d56\u6587\u4ef6\uff0c\u5b89\u88c5\u4f9d\u8d56\uff08\u5229\u7528\u7f13\u5b58\uff09\nCOPY package*.json .\/\nRUN npm install\n\n# \u518d\u590d\u5236\u6e90\u7801\nCOPY . .\nRUN npm run build\n\n# \u8fd0\u884c\u9636\u6bb5\nFROM node:18-alpine\n\nWORKDIR \/app\n\n# \u590d\u5236\u6784\u5efa\u4ea7\u7269\nCOPY --from=builder \/app\/dist .\/dist\n\n# \u5b89\u88c5\u751f\u4ea7\u4f9d\u8d56\nCOPY package*.json .\/\nRUN npm ci --production\n\nEXPOSE 3000\n\nCMD [&quot;node&quot;, &quot;dist\/index.js&quot;]\n<\/code><\/pre>\n<h3>\u4f18\u5316\u7b2c\u56db\u6b65\uff1a\u4f7f\u7528\u66f4\u5c0f\u7684\u57fa\u7840\u955c\u50cf<\/h3>\n<p><code>node:18-alpine<\/code> \u5df2\u7ecf\u5f88\u5c0f\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u66f4\u8fdb\u4e00\u6b65\uff1a\u4f7f\u7528 <code>scratch<\/code> \u6216 <code>distroless<\/code> \u955c\u50cf\u3002\u4e0d\u8fc7\uff0cNode.js \u5e94\u7528\u9700\u8981 glibc \u7b49\u7cfb\u7edf\u5e93\uff0c\u6240\u4ee5\u901a\u5e38\u9009\u62e9 <code>alpine<\/code> \u6216 <code>slim<\/code>\u3002<\/p>\n<p>\u5bf9\u4e8e\u9759\u6001\u6587\u4ef6\u670d\u52a1\uff0c\u751a\u81f3\u53ef\u4ee5\u4e0d\u7528 Node \u955c\u50cf\uff0c\u76f4\u63a5\u7528 Nginx\u3002<\/p>\n<h2>\u9ad8\u7ea7\u6280\u5de7\uff1a\u591a\u9636\u6bb5\u6784\u5efa\u7684\u66f4\u591a\u5e94\u7528<\/h2>\n<h3>1. \u6784\u5efa\u591a\u4e2a\u4ea7\u7269<\/h3>\n<p>\u5982\u679c\u4f60\u9700\u8981\u6784\u5efa\u524d\u7aef\u548c\u540e\u7aef\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a Dockerfile \u4e2d\u5b8c\u6210\uff1a<\/p>\n<pre><code class=\"language-dockerfile\">\n# \u524d\u7aef\u6784\u5efa\nFROM node:18 AS frontend-builder\nWORKDIR \/app\nCOPY frontend\/package*.json .\/\nRUN npm install\nCOPY frontend\/ .\/\nRUN npm run build\n\n# \u540e\u7aef\u6784\u5efa\nFROM node:18 AS backend-builder\nWORKDIR \/app\nCOPY backend\/package*.json .\/\nRUN npm install\nCOPY backend\/ .\/\nRUN npm run build\n\n# \u8fd0\u884c\u9636\u6bb5\nFROM node:18-alpine\nWORKDIR \/app\nCOPY --from=backend-builder \/app\/dist .\/dist\nCOPY --from=frontend-builder \/app\/dist .\/public\nCMD [&quot;node&quot;, &quot;dist\/index.js&quot;]\n<\/code><\/pre>\n<h3>2. \u4f7f\u7528\u7f13\u5b58\u6302\u8f7d\uff08BuildKit\uff09<\/h3>\n<p>Docker 18.09+ \u652f\u6301 BuildKit\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>--mount=type=cache<\/code> \u52a0\u901f\u4f9d\u8d56\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"language-dockerfile\">\n# syntax=docker\/dockerfile:1\nFROM node:18 AS builder\nWORKDIR \/app\nRUN --mount=type=cache,target=\/root\/.npm \\\n    npm install\nCOPY . .\nRUN npm run build\n<\/code><\/pre>\n<p>\u8fd9\u4f1a\u5c06 npm \u7f13\u5b58\u6301\u4e45\u5316\uff0c\u907f\u514d\u6bcf\u6b21\u6784\u5efa\u90fd\u91cd\u65b0\u4e0b\u8f7d\u3002<\/p>\n<h3>3. \u5b89\u5168\u6700\u4f73\u5b9e\u8df5<\/h3>\n<ul>\n<li>\u8fd0\u884c\u9636\u6bb5\u4f7f\u7528\u975e root \u7528\u6237\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-dockerfile\">\nFROM node:18-alpine\nRUN addgroup -S appgroup &amp;&amp; adduser -S appuser -G appgroup\nUSER appuser\n# ...\n<\/code><\/pre>\n<ul>\n<li>\u907f\u514d\u590d\u5236\u654f\u611f\u6587\u4ef6\uff08\u5982 <code>.env<\/code>\uff09\uff0c\u4f7f\u7528 Docker \u7684 <code>.dockerignore<\/code>\u3002<\/li>\n<\/ul>\n<h2>\u5e38\u89c1\u5751\u4e0e\u8e29\u5751\u7ecf\u9a8c<\/h2>\n<ul>\n<li><strong>\u57511<\/strong>\uff1a\u5fd8\u8bb0\u590d\u5236 <code>package.json<\/code> \u5bfc\u81f4 <code>npm ci<\/code> \u5931\u8d25\u3002<\/li>\n<li><strong>\u57512<\/strong>\uff1a<code>npm ci<\/code> \u9700\u8981 <code>package-lock.json<\/code>\uff0c\u786e\u4fdd\u5b83\u5b58\u5728\u4e8e\u6e90\u7801\u4e2d\u3002<\/li>\n<li><strong>\u57513<\/strong>\uff1a\u591a\u9636\u6bb5\u6784\u5efa\u4e2d\uff0c\u9636\u6bb5\u540d\u79f0\u5fc5\u987b\u552f\u4e00\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/li>\n<li><strong>\u57514<\/strong>\uff1aAlpine \u955c\u50cf\u4e2d\uff0c\u67d0\u4e9b\u539f\u751f\u6a21\u5757\u53ef\u80fd\u9700\u8981\u7f16\u8bd1\uff0c\u9700\u8981\u5b89\u88c5 <code>build-base<\/code>\u3002<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u591a\u9636\u6bb5\u6784\u5efa\uff0c\u6211\u4eec\u6210\u529f\u5c06\u955c\u50cf\u4f53\u79ef\u4ece 1.5GB \u538b\u7f29\u5230 150MB\uff0c\u540c\u65f6\u63d0\u5347\u4e86\u6784\u5efa\u901f\u5ea6\u548c\u5b89\u5168\u6027\u3002\u5173\u952e\u8981\u70b9\uff1a<\/p>\n<ol>\n<li>\u5206\u79bb\u6784\u5efa\u548c\u8fd0\u884c\u73af\u5883<\/li>\n<li>\u53ea\u590d\u5236\u5fc5\u8981\u6587\u4ef6<\/li>\n<li>\u5229\u7528\u7f13\u5b58\u4f18\u5316\u6784\u5efa\u901f\u5ea6<\/li>\n<li>\u9009\u62e9\u5408\u9002\u7684\u57fa\u7840\u955c\u50cf<\/li>\n<\/ol>\n<p>\u4e0b\u4e00\u6b65\uff0c\u4f60\u53ef\u4ee5\u63a2\u7d22 Docker BuildKit \u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u6216\u8005\u5c1d\u8bd5\u5c06\u955c\u50cf\u4f53\u79ef\u538b\u7f29\u5230 50MB \u4ee5\u4e0b\uff08\u4f7f\u7528 distroless \u6216 scratch\uff09\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.docker.com\/develop\/develop-images\/multistage-build\/\">Docker \u5b98\u65b9\u6587\u6863 &#8211; \u591a\u9636\u6bb5\u6784\u5efa<\/a><\/li>\n<li><a href=\"https:\/\/docs.docker.com\/build\/buildkit\/\">BuildKit \u6587\u6863<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u901a\u8fc7\u4e00\u4e2a Node.js \u5e94\u7528\u6848\u4f8b\uff0c\u6df1\u5165\u8bb2\u89e3 Docker \u591a\u9636\u6bb5\u6784\u5efa\u7684\u539f\u7406\u4e0e\u6700\u4f73\u5b9e\u8df5\uff0c\u624b\u628a\u624b\u6559\u4f60\u5c06\u955c\u50cf\u4f53\u79ef\u4ece 1.5GB \u538b\u7f29\u81f3 150MB\uff0c\u540c\u65f6\u63d0\u5347\u6784\u5efa\u901f\u5ea6\u548c\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[63,60,5,61,62],"class_list":["post-181","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-devops","tag-docker","tag-node-js","tag-61","tag-62"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=181"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/181\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}