{"id":6474,"date":"2024-08-16T15:13:58","date_gmt":"2024-08-16T07:13:58","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6474"},"modified":"2024-08-16T15:13:58","modified_gmt":"2024-08-16T07:13:58","slug":"dockerfile-run-%e5%92%8c-cmd-%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6474","title":{"rendered":"Dockerfile  RUN \u548c CMD \u533a\u522b"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p>\u5728 Dockerfile \u4e2d\uff0c<code>RUN<\/code> \u548c <code>CMD<\/code> \u662f\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6307\u4ee4\uff0c\u4f46\u5b83\u4eec\u7684\u4f5c\u7528\u548c\u4f7f\u7528\u573a\u666f\u6709\u6240\u4e0d\u540c\uff1a<\/p>\n<h3><strong>1. RUN \u6307\u4ee4<\/strong><\/h3>\n<ul>\n<li><strong>\u4f5c\u7528<\/strong>: <code>RUN<\/code> \u6307\u4ee4\u4e3b\u8981\u7528\u4e8e<strong>\u6784\u5efa\u955c\u50cf<\/strong>\u65f6\u6267\u884c\u547d\u4ee4\u3002\u901a\u5e38\u7528\u4e8e\u5b89\u88c5\u4f9d\u8d56\u3001\u914d\u7f6e\u73af\u5883\u3001\u6784\u5efa\u9879\u76ee\u7b49\u3002<\/li>\n<li><strong>\u7ed3\u679c<\/strong>: \u6bcf\u4e2a <code>RUN<\/code> \u6307\u4ee4\u90fd\u4f1a\u5728\u955c\u50cf\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c42\uff08Layer\uff09\u3002\u6267\u884c\u7684\u547d\u4ee4\u7684\u7ed3\u679c\u4f1a\u88ab\u4fdd\u5b58\u5728\u6700\u7ec8\u7684\u955c\u50cf\u4e2d\u3002<\/li>\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong>: \u9002\u7528\u4e8e\u9700\u8981\u5728\u955c\u50cf\u6784\u5efa\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\u547d\u4ee4\uff0c\u5982\u5b89\u88c5\u8f6f\u4ef6\u5305\u3001\u7f16\u8bd1\u6e90\u4ee3\u7801\u3001\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u7b49\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-dockerfile\">FROM ubuntu:latest\n\n# \u5b89\u88c5 curl\uff0c\u6784\u5efa\u65f6\u6267\u884c\nRUN apt-get update &amp;&amp; apt-get install -y curl<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>RUN<\/code> \u6307\u4ee4\u5728\u955c\u50cf\u6784\u5efa\u65f6\u6267\u884c\u547d\u4ee4\uff0c\u5e76\u5c06\u5b89\u88c5\u7684\u5185\u5bb9\u4fdd\u5b58\u5230\u955c\u50cf\u4e2d\u3002<\/p>\n<h3><strong>2. CMD \u6307\u4ee4<\/strong><\/h3>\n<ul>\n<li><strong>\u4f5c\u7528<\/strong>: <code>CMD<\/code> \u6307\u4ee4\u4e3b\u8981\u7528\u4e8e\u6307\u5b9a<strong>\u5bb9\u5668\u542f\u52a8\u65f6<\/strong>\u8981\u8fd0\u884c\u7684\u9ed8\u8ba4\u547d\u4ee4\u6216\u53ef\u6267\u884c\u7a0b\u5e8f\u3002<code>CMD<\/code> \u662f\u4e3a\u5bb9\u5668\u8fd0\u884c\u65f6\u8bbe\u8ba1\u7684\uff0c\u800c\u4e0d\u662f\u4e3a\u6784\u5efa\u955c\u50cf\u65f6\u4f7f\u7528\u7684\u3002<\/li>\n<li><strong>\u7ed3\u679c<\/strong>: <code>CMD<\/code> \u53ea\u6307\u5b9a\u4e86\u5bb9\u5668\u542f\u52a8\u65f6\u7684\u9ed8\u8ba4\u547d\u4ee4\uff0c\u5982\u679c\u7528\u6237\u5728\u8fd0\u884c\u5bb9\u5668\u65f6\u63d0\u4f9b\u4e86\u5176\u4ed6\u547d\u4ee4\uff0c\u90a3\u4e48 <code>CMD<\/code> \u4f1a\u88ab\u8986\u76d6\u3002<\/li>\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong>: \u9002\u7528\u4e8e\u5b9a\u4e49\u5bb9\u5668\u542f\u52a8\u65f6\u7684\u9ed8\u8ba4\u884c\u4e3a\uff0c\u5982\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u6216\u670d\u52a1\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-dockerfile\">FROM ubuntu:latest\n\n# \u8fd0\u884c\u65f6\u7684\u9ed8\u8ba4\u547d\u4ee4\nCMD [&quot;echo&quot;, &quot;Hello, World!&quot;]<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4f60\u8fd0\u884c <code>docker run &lt;image&gt;<\/code>\uff0c\u5bb9\u5668\u4f1a\u8f93\u51fa &quot;Hello, World!&quot;\u3002<\/p>\n<h3><strong>3. RUN \u548c CMD \u7684\u533a\u522b\u603b\u7ed3<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u6267\u884c\u65f6\u673a<\/strong>:<\/p>\n<ul>\n<li><code>RUN<\/code> \u5728<strong>\u6784\u5efa\u955c\u50cf<\/strong>\u65f6\u6267\u884c\u3002<\/li>\n<li><code>CMD<\/code> \u5728<strong>\u5bb9\u5668\u542f\u52a8<\/strong>\u65f6\u6267\u884c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7528\u9014<\/strong>:<\/p>\n<ul>\n<li><code>RUN<\/code> \u7528\u4e8e\u5b89\u88c5\u8f6f\u4ef6\u3001\u914d\u7f6e\u73af\u5883\u7b49\uff0c\u751f\u6210\u955c\u50cf\u65f6\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c42\u3002<\/li>\n<li><code>CMD<\/code> \u7528\u4e8e\u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u7684\u9ed8\u8ba4\u547d\u4ee4\u6216\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u53ef\u8986\u76d6\u6027<\/strong>:<\/p>\n<ul>\n<li><code>RUN<\/code> \u662f\u4e0d\u53ef\u8986\u76d6\u7684\uff0c\u56e0\u4e3a\u5b83\u662f\u6784\u5efa\u955c\u50cf\u7684\u4e00\u90e8\u5206\u3002<\/li>\n<li><code>CMD<\/code> \u662f\u53ef\u4ee5\u88ab\u8986\u76d6\u7684\u3002\u5982\u679c\u5728\u8fd0\u884c\u5bb9\u5668\u65f6\u6307\u5b9a\u4e86\u5176\u4ed6\u547d\u4ee4\uff0c<code>CMD<\/code> \u4f1a\u88ab\u5ffd\u7565\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><strong>4. CMD \u7684\u6269\u5c55\uff1aENTRYPOINT<\/strong><\/h3>\n<ul>\n<li>\u53e6\u5916\u8fd8\u6709\u4e00\u4e2a\u7c7b\u4f3c <code>CMD<\/code> \u7684\u6307\u4ee4\u662f <code>ENTRYPOINT<\/code>\uff0c\u5b83\u7528\u4e8e\u5f3a\u5236\u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u6267\u884c\u7684\u547d\u4ee4\u3002\u4e0e <code>CMD<\/code> \u4e0d\u540c\u7684\u662f\uff0c<code>ENTRYPOINT<\/code> \u901a\u5e38\u4e0d\u4f1a\u88ab\u8986\u76d6\uff0c\u9664\u975e\u4f7f\u7528 <code>docker run --entrypoint<\/code> \u663e\u5f0f\u8986\u76d6\u5b83\u3002<\/li>\n<\/ul>\n<h3><strong>\u603b\u7ed3<\/strong><\/h3>\n<ul>\n<li>\u4f7f\u7528 <code>RUN<\/code> \u6307\u4ee4\u8fdb\u884c\u6784\u5efa\u65f6\u7684\u547d\u4ee4\u6267\u884c\uff0c\u6784\u5efa\u5b8c\u6210\u540e\u7ed3\u679c\u4f1a\u4fdd\u5b58\u5728\u955c\u50cf\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528 <code>CMD<\/code> \u6307\u4ee4\u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u7684\u9ed8\u8ba4\u884c\u4e3a\uff0c\u4f46\u53ef\u4ee5\u5728\u8fd0\u884c\u5bb9\u5668\u65f6\u8986\u76d6\u8fd9\u4e2a\u9ed8\u8ba4\u884c\u4e3a\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210 \u5728 Dockerfile \u4e2d\uff0cRUN \u548c CMD \u662f\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6307\u4ee4\uff0c\u4f46\u5b83&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6474\">\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-6474","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\/6474","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=6474"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6474\/revisions"}],"predecessor-version":[{"id":6475,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6474\/revisions\/6475"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}