{"id":63459,"date":"2025-04-29T11:50:42","date_gmt":"2025-04-29T03:50:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63459\/"},"modified":"2025-04-29T11:50:42","modified_gmt":"2025-04-29T03:50:42","slug":"dockerfile%e4%b8%ad%e6%9c%80%e5%b8%b8%e8%a7%81%e7%9a%84%e6%8c%87%e4%bb%a4%e6%98%af%e4%bb%80%e4%b9%88-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63459\/","title":{"rendered":"dockerfile\u4e2d\u6700\u5e38\u89c1\u7684\u6307\u4ee4\u662f\u4ec0\u4e48"},"content":{"rendered":"<blockquote><p>\n  dockerfile \u4e2d\u6700\u5e38\u7528\u7684\u6307\u4ee4\u6709\uff1afrom\uff1a\u521b\u5efa\u65b0\u955c\u50cf\u6216\u6d3e\u751f\u65b0\u955c\u50cfrun\uff1a\u6267\u884c\u547d\u4ee4\uff08\u5b89\u88c5\u8f6f\u4ef6\u3001\u914d\u7f6e\u7cfb\u7edf\uff09copy\uff1a\u590d\u5236\u672c\u5730\u6587\u4ef6\u5230\u955c\u50cfadd\uff1a\u7c7b\u4f3c copy\uff0c\u53ef\u81ea\u52a8\u89e3\u538b\u7f29 tar \u5b58\u6863\u6216\u83b7\u53d6 url \u6587\u4ef6cmd\uff1a\u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u7684\u547d\u4ee4expose\uff1a\u58f0\u660e\u5bb9\u5668\u76d1\u542c\u7aef\u53e3\uff08\u4f46\u4e0d\u516c\u5f00\uff09env\uff1a\u8bbe\u7f6e\u73af\u5883\u53d8\u91cfvolume\uff1a\u6302\u8f7d\u4e3b\u673a\u76ee\u5f55\u6216\u533f\u540d\u5377workdir\uff1a\u8bbe\u7f6e\u5bb9\u5668\u4e2d\u7684\u5de5\u4f5c\u76ee\u5f55entrypoint\uff1a\u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u8981\u6267\u884c\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08\u7c7b\u4f3c cmd\uff0c\u4f46\u4e0d\u53ef\u8986\u76d6\uff09\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202404\/07\/2024040719212189212.jpg\" class=\"aligncenter\" title=\"dockerfile\u4e2d\u6700\u5e38\u89c1\u7684\u6307\u4ee4\u662f\u4ec0\u4e48\u63d2\u56fe\" alt=\"dockerfile\u4e2d\u6700\u5e38\u89c1\u7684\u6307\u4ee4\u662f\u4ec0\u4e48\u63d2\u56fe\" \/><\/p>\n<p><strong>Dockerfile \u4e2d\u6700\u5e38\u89c1\u7684\u6307\u4ee4<\/strong><\/p>\n<p>Dockerfile \u4e2d\u6700\u5e38\u7528\u7684\u6307\u4ee4\u5982\u4e0b\uff1a<\/p>\n<p><strong>1. FROM<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bb9\u5668\u955c\u50cf\u6216\u4ece\u57fa\u7840\u955c\u50cf\u6d3e\u751f\u51fa\u4e00\u4e2a\u65b0\u955c\u50cf\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>FROM ubuntu:20.04<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. RUN<\/strong><\/p>\n<ul>\n<li>\u5728\u5bb9\u5668\u4e2d\u6267\u884c\u547d\u4ee4\u3002\u901a\u5e38\u7528\u4e8e\u5b89\u88c5\u8f6f\u4ef6\u6216\u914d\u7f6e\u7cfb\u7edf\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>RUN apt-get update &amp;&amp; apt-get install -y nginx<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. COPY<\/strong><\/p>\n<ul>\n<li>\u5c06\u672c\u5730\u6587\u4ef6\u6216\u76ee\u5f55\u590d\u5236\u5230\u5bb9\u5668\u955c\u50cf\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>COPY index.html \/usr\/share\/nginx\/html<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. ADD<\/strong><\/p>\n<ul>\n<li>\u4e0e COPY \u7c7b\u4f3c\uff0c\u4f46\u53ef\u4ee5\u81ea\u52a8\u89e3\u538b\u7f29 tar \u5b58\u6863\u6216\u4ece URL \u4e2d\u83b7\u53d6\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>ADD myapp.tar.gz \/usr\/local\/myapp<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. CMD<\/strong><\/p>\n<ul>\n<li>\u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u8981\u6267\u884c\u7684\u547d\u4ee4\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>CMD [\"nginx\", \"-g\", \"daemon off;\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. EXPOSE<\/strong><\/p>\n<ul>\n<li>\u58f0\u660e\u5bb9\u5668\u5c06\u76d1\u542c\u7684\u7aef\u53e3\uff0c\u4f46\u4e0d\u4f1a\u5b9e\u9645\u5728 Docker \u5b88\u62a4\u7a0b\u5e8f\u4e2d\u516c\u5f00\u7aef\u53e3\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>EXPOSE 80<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>7. ENV<\/strong><\/p>\n<ul>\n<li>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>ENV APP_NAME myapp<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>8. VOLUME<\/strong><\/p>\n<ul>\n<li>\u6302\u8f7d\u4e3b\u673a\u76ee\u5f55\u6216\u533f\u540d\u5377\u5230\u5bb9\u5668\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>VOLUME \/var\/log\/myapp<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>9. WORKDIR<\/strong><\/p>\n<ul>\n<li>\u8bbe\u7f6e\u5bb9\u5668\u4e2d\u7684\u5de5\u4f5c\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>WORKDIR \/usr\/local\/myapp<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>10. ENTRYPOINT<\/strong><\/p>\n<ul>\n<li>\u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u8981\u6267\u884c\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u7c7b\u4f3c\u4e8e CMD\uff0c\u4f46\u4e0d\u53ef\u8986\u76d6\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>ENTRYPOINT [\"\/usr\/local\/myapp\/bin\/myapp\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662ffile\u4e2d\u6700\u5e38\u89c1\u7684\u6307\u4ee4\u662f\u4ec0\u4e48\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>dockerfile \u4e2d\u6700\u5e38\u7528\u7684\u6307\u4ee4\u6709\uff1afrom\uff1a\u521b\u5efa\u65b0\u955c\u50cf\u6216\u6d3e\u751f\u65b0\u955c\u50cfrun\uff1a\u6267\u884c\u547d\u4ee4\uff08\u5b89\u88c5\u8f6f\u4ef6\u3001\u914d\u7f6e\u7cfb\u7edf\uff09copy\uff1a\u590d\u5236\u672c\u5730\u6587\u4ef6\u5230\u955c\u50cfadd\uff1a\u7c7b\u4f3c copy\uff0c\u53ef\u81ea\u52a8\u89e3\u538b\u7f29 tar \u5b58\u6863\u6216\u83b7\u53d6 url \u6587\u4ef6cmd\uff1a\u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u7684\u547d\u4ee4expose\uff1a\u58f0\u660e\u5bb9\u5668\u76d1\u542c\u7aef\u53e3\uff08\u4f46\u4e0d\u516c\u5f00\uff09env\uff1a\u8bbe\u7f6e\u73af\u5883\u53d8\u91cfvolume\uff1a\u6302\u8f7d\u4e3b\u673a\u76ee\u5f55\u6216\u533f\u540d\u5377workdir\uff1a\u8bbe\u7f6e\u5bb9\u5668\u4e2d\u7684\u5de5\u4f5c\u76ee\u5f55entrypoint\uff1a\u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u8981\u6267\u884c\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08\u7c7b\u4f3c cmd\uff0c\u4f46\u4e0d\u53ef\u8986\u76d6\uff09 Dockerfile \u4e2d\u6700\u5e38\u89c1\u7684\u6307\u4ee4 Dockerfile \u4e2d\u6700\u5e38\u7528\u7684\u6307\u4ee4\u5982\u4e0b\uff1a 1. FROM \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bb9\u5668\u955c\u50cf\u6216\u4ece\u57fa\u7840\u955c\u50cf\u6d3e\u751f\u51fa\u4e00\u4e2a\u65b0\u955c\u50cf\u3002 \u793a\u4f8b\uff1a FROM ubuntu:20.04 \u767b\u5f55\u540e\u590d\u5236 2. RUN \u5728\u5bb9\u5668\u4e2d\u6267\u884c\u547d\u4ee4\u3002\u901a\u5e38\u7528\u4e8e\u5b89\u88c5\u8f6f\u4ef6\u6216\u914d\u7f6e\u7cfb\u7edf\u3002 \u793a\u4f8b\uff1a RUN apt-get update &amp;&amp; apt-get install -y nginx \u767b\u5f55\u540e\u590d\u5236 3. COPY \u5c06\u672c\u5730\u6587\u4ef6\u6216\u76ee\u5f55\u590d\u5236\u5230\u5bb9\u5668\u955c\u50cf\u4e2d\u3002 \u793a\u4f8b\uff1a COPY index.html \/usr\/share\/nginx\/html \u767b\u5f55\u540e\u590d\u5236 4. ADD \u4e0e COPY \u7c7b\u4f3c\uff0c\u4f46\u53ef\u4ee5\u81ea\u52a8\u89e3\u538b\u7f29 tar \u5b58\u6863\u6216\u4ece URL \u4e2d\u83b7\u53d6\u6587\u4ef6\u3002 \u793a\u4f8b\uff1a ADD myapp.tar.gz \/usr\/local\/myapp \u767b\u5f55\u540e\u590d\u5236 5. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-63459","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=63459"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63459\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}