{"id":6477,"date":"2024-08-16T15:40:57","date_gmt":"2024-08-16T07:40:57","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6477"},"modified":"2024-08-16T15:40:57","modified_gmt":"2024-08-16T07:40:57","slug":"dockerfile-%e4%b8%ad%e7%9a%84%e5%a4%9a%e9%98%b6%e6%ae%b5%e6%9e%84%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6477","title":{"rendered":"Dockerfile \u4e2d\u7684\u591a\u9636\u6bb5\u6784\u5efa"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p>Dockerfile \u4e2d\u7684\u591a\u9636\u6bb5\u6784\u5efa\uff08Multistage Build\uff09\u662f\u4e00\u79cd\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u4e00\u4e2a Dockerfile \u4e2d\u4f7f\u7528\u591a\u4e2a <code>FROM<\/code> \u6307\u4ee4\u6765\u6784\u5efa\u4e0d\u540c\u7684\u9636\u6bb5\uff0c\u6bcf\u4e2a\u9636\u6bb5\u53ef\u4ee5\u4ea7\u751f\u4e0d\u540c\u7684\u4e2d\u95f4\u955c\u50cf\u3002\u6700\u7ec8\uff0c\u4f60\u53ef\u4ee5\u53ea\u4fdd\u7559\u6700\u540e\u9636\u6bb5\u4e2d\u9700\u8981\u7684\u90e8\u5206\uff0c\u4ece\u800c\u521b\u5efa\u66f4\u5c0f\u3001\u66f4\u9ad8\u6548\u7684\u955c\u50cf\u3002<\/p>\n<h3><strong>\u591a\u9636\u6bb5\u6784\u5efa\u7684\u539f\u7406<\/strong><\/h3>\n<p>\u5728\u4f20\u7edf\u7684 Dockerfile \u4e2d\uff0c\u6240\u6709\u7684\u6784\u5efa\u6b65\u9aa4\u90fd\u5728\u540c\u4e00\u4e2a\u955c\u50cf\u4e2d\u6267\u884c\u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u955c\u50cf\u5305\u542b\u4e0d\u5fc5\u8981\u7684\u6587\u4ef6\u548c\u4f9d\u8d56\uff0c\u589e\u5927\u955c\u50cf\u7684\u4f53\u79ef\u3002\u800c\u591a\u9636\u6bb5\u6784\u5efa\u901a\u8fc7\u5206\u79bb\u6784\u5efa\u548c\u8fd0\u884c\u73af\u5883\uff0c\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<h3><strong>\u591a\u9636\u6bb5\u6784\u5efa\u7684\u7ed3\u6784<\/strong><\/h3>\n<p>\u4e00\u4e2a\u591a\u9636\u6bb5\u6784\u5efa\u7684 Dockerfile \u5305\u542b\u591a\u4e2a <code>FROM<\/code> \u6307\u4ee4\uff0c\u6bcf\u4e2a <code>FROM<\/code> \u90fd\u4ee3\u8868\u4e00\u4e2a\u72ec\u7acb\u7684\u6784\u5efa\u9636\u6bb5\u3002\u4f60\u53ef\u4ee5\u5728\u4e0d\u540c\u9636\u6bb5\u4e2d\u8fdb\u884c\u4e0d\u540c\u7684\u6784\u5efa\u64cd\u4f5c\uff0c\u5e76\u5728\u6700\u7ec8\u9636\u6bb5\u4e2d\u53ea\u590d\u5236\u6240\u9700\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u591a\u9636\u6bb5\u6784\u5efa\u6765\u4f18\u5316 Go \u5e94\u7528\u7684\u955c\u50cf<\/strong><\/p>\n<pre><code class=\"language-dockerfile\"># \u7b2c\u4e00\u9636\u6bb5\uff1a\u6784\u5efa Go \u5e94\u7528\nFROM golang:1.20-alpine AS builder\n\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\nWORKDIR \/app\n\n# \u5c06\u9879\u76ee\u7684 go.mod \u548c go.sum \u590d\u5236\u5230\u5bb9\u5668\u4e2d\uff0c\u5e76\u4e0b\u8f7d\u4f9d\u8d56\nCOPY go.mod go.sum .\/\nRUN go mod download\n\n# \u5c06\u6e90\u4ee3\u7801\u590d\u5236\u5230\u5bb9\u5668\u4e2d\nCOPY . .\n\n# \u7f16\u8bd1\u5e94\u7528\nRUN go build -o myapp\n\n# \u7b2c\u4e8c\u9636\u6bb5\uff1a\u521b\u5efa\u6700\u7ec8\u7684\u8f7b\u91cf\u7ea7\u955c\u50cf\nFROM alpine:3.18\n\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\nWORKDIR \/app\n\n# \u4ece\u7b2c\u4e00\u9636\u6bb5\u590d\u5236\u7f16\u8bd1\u597d\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\nCOPY --from=builder \/app\/myapp .\n\n# \u66b4\u9732\u7aef\u53e3\nEXPOSE 8080\n\n# \u8fd0\u884c\u5e94\u7528\nCMD [&quot;.\/myapp&quot;]<\/code><\/pre>\n<h3><strong>\u793a\u4f8b\u89e3\u6790<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7b2c\u4e00\u9636\u6bb5\uff08\u6784\u5efa\u9636\u6bb5\uff09<\/strong>:<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>golang:1.20-alpine<\/code> \u955c\u50cf\u4f5c\u4e3a\u6784\u5efa\u73af\u5883\u3002<\/li>\n<li>\u7f16\u8bd1 Go \u9879\u76ee\uff0c\u751f\u6210\u4e8c\u8fdb\u5236\u53ef\u6267\u884c\u6587\u4ef6 <code>myapp<\/code>\u3002<\/li>\n<li>\u8fd9\u4e2a\u9636\u6bb5\u4e3b\u8981\u7528\u4e8e\u5b89\u88c5\u4f9d\u8d56\u548c\u7f16\u8bd1\u4ee3\u7801\uff0c\u4f46\u6700\u7ec8\u955c\u50cf\u4e0d\u9700\u8981\u4fdd\u7559\u8fd9\u4e00\u90e8\u5206\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7b2c\u4e8c\u9636\u6bb5\uff08\u8fd0\u884c\u9636\u6bb5\uff09<\/strong>:<\/p>\n<ul>\n<li>\u4f7f\u7528\u66f4\u5c0f\u7684 <code>alpine<\/code> \u955c\u50cf\uff0c\u53ea\u5305\u542b\u8fd0\u884c\u5e94\u7528\u6240\u9700\u7684\u57fa\u7840\u73af\u5883\u3002<\/li>\n<li>\u4ece\u6784\u5efa\u9636\u6bb5\u590d\u5236\u5df2\u7f16\u8bd1\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u5230\u6700\u7ec8\u955c\u50cf\u4e2d\u3002<\/li>\n<li>\u8fd9\u6837\u751f\u6210\u7684\u955c\u50cf\u53ea\u5305\u542b\u8fd0\u884c\u6240\u9700\u7684\u6587\u4ef6\uff0c\u907f\u514d\u4e86\u6784\u5efa\u5de5\u5177\u548c\u6e90\u4ee3\u7801\u7684\u5197\u4f59\uff0c\u4f7f\u955c\u50cf\u66f4\u5c0f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><strong>\u591a\u9636\u6bb5\u6784\u5efa\u7684\u4f18\u52bf<\/strong><\/h3>\n<ol>\n<li>\n<p><strong>\u51cf\u5c0f\u955c\u50cf\u4f53\u79ef<\/strong>: \u901a\u8fc7\u5c06\u6784\u5efa\u6b65\u9aa4\u548c\u6700\u7ec8\u8fd0\u884c\u73af\u5883\u5206\u5f00\uff0c\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6587\u4ef6\u548c\u4f9d\u8d56\u8fdb\u5165\u6700\u7ec8\u955c\u50cf\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c0f\u955c\u50cf\u4f53\u79ef\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u66f4\u6e05\u6670\u7684\u6784\u5efa\u8fc7\u7a0b<\/strong>: \u591a\u9636\u6bb5\u6784\u5efa\u4f7f\u6784\u5efa\u8fc7\u7a0b\u66f4\u52a0\u6a21\u5757\u5316\uff0c\u6bcf\u4e2a\u9636\u6bb5\u6709\u660e\u786e\u7684\u804c\u8d23\uff0c\u5982\u4f9d\u8d56\u5b89\u88c5\u3001\u4ee3\u7801\u7f16\u8bd1\u548c\u6700\u7ec8\u6253\u5305\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u66f4\u7075\u6d3b\u7684\u6784\u5efa\u7ba1\u7406<\/strong>: \u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u6027\u5730\u4ece\u4e0d\u540c\u9636\u6bb5\u4e2d\u63d0\u53d6\u6240\u9700\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4ece\u591a\u4e2a\u6784\u5efa\u9636\u6bb5\u4e2d\u7ec4\u5408\u4e0d\u540c\u7684\u6587\u4ef6\u5230\u6700\u7ec8\u955c\u50cf\u4e2d\u3002<\/p>\n<\/li>\n<\/ol>\n<h3><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/h3>\n<p>\u591a\u9636\u6bb5\u6784\u5efa\u7279\u522b\u9002\u5408\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<ul>\n<li>\u9700\u8981\u5728\u6784\u5efa\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u5927\u578b\u7684\u5de5\u5177\u94fe\u6216\u4f9d\u8d56\u9879\uff08\u5982\u7f16\u8bd1\u5668\uff09\uff0c\u4f46\u8fd9\u4e9b\u5de5\u5177\u5728\u8fd0\u884c\u65f6\u4e0d\u9700\u8981\u3002<\/li>\n<li>\u5e0c\u671b\u5c06\u5f00\u53d1\u73af\u5883\u4e0e\u751f\u4ea7\u73af\u5883\u9694\u79bb\uff0c\u786e\u4fdd\u6700\u7ec8\u7684\u955c\u50cf\u53ea\u5305\u542b\u751f\u4ea7\u73af\u5883\u6240\u9700\u7684\u6587\u4ef6\u3002<\/li>\n<li>\u5e0c\u671b\u6784\u5efa\u5c0f\u800c\u9ad8\u6548\u7684\u5bb9\u5668\u955c\u50cf\uff0c\u4f8b\u5982\u7528\u4e8e\u5fae\u670d\u52a1\u7684\u90e8\u7f72\u3002<\/li>\n<\/ul>\n<h3><strong>\u603b\u7ed3<\/strong><\/h3>\n<p>Dockerfile \u4e2d\u7684\u591a\u9636\u6bb5\u6784\u5efa\u901a\u8fc7\u5141\u8bb8\u4f60\u5b9a\u4e49\u591a\u4e2a\u6784\u5efa\u9636\u6bb5\uff0c\u4f7f\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a Dockerfile \u4e2d\u6267\u884c\u5b8c\u6574\u7684\u6784\u5efa\u548c\u6253\u5305\u6d41\u7a0b\uff0c\u540c\u65f6\u53ea\u4fdd\u7559\u6700\u7ec8\u8fd0\u884c\u6240\u9700\u7684\u90e8\u5206\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u4f18\u5316\u955c\u50cf\u5927\u5c0f\u548c\u6784\u5efa\u6548\u7387\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u7cbe\u7b80\u548c\u4f18\u5316\u7684\u751f\u4ea7\u73af\u5883\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210 Dockerfile \u4e2d\u7684\u591a\u9636\u6bb5\u6784\u5efa\uff08Multistage Build\uff09\u662f\u4e00&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6477\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[148],"class_list":["post-6477","post","type-post","status-publish","format-standard","hentry","category-123","tag-docker"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6477"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6477\/revisions"}],"predecessor-version":[{"id":6478,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6477\/revisions\/6478"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}