{"id":6470,"date":"2024-08-16T15:03:23","date_gmt":"2024-08-16T07:03:23","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6470"},"modified":"2024-08-16T15:03:23","modified_gmt":"2024-08-16T07:03:23","slug":"dockerfile-copy-%e5%8f%af%e4%bb%a5%e6%8b%b7%e8%b4%9d%e6%ba%90%e6%96%87%e4%bb%b6%e6%88%96%e5%8f%af%e6%89%a7%e8%a1%8c%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6470","title":{"rendered":"Dockerfile COPY \u53ef\u4ee5\u62f7\u8d1d\u6e90\u6587\u4ef6\u6216\u53ef\u6267\u884c\u6587\u4ef6"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p>\u95ee\uff1a\u4e00\u4e2a go\u9879\u76ee\u7528 docker \u90e8\u7f72\uff0cdocker file \u91cc\u7684 copy\u6307\u4ee4\uff0c\u5373\u53ef\u4ee5\u62f7\u8d1d\u9879\u76ee\u6e90\u6587\u4ef6\uff0c\u5728build image \u65f6\u5728\u5bb9\u5668\u5185\u7f16\u8bd1\uff1b\u4e5f\u53ef\u4ee5\u76f4\u63a5\u62f7\u8d1d\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/p>\n<p>\u662f\u7684\uff0c\u5728 Docker \u4e2d\u6784\u5efa Go \u9879\u76ee\u65f6\uff0c\u4f60\u6709\u4e24\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u4f7f\u7528 <code>COPY<\/code> \u6307\u4ee4\uff1a\u4e00\u79cd\u662f\u5728\u5bb9\u5668\u5185\u7f16\u8bd1\u6e90\u4ee3\u7801\uff0c\u53e6\u4e00\u79cd\u662f\u76f4\u63a5\u62f7\u8d1d\u5df2\u7ecf\u7f16\u8bd1\u597d\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4e24\u79cd\u65b9\u6cd5\u90fd\u6709\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<h3><strong>1. \u5728\u5bb9\u5668\u5185\u7f16\u8bd1\u6e90\u4ee3\u7801<\/strong><\/h3>\n<p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f60\u5c06 Go \u9879\u76ee\u7684\u6e90\u4ee3\u7801\u590d\u5236\u5230\u5bb9\u5668\u4e2d\uff0c\u5e76\u5728\u5bb9\u5668\u5185\u8fdb\u884c\u7f16\u8bd1\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u5f00\u53d1\u6216 CI\/CD \u7ba1\u9053\u4e2d\u3002<\/p>\n<p><strong>Dockerfile \u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-dockerfile\"># \u4f7f\u7528\u5b98\u65b9 Golang \u955c\u50cf\u4f5c\u4e3a\u6784\u5efa\u73af\u5883\nFROM golang:1.20-alpine AS build\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\nCOPY go.mod go.sum .\/\nRUN go mod download\n\n# \u5c06\u9879\u76ee\u7684\u6e90\u4ee3\u7801\u590d\u5236\u5230\u5bb9\u5668\u4e2d\nCOPY . .\n\n# \u7f16\u8bd1\u53ef\u6267\u884c\u6587\u4ef6\nRUN go build -o myapp\n\n# \u4f7f\u7528\u4e00\u4e2a\u66f4\u5c0f\u7684\u955c\u50cf\u4f5c\u4e3a\u8fd0\u884c\u73af\u5883\nFROM alpine:3.18\n\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\nWORKDIR \/app\n\n# \u4ece\u6784\u5efa\u9636\u6bb5\u590d\u5236\u7f16\u8bd1\u597d\u7684\u53ef\u6267\u884c\u6587\u4ef6\nCOPY --from=build \/app\/myapp .\n\n# \u66b4\u9732\u7aef\u53e3\uff08\u6839\u636e\u5e94\u7528\u9700\u6c42\uff09\nEXPOSE 8080\n\n# \u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\nCMD [&quot;.\/myapp&quot;]<\/code><\/pre>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bb9\u5668\u5185\u7f16\u8bd1\u786e\u4fdd\u4e86\u73af\u5883\u7684\u4e00\u81f4\u6027\uff0c\u907f\u514d\u4e86\u4e0d\u540c\u5f00\u53d1\u73af\u5883\u9020\u6210\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/li>\n<li>\u652f\u6301\u5b8c\u6574\u7684\u7f16\u8bd1\u548c\u4f9d\u8d56\u7ba1\u7406\u6d41\u7a0b\uff0c\u9002\u5408\u5728 CI\/CD \u4e2d\u81ea\u52a8\u5316\u6784\u5efa\u3002<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u6784\u5efa\u65f6\u95f4\u8f83\u957f\uff0c\u56e0\u4e3a\u6bcf\u6b21\u90fd\u9700\u8981\u5728\u5bb9\u5668\u4e2d\u6267\u884c\u5b8c\u6574\u7684\u6784\u5efa\u8fc7\u7a0b\u3002<\/li>\n<li>\u6784\u5efa\u73af\u5883\u53ef\u80fd\u8f83\u5927\uff0c\u6bd4\u5982\u5b98\u65b9\u7684 Golang \u955c\u50cf\u8f83\u4e3a\u5e9e\u5927\u3002<\/li>\n<\/ul>\n<h3><strong>2. \u76f4\u63a5\u62f7\u8d1d\u5df2\u7f16\u8bd1\u7684\u53ef\u6267\u884c\u6587\u4ef6<\/strong><\/h3>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5728\u672c\u5730\u73af\u5883\u4e2d\u5148\u7f16\u8bd1\u597d Go \u9879\u76ee\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u5176\u76f4\u63a5\u590d\u5236\u5230 Docker \u955c\u50cf\u4e2d\u3002\u901a\u5e38\u9002\u7528\u4e8e\u751f\u4ea7\u73af\u5883\u6216\u4f60\u5df2\u7ecf\u6709\u9884\u5148\u7f16\u8bd1\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>Dockerfile \u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-dockerfile\"># \u4f7f\u7528\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u57fa\u7840\u955c\u50cf\nFROM alpine:3.18\n\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\nWORKDIR \/app\n\n# \u5c06\u5df2\u7f16\u8bd1\u7684\u53ef\u6267\u884c\u6587\u4ef6\u590d\u5236\u5230\u5bb9\u5668\u4e2d\nCOPY myapp .\n\n# \u66b4\u9732\u7aef\u53e3\uff08\u6839\u636e\u5e94\u7528\u9700\u6c42\uff09\nEXPOSE 8080\n\n# \u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\nCMD [&quot;.\/myapp&quot;]<\/code><\/pre>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u6784\u5efa\u901f\u5ea6\u5feb\uff0c\u56e0\u4e3a\u7701\u53bb\u4e86\u5728\u5bb9\u5668\u5185\u7f16\u8bd1\u7684\u6b65\u9aa4\u3002<\/li>\n<li>\u955c\u50cf\u66f4\u8f7b\u91cf\uff0c\u56e0\u4e3a\u53ea\u5305\u542b\u8fd0\u884c\u65f6\u73af\u5883\u548c\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u9700\u8981\u786e\u4fdd\u672c\u5730\u7f16\u8bd1\u73af\u5883\u548c\u8fd0\u884c\u73af\u5883\u4e00\u81f4\uff0c\u5426\u5219\u53ef\u80fd\u51fa\u73b0\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u8fd0\u884c\uff08\u5982\u4ece macOS \u6784\u5efa\uff0c\u90e8\u7f72\u5230 Linux\uff09\uff0c\u9700\u8981\u4ea4\u53c9\u7f16\u8bd1\u3002<\/li>\n<\/ul>\n<h3><strong>\u603b\u7ed3<\/strong><\/h3>\n<ul>\n<li>\u5982\u679c\u4f60\u66f4\u5173\u6ce8\u6784\u5efa\u7684\u4e00\u81f4\u6027\u548c\u81ea\u52a8\u5316\uff0c\u63a8\u8350\u5728\u5bb9\u5668\u5185\u8fdb\u884c\u7f16\u8bd1\u3002<\/li>\n<li>\u5982\u679c\u4f60\u66f4\u5173\u6ce8\u6784\u5efa\u901f\u5ea6\uff0c\u5e76\u4e14\u53ef\u4ee5\u786e\u4fdd\u73af\u5883\u4e00\u81f4\u6027\uff0c\u76f4\u63a5\u62f7\u8d1d\u5df2\u7f16\u8bd1\u7684\u53ef\u6267\u884c\u6587\u4ef6\u662f\u4e00\u4e2a\u66f4\u7b80\u5355\u7684\u9009\u62e9\u3002<\/li>\n<\/ul>\n<p>\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u6784\u5efa\u6d41\u7a0b\u7684\u4e0d\u540c\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210 \u95ee\uff1a\u4e00\u4e2a go\u9879\u76ee\u7528 docker \u90e8\u7f72\uff0cdocker file \u91cc\u7684 co&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6470\">\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-6470","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\/6470","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=6470"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6470\/revisions"}],"predecessor-version":[{"id":6471,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6470\/revisions\/6471"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}