{"id":418,"date":"2025-11-18T14:55:29","date_gmt":"2025-11-18T06:55:29","guid":{"rendered":"http:\/\/43.139.99.192\/?p=418"},"modified":"2025-11-18T15:56:40","modified_gmt":"2025-11-18T07:56:40","slug":"%e9%80%9a%e4%bf%97%e6%98%93%e6%87%82%e5%9c%b0%e5%ad%a6%e4%b9%a0%e4%b8%80%e4%b8%8bdocker","status":"publish","type":"post","link":"http:\/\/43.139.99.192\/index.php\/2025\/11\/18\/%e9%80%9a%e4%bf%97%e6%98%93%e6%87%82%e5%9c%b0%e5%ad%a6%e4%b9%a0%e4%b8%80%e4%b8%8bdocker\/","title":{"rendered":"\u901a\u4fd7\u6613\u61c2\u5730\u5b66\u4e60\u4e00\u4e0bdocker"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Dockerfile\u548c\u81ea\u5b9a\u4e49\u955c\u50cf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u592a\u597d\u4e86\uff01\u6211\u4eec\u7ee7\u7eed\u7528\u201c\u7269\u6d41\u7cfb\u7edf\u201d\u7684\u6bd4\u55bb\uff0c\u6765\u6df1\u5165\u8bb2\u89e3Dockerfile\u548c\u81ea\u5b9a\u4e49\u955c\u50cf\u7684\u77e5\u8bc6\u3002\u8fd9\u5c31\u50cf\u662f\u5b66\u4e60\u5982\u4f55<strong>\u81ea\u5df1\u8bbe\u8ba1\u6a21\u5177<\/strong>\uff0c\u800c\u4e0d\u662f\u603b\u662f\u7528\u522b\u4eba\u505a\u597d\u7684\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Dockerfile\u662f\u4ec0\u4e48\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u901a\u4fd7\u89e3\u91ca\uff1aDockerfile\u5c31\u50cf\u662f\u5236\u4f5c\u6a21\u5177\u7684\u201c\u914d\u65b9\u201d\u6216\u201c\u65bd\u5de5\u56fe\u7eb8\u201d\u3002<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e4b\u524d\u6211\u4eec\u90fd\u662f\u7528\u522b\u4eba\u505a\u597d\u7684\u6a21\u5177\uff08\u955c\u50cf\uff09\uff0c\u6bd4\u5982<code>nginx<\/code>\u3001<code>ubuntu<\/code>\u3002<\/li>\n\n\n\n<li>\u73b0\u5728\uff0c\u6211\u4eec\u60f3\u505a\u4e00\u4e2a<strong>\u4e13\u5c5e\u6a21\u5177<\/strong>\uff0c\u6bd4\u5982\u201c\u4e00\u4e2a\u5305\u542b\u6211\u7684Python\u4ee3\u7801\u3001\u4f9d\u8d56\u5e93\u548c\u914d\u7f6e\u6587\u4ef6\u7684\u4e13\u5c5e\u670d\u52a1\u5668\u6a21\u5177\u201d\u3002<\/li>\n\n\n\n<li>Dockerfile\u5c31\u662f\u8fd9\u4e2a\u6a21\u5177\u7684<strong>\u8be6\u7ec6\u5236\u4f5c\u8bf4\u660e\u4e66<\/strong>\uff0c\u91cc\u9762\u4e00\u6b65\u4e00\u6b65\u5730\u5199\u7740\u8be5\u600e\u4e48\u5236\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Dockerfile\u6838\u5fc3\u8bed\u6cd5\u8be6\u89e3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8ba9\u6211\u4eec\u6765\u770b\u8fd9\u4efd\u201c\u8bf4\u660e\u4e66\u201d\u91cc\u6700\u91cd\u8981\u7684\u51e0\u4e2a\u6307\u4ee4\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. <code>FROM<\/code>&#8211; \u9009\u62e9\u57fa\u7840\u6a21\u5177<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u4f60\u8981\u505a\u86cb\u7cd5\uff0c\u4e0d\u4f1a\u4ece\u79cd\u5c0f\u9ea6\u5f00\u59cb\u3002\u4f60\u4f1a\u5148<strong>\u4e70\u4e00\u4e2a\u73b0\u6210\u7684\u86cb\u7cd5\u80da<\/strong>\u4f5c\u4e3a\u57fa\u7840\u3002<code>FROM<\/code>\u5c31\u662f\u6307\u5b9a\u8fd9\u4e2a\u201c\u57fa\u7840\u86cb\u7cd5\u80da\u201d\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5\uff1a<\/strong>\u200b <code>FROM &lt;\u57fa\u7840\u955c\u50cf&gt;<\/code><\/li>\n\n\n\n<li><strong>\u4f8b\u5b50\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>FROM ubuntu:20.04<\/code>\uff08\u57fa\u4e8eUbuntu\u7cfb\u7edf\u8fd9\u4e2a\u6a21\u5177\u5f00\u59cb\u52a0\u5de5\uff09<\/li>\n\n\n\n<li><code>FROM python:3.9<\/code>\uff08\u57fa\u4e8e\u4e00\u4e2a\u5df2\u7ecf\u88c5\u597dPython\u7684\u6a21\u5177\u5f00\u59cb\u52a0\u5de5\uff0c\u66f4\u7701\u4e8b\uff01\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u91cd\u8981\uff1a<\/strong>\u200b <strong>\u6bcf\u4e2aDockerfile\u5fc5\u987b\u4ee5<code>FROM<\/code>\u6307\u4ee4\u5f00\u5934<\/strong>\u3002\u4f60\u5fc5\u987b\u57fa\u4e8e\u4e00\u4e2a\u5df2\u6709\u7684\u955c\u50cf\u5f00\u59cb\u6784\u5efa\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. <code>RUN<\/code>&#8211; \u5728\u5236\u4f5c\u8fc7\u7a0b\u4e2d\u6267\u884c\u547d\u4ee4<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u5728\u86cb\u7cd5\u80da\u4e0a<strong>\u8fdb\u884c\u64cd\u4f5c\u6b65\u9aa4<\/strong>\u3002\u6bd4\u5982\uff1a\u201cRUN \u6d82\u62b9\u5976\u6cb9\u201d\u3001\u201cRUN \u653e\u4e0a\u8349\u8393\u201d\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5\uff1a<\/strong>\u200b <code>RUN &lt;\u547d\u4ee4&gt;<\/code><\/li>\n\n\n\n<li><strong>\u4f8b\u5b50\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>RUN apt-get update &amp;&amp; apt-get install -y python3<\/code>\uff08\u5728\u57fa\u4e8eUbuntu\u7684\u6a21\u5177\u91cc\u5b89\u88c5Python3\uff09<\/li>\n\n\n\n<li><code>RUN pip install -r requirements.txt<\/code>\uff08\u5b89\u88c5Python\u4f9d\u8d56\u5305\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4f5c\u7528\uff1a<\/strong>\u200b \u6267\u884c\u4efb\u4f55\u9700\u8981\u5728\u5bb9\u5668\u5185\u90e8\u5b8c\u6210\u7684\u8bbe\u7f6e\u548c\u5b89\u88c5\u547d\u4ee4\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. <code>COPY<\/code>\/ <code>ADD<\/code>&#8211; \u628a\u6587\u4ef6\u653e\u8fdb\u6a21\u5177\u91cc<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u628a\u4f60\u7684<strong>\u72ec\u5bb6\u79d8\u65b9\uff08\u4ee3\u7801\u3001\u914d\u7f6e\u6587\u4ef6\uff09<\/strong>\u200b \u653e\u8fdb\u86cb\u7cd5\u91cc\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5\uff1a<\/strong>\u200b <code>COPY &lt;\u6e90\u8def\u5f84&gt; &lt;\u76ee\u6807\u8def\u5f84&gt;<\/code><\/li>\n\n\n\n<li><strong>\u4f8b\u5b50\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>COPY . \/app<\/code>\uff08\u5c06\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\uff0c\u590d\u5236\u5230\u6a21\u5177\u5185\u7684<code>\/app<\/code>\u76ee\u5f55\u4e0b\uff09<\/li>\n\n\n\n<li><code>COPY requirements.txt \/code\/requirements.txt<\/code>\uff08\u53ea\u590d\u5236\u4f9d\u8d56\u5217\u8868\u6587\u4ef6\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>COPY<\/code>vs <code>ADD<\/code>\uff1a<\/strong>\u200b \u5bf9\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u7528<code>COPY<\/code>\u5c31\u591f\u4e86\u3002<code>ADD<\/code>\u6709\u4e00\u4e9b\u989d\u5916\u529f\u80fd\uff08\u5982\u81ea\u52a8\u89e3\u538b\uff09\uff0c\u4f46\u539f\u5219\u662f\uff1a<strong>\u9664\u975e\u4f60\u9700\u8981\u89e3\u538b\uff0c\u5426\u5219\u4e00\u5f8b\u7528<code>COPY<\/code><\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. <code>WORKDIR<\/code>&#8211; \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u6307\u5b9a\u4e00\u4e2a<strong>\u5de5\u4f5c\u53f0<\/strong>\u3002\u4e4b\u540e\u7684\u6240\u6709\u64cd\u4f5c\uff08\u6bd4\u5982<code>RUN<\/code>\uff0c <code>COPY<\/code>\uff09\u9ed8\u8ba4\u90fd\u5728\u8fd9\u4e2a\u5de5\u4f5c\u53f0\u4e0a\u8fdb\u884c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5\uff1a<\/strong>\u200b <code>WORKDIR &lt;\u8def\u5f84&gt;<\/code><\/li>\n\n\n\n<li><strong>\u4f8b\u5b50\uff1a<\/strong>\u200b <code>WORKDIR \/app<\/code><\/li>\n\n\n\n<li><strong>\u597d\u5904\uff1a<\/strong>\u200b \u907f\u514d\u4e86\u5728\u547d\u4ee4\u4e2d\u5199\u5f88\u957f\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u8ba9\u201c\u8bf4\u660e\u4e66\u201d\u66f4\u6e05\u6670\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5. <code>EXPOSE<\/code>&#8211; \u58f0\u660e\u7aef\u53e3<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u5728\u6a21\u5177\u7684\u8bbe\u8ba1\u56fe\u4e0a<strong>\u6807\u6ce8\u51fa\u201c\u8fd9\u91cc\u6709\u4e2a\u670d\u52a1\u7a97\u53e3\u201d<\/strong>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5\uff1a<\/strong>\u200b <code>EXPOSE &lt;\u7aef\u53e3\u53f7&gt;<\/code><\/li>\n\n\n\n<li><strong>\u4f8b\u5b50\uff1a<\/strong>\u200b <code>EXPOSE 80<\/code><\/li>\n\n\n\n<li><strong>\u6ce8\u610f\uff1a<\/strong>\u200b \u8fd9\u53ea\u662f\u4e00\u4e2a<strong>\u58f0\u660e<\/strong>\uff0c\u65b9\u4fbf\u522b\u4eba\u770b\u6587\u6863\u77e5\u9053\u8fd9\u4e2a\u955c\u50cf\u7528\u4ec0\u4e48\u7aef\u53e3\u3002<strong>\u5b83\u5e76\u4e0d\u4f1a\u81ea\u52a8\u5b8c\u6210\u7aef\u53e3\u6620\u5c04\uff01<\/strong>\u200b \u771f\u6b63\u7684\u7aef\u53e3\u6620\u5c04\u8fd8\u662f\u5728\u8fd0\u884c\u5bb9\u5668\u65f6\u7528 <code>-p<\/code>\u53c2\u6570\u6765\u505a\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">6. <code>CMD<\/code>&#8211; \u5bb9\u5668\u542f\u52a8\u65f6\u9ed8\u8ba4\u8981\u8fd0\u884c\u7684\u547d\u4ee4<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u7ed9\u8fd9\u4e2a\u8d27\u7bb1\uff08\u5bb9\u5668\uff09\u8d34\u4e0a<strong>\u4f7f\u7528\u8bf4\u660e\u4e66\u7684\u7b2c\u4e00\u6761<\/strong>\uff1a\u201c\u542f\u52a8\u540e\uff0c\u8bf7\u81ea\u52a8\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u201d\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5\uff1a<\/strong>\u200b <code>CMD [\u201c\u53ef\u6267\u884c\u6587\u4ef6\u201d, \u201c\u53c2\u65701\u201d, \u201c\u53c2\u65702\u201d]<\/code>\uff08\u63a8\u8350\u8fd9\u79cd\u683c\u5f0f\uff0c\u79f0\u4e3a<em>exec\u683c\u5f0f<\/em>\uff09<\/li>\n\n\n\n<li><strong>\u4f8b\u5b50\uff1a<\/strong>\u200b <code>CMD [\u201cpython\u201d, \u201capp.py\u201d]<\/code><\/li>\n\n\n\n<li><strong>\u91cd\u8981\uff1a<\/strong>\u200b Dockerfile\u4e2d\u53ea\u80fd\u6709\u4e00\u6761<code>CMD<\/code>\u6307\u4ee4\uff0c\u5982\u679c\u6709\u591a\u6761\uff0c\u5219\u53ea\u6709\u6700\u540e\u4e00\u6761\u751f\u6548\u3002<\/li>\n\n\n\n<li><strong><code>RUN<\/code>vs <code>CMD<\/code>\u7684\u533a\u522b\uff08\u6838\u5fc3\u6982\u5ff5\uff01\uff09\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>RUN<\/code>\u662f<strong>\u6784\u5efa\u955c\u50cf\uff08\u5236\u9020\u6a21\u5177\uff09\u65f6<\/strong>\u6267\u884c\u7684\u547d\u4ee4\uff0c\u6bd4\u5982\u5b89\u88c5\u8f6f\u4ef6\u3002<\/li>\n\n\n\n<li><code>CMD<\/code>\u662f<strong>\u542f\u52a8\u5bb9\u5668\uff08\u6fc0\u6d3b\u8d27\u7bb1\uff09\u65f6<\/strong>\u6267\u884c\u7684\u547d\u4ee4\uff0c\u6bd4\u5982\u542f\u52a8\u670d\u52a1\u5668\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728<code>docker run<\/code>\u65f6\u6307\u5b9a\u65b0\u547d\u4ee4\u6765\u8986\u76d6<code>CMD<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">7. <code>ENV<\/code>&#8211; \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u5728\u6a21\u5177\u91cc\u9884\u8bbe\u4e00\u4e9b<strong>\u5168\u5c40\u914d\u7f6e\u53c2\u6570<\/strong>\uff0c\u6bd4\u5982\u201c\u751c\u5ea6=\u9ad8\u201d\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5\uff1a<\/strong>\u200b <code>ENV &lt;key&gt; &lt;value&gt;<\/code>\u6216 <code>ENV &lt;key1&gt;=&lt;value1&gt; &lt;key2&gt;=&lt;value2&gt;<\/code><\/li>\n\n\n\n<li><strong>\u4f8b\u5b50\uff1a<\/strong>\u200b <code>ENV MODE=production<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u4e2a\u5b8c\u6574\u7684Dockerfile\u4f8b\u5b50<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684Python Flask\u5e94\u7528\uff0c\u9879\u76ee\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my-app\/\n\u251c\u2500\u2500 app.py          # \u4f60\u7684Python\u4ee3\u7801\n\u251c\u2500\u2500 requirements.txt # \u4f9d\u8d56\u5217\u8868\n\u2514\u2500\u2500 Dockerfile      # \u6211\u4eec\u6b63\u5728\u5199\u7684\u201c\u6a21\u5177\u8bf4\u660e\u4e66\u201d<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Dockerfile \u5185\u5bb9\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7b2c\u4e00\u884c\uff1a\u9009\u62e9\u4e00\u4e2a\u5df2\u7ecf\u88c5\u597dPython\u7684\u57fa\u7840\u6a21\u5177\uff0c\u7701\u53bb\u81ea\u5df1\u5b89\u88c5Python\u7684\u9ebb\u70e6\nFROM python:3.9-slim\n\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u4e3a \/app\uff0c\u540e\u7eed\u64cd\u4f5c\u90fd\u5728\u8fd9\u91cc\u8fdb\u884c\nWORKDIR \/app\n\n# \u5148\u628a\u4f9d\u8d56\u5217\u8868\u6587\u4ef6\u590d\u5236\u5230\u5de5\u4f5c\u76ee\u5f55\n# \uff08\u8fd9\u662f\u4e00\u4e2a\u4f18\u5316\u6280\u5de7\uff0c\u5229\u7528Docker\u7684\u7f13\u5b58\u5c42\uff0c\u5982\u679crequirements.txt\u6ca1\u53d8\uff0c\u5219\u4e0d\u9700\u8981\u91cd\u65b0pip install\uff09\nCOPY requirements.txt .\n\n# \u5728\u6784\u5efa\u955c\u50cf\u65f6\u6267\u884c\u547d\u4ee4\uff1a\u5b89\u88c5\u4f9d\u8d56\u5305\nRUN pip install --no-cache-dir -r requirements.txt\n\n# \u518d\u628a\u5f53\u524d\u76ee\u5f55\u7684\u6240\u6709\u6587\u4ef6\uff08\u4f60\u7684\u4ee3\u7801\uff09\u90fd\u590d\u5236\u5230\u5de5\u4f5c\u76ee\u5f55\nCOPY . .\n\n# \u58f0\u660e\u5bb9\u5668\u8fd0\u884c\u65f6\u63d0\u4f9b\u670d\u52a1\u7684\u7aef\u53e3\u662f5000\uff08\u53ea\u662f\u58f0\u660e\uff0c\u4e0d\u662f\u6620\u5c04\uff09\nEXPOSE 5000\n\n# \u8bbe\u7f6e\u4e00\u4e2a\u73af\u5883\u53d8\u91cf\nENV FLASK_APP=app.py\n\n# \u6307\u5b9a\u5bb9\u5668\u542f\u52a8\u65f6\u9ed8\u8ba4\u8fd0\u884c\u7684\u547d\u4ee4\uff1a\u542f\u52a8Flask\u5e94\u7528\nCMD &#91;\"flask\", \"run\", \"--host\", \"0.0.0.0\"]<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/43.139.99.192\/wp-content\/uploads\/2025\/11\/image-1-1024x463.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"463\" data-original=\"http:\/\/43.139.99.192\/wp-content\/uploads\/2025\/11\/image-1-1024x463.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-422\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u201c\u65bd\u5de5\u201d\uff1a\u4f7f\u7528Dockerfile\u6784\u5efa\u81ea\u5b9a\u4e49\u955c\u50cf<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\uff0c\u6211\u4eec\u6709\u4e86\u201c\u65bd\u5de5\u56fe\u7eb8\u201d\uff08Dockerfile\uff09\uff0c\u53ef\u4ee5\u5f00\u59cb\u5236\u9020\u6211\u4eec\u81ea\u5df1\u7684\u6a21\u5177\u4e86\uff01 <strong>\u547d\u4ee4\uff1a<code>docker build -t &lt;\u4f60\u7684\u955c\u50cf\u540d&gt; &lt;\u4e0a\u4e0b\u6587\u8def\u5f84&gt;<\/code><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>-t<\/code>\uff1a<\/strong>\u200b \u7ed9\u6784\u5efa\u6210\u529f\u7684\u955c\u50cf\u6253\u4e00\u4e2a\u6807\u7b7e\uff08\u8d77\u4e2a\u540d\u5b57\uff09\uff0c\u683c\u5f0f\u901a\u5e38\u662f<code>\u7528\u6237\u540d\/\u955c\u50cf\u540d:\u6807\u7b7e<\/code>\uff0c\u5982\u679c\u4e0d\u4e0a\u4f20\u53ef\u4ee5\u968f\u4fbf\u8d77\uff0c\u6bd4\u5982<code>my-python-app:v1<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>.<\/code>\uff1a<\/strong>\u200b \u8fd9\u4e2a\u70b9\u4ee3\u8868<strong>\u6784\u5efa\u4e0a\u4e0b\u6587<\/strong>\u7684\u8def\u5f84\u3002Docker\u5f15\u64ce\u4f1a\u628a\u5f53\u524d\u76ee\u5f55\uff08\u5305\u62ec\u5b50\u76ee\u5f55\uff09\u7684\u6240\u6709\u6587\u4ef6\u6253\u5305\u53d1\u9001\u7ed9Docker\u5b88\u62a4\u8fdb\u7a0b\u3002\u6240\u4ee5<strong>\u4e0d\u8981\u628a\u65e0\u5173\u7684\u5927\u6587\u4ef6\u653e\u5728Dockerfile\u6240\u5728\u76ee\u5f55<\/strong>\u3002\u53ef\u4ee5\u7528 <code>.dockerignore<\/code>\u6587\u4ef6\u6765\u5ffd\u7565\u4e0d\u9700\u8981\u7684\u6587\u4ef6\uff08\u7c7b\u4f3c <code>.gitignore<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b9e\u9645\u64cd\u4f5c\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728\u7ec8\u7aef\u4e2d\uff0c\u8fdb\u5165\u5305\u542bDockerfile\u7684\u76ee\u5f55\uff08<code>my-app\/<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u6267\u884c\u547d\u4ee4\uff1a <code>docker build -t my-python-app:latest .<\/code><\/li>\n\n\n\n<li>\u4f60\u4f1a\u770b\u5230Docker\u4e00\u6b65\u6b65\u6267\u884cDockerfile\u4e2d\u7684\u6307\u4ee4\uff0c\u6bcf\u4e00\u884c\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u4e34\u65f6\u7684\u201c\u955c\u50cf\u5c42\u201d\u3002<\/li>\n\n\n\n<li>\u6784\u5efa\u6210\u529f\u540e\uff0c\u7528 <code>docker images<\/code>\u67e5\u770b\uff0c\u4f60\u5c31\u80fd\u770b\u5230\u5d2d\u65b0\u7684 <code>my-python-app<\/code>\u955c\u50cf\u4e86\uff01<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8fd0\u884c\u4f60\u7684\u81ea\u5b9a\u4e49\u955c\u50cf\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d -p 5000:5000 my-python-app:latest<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\uff0c\u4f60\u7684\u81ea\u5b9a\u4e49\u5e94\u7528\u5c31\u5728\u5bb9\u5668\u4e2d\u8fd0\u884c\u8d77\u6765\u4e86\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6838\u5fc3\u601d\u60f3\u603b\u7ed3<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5206\u5c42\u6784\u5efa\uff1a<\/strong>\u200b Dockerfile\u7684\u6bcf\u6761\u6307\u4ee4\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u955c\u50cf\u5c42\u3002\u5982\u679cDockerfile\u6ca1\u53d8\uff0c\u91cd\u5efa\u65f6\u4f1a\u76f4\u63a5\u4f7f\u7528\u7f13\u5b58\uff0c\u6781\u5927\u52a0\u5feb\u6784\u5efa\u901f\u5ea6\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6211\u4eec\u628a\u53d8\u5316\u9891\u7387\u4f4e\u7684\u64cd\u4f5c\uff08\u5982<code>COPY requirements.txt<\/code>\u548c<code>RUN pip install<\/code>\uff09\u653e\u5728\u524d\u9762\uff0c\u53d8\u5316\u9891\u7387\u9ad8\u7684\u64cd\u4f5c\uff08\u5982<code>COPY . .<\/code>\u590d\u5236\u4ee3\u7801\uff09\u653e\u5728\u540e\u9762\u3002<\/li>\n\n\n\n<li><strong>\u4e00\u4e2a\u5bb9\u5668\u4e00\u4e2a\u8fdb\u7a0b\uff1a<\/strong>\u200b \u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u5bb9\u5668\u53ea\u8fd0\u884c\u4e00\u4e2a\u4e3b\u8fdb\u7a0b\uff08\u7531<code>CMD<\/code>\u6307\u5b9a\uff09\u3002\u8fd9\u7b26\u5408Docker\u7684\u201c\u5355\u4e00\u804c\u8d23\u201d\u539f\u5219\u3002<\/li>\n\n\n\n<li><strong>\u955c\u50cf\u8981\u5c3d\u53ef\u80fd\u5c0f\uff1a<\/strong>\u200b \u9009\u62e9\u66f4\u5c0f\u7684\u57fa\u7840\u955c\u50cf\uff08\u5982<code>-slim<\/code>\u7248\u672c\uff09\uff0c\u53ca\u65f6\u6e05\u7406\u4e0d\u5fc5\u8981\u7684\u7f13\u5b58\u6587\u4ef6\uff0c\u8ba9\u4f60\u7684\u201c\u6a21\u5177\u201d\u66f4\u8f7b\u4fbf\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\uff0c\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u4ece\u201c\u4f7f\u7528\u8005\u201d\u5230\u201c\u521b\u9020\u8005\u201d\u7684\u5173\u952e\u4e00\u6b65\uff01\u8bd5\u7740\u4e3a\u4f60\u81ea\u5df1\u7684\u9879\u76ee\u5199\u4e00\u4e2aDockerfile\u5427\uff0c\u8fd9\u662f\u5b66\u4e60Docker\u6700\u6709\u6210\u5c31\u611f\u7684\u90e8\u5206\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u597d\u7684\uff01\u6211\u4eec\u7ee7\u7eed\u7528\u201c\u7269\u6d41\u7cfb\u7edf\u201d\u7684\u6bd4\u55bb\uff0c\u6765\u8bb2\u89e3Docker\u5bb9\u5668\u7f51\u7edc\u8fd9\u4e2a\u65e2\u91cd\u8981\u53c8\u6709\u8da3\u7684\u8bdd\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u9700\u8981\u5bb9\u5668\u7f51\u7edc\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u60f3\u8c61\u4e00\u4e0b\u6211\u4eec\u7684\u7269\u6d41\u4e2d\u5fc3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u4e2a<strong>\u5bb9\u5668<\/strong>\u90fd\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u201c\u8d27\u7bb1\u201d<\/li>\n\n\n\n<li>\u8fd9\u4e9b\u8d27\u7bb1\u91cc\u8fd0\u884c\u7740\u4e0d\u540c\u7684\u670d\u52a1\uff1a\u6709\u7684\u88c5\u6570\u636e\u5e93\uff0c\u6709\u7684\u88c5\u7f51\u7ad9\u524d\u7aef\uff0c\u6709\u7684\u88c5\u540e\u7aefAPI<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u95ee\u9898\u6765\u4e86\uff1a<\/strong>\u200b \u5982\u679c\u8fd9\u4e9b\u8d27\u7bb1\u90fd\u5b8c\u5168\u9694\u79bb\uff0c\u4e92\u76f8\u4e0d\u77e5\u9053\u5bf9\u65b9\u7684\u5b58\u5728\uff0c\u90a3\u7f51\u7ad9\u524d\u7aef\u600e\u4e48\u8bbf\u95ee\u540e\u7aefAPI\uff1f\u540e\u7aef\u53c8\u600e\u4e48\u8fde\u63a5\u6570\u636e\u5e93\uff1f <strong>\u7b54\u6848\u5c31\u662f\uff1a\u6211\u4eec\u9700\u8981\u5728\u8d27\u7bb1\u4e4b\u95f4\u4fee\u5efa\u201c\u9053\u8def\u201d\uff0c\u8ba9\u5b83\u4eec\u80fd\u591f\u76f8\u4e92\u901a\u4fe1\uff01<\/strong>\u200b \u8fd9\u5c31\u662fDocker\u7f51\u7edc\u7684\u4f5c\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Docker\u7f51\u7edc\u7684\u4e09\u79cd\u57fa\u672c\u201c\u9053\u8def\u7cfb\u7edf\u201d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Docker\u63d0\u4f9b\u4e86\u51e0\u79cd\u4e0d\u540c\u7684\u7f51\u7edc\u6a21\u5f0f\uff0c\u5c31\u50cf\u4e0d\u540c\u7ea7\u522b\u7684\u9053\u8def\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u6865\u63a5\u7f51\u7edc &#8211; \u201c\u7269\u6d41\u4e2d\u5fc3\u5185\u90e8\u9053\u8def\u201d\uff08\u9ed8\u8ba4\u6a21\u5f0f\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u8fd9\u662fDocker\u81ea\u5df1\u521b\u5efa\u7684\u4e00\u4e2a<strong>\u865a\u62df\u7684\u5185\u90e8\u9053\u8def\u7cfb\u7edf<\/strong>\u3002\u6240\u6709\u8fde\u63a5\u5230\u8fd9\u4e2a\u7f51\u7edc\u7684\u5bb9\u5668\uff0c\u5c31\u50cf\u5728\u540c\u4e00\u4e2a\u7269\u6d41\u56ed\u533a\u91cc\uff0c\u53ef\u4ee5\u901a\u8fc7\u5185\u90e8\u9053\u8def\u76f8\u4e92\u8bbf\u95ee\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7279\u70b9\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u5bb9\u5668\u4e4b\u95f4\u53ef\u4ee5\u4e92\u76f8\u901a\u4fe1<\/li>\n\n\n\n<li>\u6bcf\u4e2a\u5bb9\u5668\u6709\u81ea\u5df1\u72ec\u7acb\u7684IP\u5730\u5740\uff08\u6bd4\u5982172.17.0.2, 172.17.0.3\uff09<\/li>\n\n\n\n<li>\u4e0e\u5916\u754c\u9694\u79bb\uff0c\u5b89\u5168\u6027\u8f83\u597d<\/li>\n\n\n\n<li><strong>\u8fd9\u662f\u9ed8\u8ba4\u6a21\u5f0f<\/strong>\uff0c\u5982\u679c\u4f60\u4e0d\u6307\u5b9a\u7f51\u7edc\uff0c\u5bb9\u5668\u5c31\u4f1a\u8fde\u63a5\u5230\u9ed8\u8ba4\u7684\u6865\u63a5\u7f51\u7edc<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5b9e\u9645\u4f53\u9a8c\uff1a<\/strong> <code># \u8fd0\u884c\u4e24\u4e2a\u5bb9\u5668\uff0c\u5b83\u4eec\u4f1a\u81ea\u52a8\u8fde\u63a5\u5230\u9ed8\u8ba4\u7684\u6865\u63a5\u7f51\u7edc docker run -d --name web nginx docker run -d --name database mysql # \u8fdb\u5165web\u5bb9\u5668\uff0c\u5c1d\u8bd5ping\u6570\u636e\u5e93\u5bb9\u5668 docker exec -it web \/bin\/bash # \u5728\u5bb9\u5668\u5185\u6267\u884c\uff1a ping database # \u8fd9\u4f1a\u5931\u8d25\uff01\u56e0\u4e3a\u9ed8\u8ba4\u6865\u63a5\u7f51\u7edc\u4e0d\u652f\u6301\u5bb9\u5668\u540d\u89e3\u6790 ping 172.17.0.3 # \u4f60\u9700\u8981\u77e5\u9053\u6570\u636e\u5e93\u5bb9\u5668\u7684\u5b9e\u9645IP\u5730\u5740<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9ed8\u8ba4\u6865\u63a5\u7f51\u7edc\u7684\u95ee\u9898\uff1a<\/strong>\u200b \u5bb9\u5668\u4e4b\u95f4\u53ea\u80fd\u7528IP\u5730\u5740\u901a\u4fe1\uff0c\u4e0d\u80fd\u7528\u5bb9\u5668\u540d\uff0c\u5f88\u4e0d\u65b9\u4fbf\uff01<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u81ea\u5b9a\u4e49\u6865\u63a5\u7f51\u7edc &#8211; \u201c\u5e26\u8def\u724c\u7684\u667a\u80fd\u5185\u90e8\u9053\u8def\u201d\uff08\u63a8\u8350\uff01\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u6211\u4eec\u81ea\u5df1\u4fee\u5efa\u4e00\u4e2a\u66f4\u667a\u80fd\u7684\u5185\u90e8\u9053\u8def\u7cfb\u7edf\uff0c\u6bcf\u4e2a\u8def\u53e3\u90fd\u6709\u6e05\u6670\u7684<strong>\u8def\u724c\uff08\u5bb9\u5668\u540d\uff09<\/strong>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7279\u70b9\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u5bb9\u5668\u4e4b\u95f4\u4e0d\u4ec5\u53ef\u4ee5\u901a\u8fc7IP\u901a\u4fe1\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u7528<strong>\u5bb9\u5668\u540d<\/strong>\u76f8\u4e92\u8bbf\u95ee<\/li>\n\n\n\n<li>\u63d0\u4f9b\u4e86\u81ea\u52a8\u7684DNS\u89e3\u6790\u670d\u52a1\uff08\u5bb9\u5668\u540d\u81ea\u52a8\u89e3\u6790\u4e3aIP\uff09<\/li>\n\n\n\n<li>\u66f4\u597d\u7684\u9694\u79bb\u6027\uff08\u53ea\u6709\u52a0\u5165\u8fd9\u4e2a\u7f51\u7edc\u7684\u5bb9\u5668\u624d\u80fd\u4e92\u76f8\u8bbf\u95ee\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u521b\u5efa\u548c\u4f7f\u7528\uff1a<\/strong> <code># 1. \u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6865\u63a5\u7f51\u7edc\uff0c\u547d\u540d\u4e3a\"my-app-network\" docker network create my-app-network # 2. \u8fd0\u884c\u5bb9\u5668\u65f6\uff0c\u660e\u786e\u6307\u5b9a\u52a0\u5165\u8fd9\u4e2a\u7f51\u7edc docker run -d --name web --network my-app-network nginx docker run -d --name database --network my-app-network mysql # 3. \u73b0\u5728\u8fdb\u5165web\u5bb9\u5668\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u5bb9\u5668\u540d\u8bbf\u95ee\u6570\u636e\u5e93\uff01 docker exec -it web \/bin\/bash ping database # \u6210\u529f\u4e86\uff01\u56e0\u4e3a\u81ea\u5b9a\u4e49\u7f51\u7edc\u652f\u6301DNS\u89e3\u6790<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8fd9\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff01<\/strong>\u200b \u8ba9\u4f60\u7684\u591a\u5bb9\u5668\u5e94\u7528\u53ef\u4ee5\u50cf\u5728\u540c\u4e00\u4e2a&#8221;\u5185\u90e8\u7f51\u7edc&#8221;\u4e2d\u4e00\u6837\u65b9\u4fbf\u5730\u901a\u4fe1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u4e3b\u673a\u7f51\u7edc &#8211; \u201c\u76f4\u63a5\u628a\u8d27\u7bb1\u653e\u5728\u9a6c\u8def\u8fb9\u201d<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u8df3\u8fc7\u6240\u6709\u5185\u90e8\u9053\u8def\uff0c\u76f4\u63a5\u628a\u8d27\u7bb1\u653e\u5728<strong>\u516c\u5171\u9a6c\u8def\uff08\u4e3b\u673a\u7f51\u7edc\uff09<\/strong>\u200b \u65c1\u8fb9\u3002\u5bb9\u5668\u76f4\u63a5\u4f7f\u7528\u4e3b\u673a\u7684\u7f51\u7edc\u5806\u6808\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7279\u70b9\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u5bb9\u5668\u6ca1\u6709\u81ea\u5df1\u7684\u72ec\u7acb\u7f51\u7edc\u7a7a\u95f4\uff0c\u76f4\u63a5\u4f7f\u7528\u4e3b\u673a\u7684IP\u548c\u7aef\u53e3<\/li>\n\n\n\n<li>\u6027\u80fd\u6700\u597d\uff08\u56e0\u4e3a\u6ca1\u6709\u7f51\u7edc\u8f6c\u6362\u7684\u5f00\u9500\uff09<\/li>\n\n\n\n<li>\u6700\u4e0d\u5b89\u5168\uff08\u7aef\u53e3\u51b2\u7a81\u98ce\u9669\u5927\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u65b9\u5f0f\uff1a<\/strong> <code>docker run -d --name web --network host nginx<\/code>\u8fd9\u65f6\u5019\uff0cNginx\u5c31\u76f4\u63a5\u5728\u4e3b\u673a\u768480\u7aef\u53e3\u4e0a\u8fd0\u884c\uff0c\u4f60\u4e0d\u9700\u8981<code>-p 80:80<\/code>\u7aef\u53e3\u6620\u5c04\u4e86\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u65e0\u7f51\u7edc &#8211; \u201c\u5b8c\u5168\u9694\u79bb\u7684\u5c01\u95ed\u8d27\u7bb1\u201d<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6bd4\u55bb\uff1a<\/strong>\u200b \u8fd9\u4e2a\u8d27\u7bb1\u6ca1\u6709\u4efb\u4f55\u5bf9\u5916\u7684\u9053\u8def\uff0c\u5b8c\u5168\u5c01\u95ed\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u573a\u666f\uff1a<\/strong>\u200b \u8fd0\u884c\u4e00\u4e9b\u4e0d\u9700\u8981\u7f51\u7edc\u8fde\u63a5\u7684\u4efb\u52a1\uff0c\u6bd4\u5982\u6570\u636e\u6279\u5904\u7406\u3002 <code>docker run --network none my-batch-job-image<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9645\u573a\u666f\uff1a\u642d\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684Web\u5e94\u7528<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u8981\u642d\u5efa\u4e00\u4e2a\u5178\u578b\u7684Web\u5e94\u7528\uff1aNginx\uff08\u524d\u7aef\uff09 + Python\u5e94\u7528\uff08\u540e\u7aef\uff09 + MySQL\uff08\u6570\u636e\u5e93\uff09<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa41\uff1a\u521b\u5efa\u4e13\u5c5e\u7684\u201c\u5185\u90e8\u9053\u8def\u7cfb\u7edf\u201d<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>docker network create my-app-network<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa42\uff1a\u542f\u52a8\u6570\u636e\u5e93\u5bb9\u5668\uff0c\u5e76\u52a0\u5165\u7f51\u7edc<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d --name mysql-db \\\n  --network my-app-network \\\n  -e MYSQL_ROOT_PASSWORD=123456 \\\n  -v mysql-data:\/var\/lib\/mysql \\\n  mysql:8.0<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa43\uff1a\u542f\u52a8\u540e\u7aef\u5e94\u7528\u5bb9\u5668\uff0c\u5e76\u52a0\u5165\u7f51\u7edc<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d --name backend-api \\\n  --network my-app-network \\\n  -v \/path\/to\/your\/code:\/app \\\n  your-python-app-image<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa44\uff1a\u542f\u52a8\u524d\u7aefWeb\u670d\u52a1\u5668\uff0c\u5e76\u52a0\u5165\u7f51\u7edc<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d --name nginx-frontend \\\n  --network my-app-network \\\n  -p 80:80 \\\n  -v \/path\/to\/nginx.conf:\/etc\/nginx\/nginx.conf \\\n  nginx<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u901a\u4fe1\u6d41\u7a0b\uff1a<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7528\u6237\u8bbf\u95ee <code>http:\/\/\u4f60\u7684\u670d\u52a1\u5668IP<\/code>\u2192 \u5230\u8fbe <strong>nginx-frontend<\/strong>\u200b \u5bb9\u5668<\/li>\n\n\n\n<li>Nginx\u9700\u8981\u8c03\u7528API \u2192 \u5b83\u76f4\u63a5\u8bbf\u95ee <code>http:\/\/backend-api:8000<\/code>\uff08\u7528\u5bb9\u5668\u540d\uff01\uff09<\/li>\n\n\n\n<li>Python\u540e\u7aef\u9700\u8981\u8bfb\u5199\u6570\u636e \u2192 \u5b83\u76f4\u63a5\u8fde\u63a5 <code>mysql-db:3306<\/code>\uff08\u7528\u5bb9\u5668\u540d\uff01\uff09<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8fd9\u4e00\u5207\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u5c31\u662f\u56e0\u4e3a\u6240\u6709\u5bb9\u5668\u90fd\u5728\u540c\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7f51\u7edc <code>my-app-network<\/code>\u4e2d\uff01<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u7528\u7684\u7f51\u7edc\u7ba1\u7406\u547d\u4ee4<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># \u67e5\u770b\u6240\u6709\u7f51\u7edc\ndocker network ls\n\n# \u67e5\u770b\u67d0\u4e2a\u7f51\u7edc\u7684\u8be6\u7ec6\u4fe1\u606f\uff08\u5305\u62ec\u54ea\u4e9b\u5bb9\u5668\u8fde\u63a5\u5728\u4e0a\u9762\uff09\ndocker network inspect my-app-network\n\n# \u5c06\u6b63\u5728\u8fd0\u884c\u7684\u5bb9\u5668\u8fde\u63a5\u5230\u67d0\u4e2a\u7f51\u7edc\ndocker network connect my-app-network existing-container\n\n# \u5c06\u5bb9\u5668\u4ece\u67d0\u4e2a\u7f51\u7edc\u65ad\u5f00\ndocker network disconnect my-app-network container-name\n\n# \u5220\u9664\u7f51\u7edc\uff08\u5fc5\u987b\u6ca1\u6709\u5bb9\u5668\u8fde\u63a5\u5728\u4e0a\u9762\uff09\ndocker network rm my-app-network<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6838\u5fc3\u601d\u60f3\u603b\u7ed3<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9ed8\u8ba4\u6865\u63a5\u7f51\u7edc\u4e0d\u597d\u7528<\/strong>\uff1a\u5bb9\u5668\u4e4b\u95f4\u53ea\u80fd\u7528IP\u5730\u5740\u901a\u4fe1\uff0c\u4e0d\u80fd\u7528\u540d\u5b57\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u6865\u63a5\u7f51\u7edc\u662f\u738b\u9053<\/strong>\uff1a\u63d0\u4f9b\u4e86DNS\u89e3\u6790\uff08\u5bb9\u5668\u540d\u2192IP\uff09\uff0c\u8ba9\u591a\u5bb9\u5668\u5e94\u7528\u5f00\u53d1\u53d8\u5f97\u7b80\u5355\u3002<\/li>\n\n\n\n<li><strong>\u7f51\u7edc\u5c31\u662f\u5bb9\u5668\u95f4\u7684&#8221;\u9053\u8def&#8221;<\/strong>\uff1a\u6ca1\u6709\u7f51\u7edc\uff0c\u5bb9\u5668\u5c31\u662f\u5b64\u5c9b\uff1b\u6709\u4e86\u7f51\u7edc\uff0c\u5b83\u4eec\u624d\u80fd\u534f\u540c\u5de5\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u9694\u79bb<\/strong>\uff1a\u4e0d\u540c\u7684\u81ea\u5b9a\u4e49\u7f51\u7edc\u662f\u76f8\u4e92\u9694\u79bb\u7684\uff0c\u4f60\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u9879\u76ee\u521b\u5efa\u4e0d\u540c\u7684\u7f51\u7edc\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e00\u53e5\u8bdd\u8bb0\u4f4fDocker\u7f51\u7edc\uff1a\u5b83\u5c31\u662f\u4e3a\u5404\u4e2a&#8221;\u8d27\u7bb1&#8221;\uff08\u5bb9\u5668\uff09\u4fee\u5efa\u5185\u90e8\u9053\u8def\uff0c\u8ba9\u5b83\u4eec\u80fd\u591f\u7528\u540d\u5b57\u76f8\u4e92\u558a\u8bdd\u3001\u534f\u540c\u5de5\u4f5c\uff01<\/strong> \u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7f51\u7edc\uff0c\u8fd0\u884c\u51e0\u4e2a\u5bb9\u5668\u6765\u4f53\u9a8c\u8fd9\u79cd\u4fbf\u6377\u7684\u901a\u4fe1\u65b9\u5f0f\u4e86\uff01\u8fd9\u662f\u6784\u5efa\u590d\u6742\u591a\u5bb9\u5668\u5e94\u7528\u7684\u57fa\u7840\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u53ef\u4ee5\u804a\u804a<code>docker-compose<\/code>\uff0c\u5b83\u80fd\u8ba9\u4f60\u7528\u4e00\u4efd\u914d\u7f6e\u6587\u4ef6\u8f7b\u677e\u7ba1\u7406\u6574\u4e2a&#8221;\u7269\u6d41\u7cfb\u7edf&#8221;\uff08\u5305\u62ec\u7f51\u7edc\u3001\u5bb9\u5668\u3001\u6570\u636e\u5377\u7b49\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u592a\u68d2\u4e86\uff01Docker Compose \u662f\u6211\u4eec Docker \u5b66\u4e60\u7684\u6700\u540e\u4e00\u4e2a\u91cd\u8981\u62fc\u56fe\uff0c\u5b83\u4f1a\u8ba9\u4e00\u5207\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u548c\u4f18\u96c5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u9700\u8981 Docker Compose\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u56de\u5fc6\u4e00\u4e0b\u6211\u4eec\u4e4b\u524d\u90e8\u7f72\u591a\u5bb9\u5668\u5e94\u7528\u6709\u591a\u9ebb\u70e6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u521b\u5efa\u7f51\u7edc\ndocker network create my-app-network\n\n# 2. \u542f\u52a8\u6570\u636e\u5e93\ndocker run -d --name mysql-db --network my-app-network -e MYSQL_ROOT_PASSWORD=123456 -v mysql-data:\/var\/lib\/mysql mysql:8.0\n\n# 3. \u542f\u52a8\u540e\u7aef\ndocker run -d --name backend-api --network my-app-network -v \/path\/to\/code:\/app your-python-app-image\n\n# 4. \u542f\u52a8\u524d\u7aef\ndocker run -d --name nginx-frontend --network my-app-network -p 80:80 nginx<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u547d\u4ee4\u53c8\u957f\u53c8\u590d\u6742\uff0c\u5bb9\u6613\u6253\u9519<\/li>\n\n\n\n<li>\u8981\u8bb0\u4f4f\u5f88\u591a\u53c2\u6570\u548c\u73af\u5883\u53d8\u91cf<\/li>\n\n\n\n<li>\u91cd\u542f\u6574\u4e2a\u5e94\u7528\u5f88\u9ebb\u70e6<\/li>\n\n\n\n<li>\u96be\u4ee5\u5728\u4e0d\u540c\u73af\u5883\uff08\u5f00\u53d1\u3001\u6d4b\u8bd5\u3001\u751f\u4ea7\uff09\u4e4b\u95f4\u4fdd\u6301\u4e00\u81f4\u6027<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Docker Compose \u7684\u6bd4\u55bb\uff1a<\/strong>\u200b \u5b83\u5c31\u50cf\u662f<strong>\u7269\u6d41\u4e2d\u5fc3\u7684&#8221;\u4e00\u952e\u90e8\u7f72\u63a7\u5236\u53f0&#8221;<\/strong>\uff01 \u6709\u4e86\u8fd9\u4e2a\u63a7\u5236\u53f0\uff0c\u4f60\u4e0d\u9700\u8981\u624b\u52a8\u4e00\u4e2a\u4e2a\u5b89\u6392\u8d27\u7bb1\u3001\u4fee\u9053\u8def\u3001\u63a5\u6c34\u7ba1\u3002\u53ea\u9700\u8981\u6309\u4e0b\u4e00\u4e2a\u6309\u94ae\uff08<code>docker-compose up<\/code>\uff09\uff0c\u6574\u4e2a\u7269\u6d41\u7cfb\u7edf\u5c31\u81ea\u52a8\u6309\u8bbe\u8ba1\u56fe\u7eb8\u8fd0\u8f6c\u8d77\u6765\u4e86\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Docker Compose \u7684\u6838\u5fc3\uff1adocker-compose.yml \u6587\u4ef6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a YAML \u6587\u4ef6\u5c31\u662f\u6211\u4eec\u7684<strong>&#8220;\u7269\u6d41\u4e2d\u5fc3\u8bbe\u8ba1\u84dd\u56fe&#8221;<\/strong>\uff0c\u5b83\u7528\u4eba\u7c7b\u53ef\u8bfb\u7684\u65b9\u5f0f\u63cf\u8ff0\u4e86\u6574\u4e2a\u591a\u5bb9\u5668\u5e94\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e00\u4e2a\u5b8c\u6574\u7684 docker-compose.yml \u4f8b\u5b50<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8ba9\u6211\u4eec\u7528\u4e4b\u524d\u7684\u4e09\u4ef6\u5957\u5e94\u7528\uff08Nginx + Python + MySQL\uff09\u6765\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8bbe\u8ba1\u84dd\u56fe\u7684\u7248\u672c\u53f7\nversion: '3.8'\n\n# \u5b9a\u4e49\u6240\u6709\u670d\u52a1\uff08\u5c31\u662f\u6211\u4eec\u7684\u5404\u4e2a\"\u8d27\u7bb1\"\uff09\nservices:\n  # \u7b2c\u4e00\u4e2a\u670d\u52a1\uff1a\u524d\u7aefWeb\u670d\u52a1\u5668\n  nginx:\n    image: nginx:latest  # \u4f7f\u7528\u54ea\u4e2a\u6a21\u5177\uff08\u955c\u50cf\uff09\n    ports:\n      - \"80:80\"          # \u7aef\u53e3\u6620\u5c04\uff1a\u4e3b\u673a80\u7aef\u53e3 -&gt; \u5bb9\u566880\u7aef\u53e3\n    volumes:\n      - .\/nginx.conf:\/etc\/nginx\/nginx.conf  # \u914d\u7f6e\u6587\u4ef6\u6302\u8f7d\n    depends_on:\n      - backend          # \u58f0\u660e\u4f9d\u8d56\uff1a\u5148\u542f\u52a8backend\u518d\u542f\u52a8\u6211\n    networks:\n      - app-network      # \u8fde\u63a5\u5230\u54ea\u4e2a\u9053\u8def\u7cfb\u7edf\n\n  # \u7b2c\u4e8c\u4e2a\u670d\u52a1\uff1a\u540e\u7aefAPI\n  backend:\n    build: .\/backend      # \u4e0d\u662f\u7528\u73b0\u6210\u955c\u50cf\uff0c\u800c\u662f\u6839\u636eDockerfile\u6784\u5efa\n    environment:\n      - DATABASE_URL=mysql:\/\/db:3306\/mydb  # \u73af\u5883\u53d8\u91cf\n      - DEBUG=true\n    volumes:\n      - .\/backend:\/app    # \u4ee3\u7801\u70ed\u91cd\u8f7d\uff1a\u5f00\u53d1\u65f6\u4fee\u6539\u4ee3\u7801\u81ea\u52a8\u751f\u6548\n    depends_on:\n      - db               # \u4f9d\u8d56\u6570\u636e\u5e93\n    networks:\n      - app-network\n\n  # \u7b2c\u4e09\u4e2a\u670d\u52a1\uff1a\u6570\u636e\u5e93\n  db:\n    image: mysql:8.0\n    environment:\n      - MYSQL_ROOT_PASSWORD=123456\n      - MYSQL_DATABASE=mydb\n    volumes:\n      - db_data:\/var\/lib\/mysql  # \u6570\u636e\u6301\u4e45\u5316\n    networks:\n      - app-network\n\n# \u5b9a\u4e49\u6570\u636e\u5377\uff08\u6211\u4eec\u7684\"\u5916\u63a5\u786c\u76d8\"\uff09\nvolumes:\n  db_data:\n\n# \u5b9a\u4e49\u7f51\u7edc\uff08\u6211\u4eec\u7684\"\u5185\u90e8\u9053\u8def\u7cfb\u7edf\"\uff09\nnetworks:\n  app-network:\n    driver: bridge<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u5230\u4e86\u5417\uff1f\u8fd9\u4e2a YAML \u6587\u4ef6\u6e05\u6670\u5730\u63cf\u8ff0\u4e86\u6211\u4eec\u4e4b\u524d\u7528\u4e00\u5806\u547d\u4ee4\u624d\u80fd\u5b8c\u6210\u7684\u4e8b\u60c5\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Docker Compose \u6838\u5fc3\u8bed\u6cd5\u8be6\u89e3<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u670d\u52a1\u5b9a\u4e49\uff08Services\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u4e2a\u670d\u52a1\u76f8\u5f53\u4e8e\u4e00\u4e2a\u5bb9\u5668\uff0c\u53ef\u4ee5\u914d\u7f6e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>image<\/code><\/strong>: \u4f7f\u7528\u73b0\u6210\u7684\u955c\u50cf\uff0c\u5982 <code>nginx:latest<\/code><\/li>\n\n\n\n<li><strong><code>build<\/code><\/strong>: \u6839\u636e Dockerfile \u6784\u5efa\u955c\u50cf\uff0c\u5982 <code>build: .\/backend<\/code><\/li>\n\n\n\n<li><strong><code>ports<\/code><\/strong>: \u7aef\u53e3\u6620\u5c04\uff0c\u683c\u5f0f <code>\"\u4e3b\u673a\u7aef\u53e3:\u5bb9\u5668\u7aef\u53e3\"<\/code><\/li>\n\n\n\n<li><strong><code>volumes<\/code><\/strong>: \u6570\u636e\u5377\u6302\u8f7d\uff0c\u652f\u6301\u4e09\u79cd\u683c\u5f0f\uff1a\n<ul class=\"wp-block-list\">\n<li>\u547d\u540d\u5377\uff1a<code>db_data:\/var\/lib\/mysql<\/code>\uff08\u63a8\u8350\u7528\u4e8e\u6570\u636e\uff09<\/li>\n\n\n\n<li>\u7ed1\u5b9a\u6302\u8f7d\uff1a<code>.\/nginx.conf:\/etc\/nginx\/nginx.conf<\/code>\uff08\u63a8\u8350\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\uff09<\/li>\n\n\n\n<li>\u533f\u540d\u5377\uff1a<code>\/var\/lib\/mysql<\/code>\uff08\u4e0d\u63a8\u8350\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>environment<\/code><\/strong>: \u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u7528\u5b57\u5178\u6216\u6570\u7ec4\u683c\u5f0f<\/li>\n\n\n\n<li><strong><code>depends_on<\/code><\/strong>: \u4f9d\u8d56\u5173\u7cfb\uff0c\u63a7\u5236\u542f\u52a8\u987a\u5e8f<\/li>\n\n\n\n<li><strong><code>networks<\/code><\/strong>: \u8fde\u63a5\u7684\u7f51\u7edc<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u7f51\u7edc\u5b9a\u4e49\uff08Networks\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u7f51\u7edc\uff0c\u8ba9\u670d\u52a1\u4e4b\u95f4\u7528\u670d\u52a1\u540d\u4e92\u76f8\u8bbf\u95ee<\/li>\n\n\n\n<li>\u5982\u679c\u4e0d\u6307\u5b9a\uff0cCompose \u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u7f51\u7edc<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u6570\u636e\u5377\u5b9a\u4e49\uff08Volumes\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u58f0\u660e\u9700\u8981\u4f7f\u7528\u7684\u547d\u540d\u5377\uff0cCompose \u4f1a\u81ea\u52a8\u521b\u5efa\u548c\u7ba1\u7406<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Docker Compose \u5e38\u7528\u547d\u4ee4<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e86&#8221;\u8bbe\u8ba1\u84dd\u56fe&#8221;\uff0c\u73b0\u5728\u6765\u5b66\u4e60&#8221;\u63a7\u5236\u53f0\u6309\u94ae&#8221;\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>\u4e00\u952e\u90e8\u7f72<\/strong>\u200b &#8211; <code>docker-compose up<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u542f\u52a8\u6240\u6709\u670d\u52a1\uff08\u524d\u53f0\u8fd0\u884c\uff0c\u53ef\u4ee5\u770b\u5230\u65e5\u5fd7\uff09\ndocker-compose up\n\n# \u540e\u53f0\u542f\u52a8\u6240\u6709\u670d\u52a1\ndocker-compose up -d\n\n# \u91cd\u65b0\u6784\u5efa\u955c\u50cf\u5e76\u542f\u52a8\uff08Dockerfile\u6709\u4fee\u6539\u65f6\u4f7f\u7528\uff09\ndocker-compose up --build<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5c31\u50cf\uff1a<\/strong>\u200b \u6309\u4e0b&#8221;\u5f00\u59cb\u8fd0\u8425&#8221;\u6309\u94ae\uff0c\u6574\u4e2a\u7269\u6d41\u4e2d\u5fc3\u6309\u84dd\u56fe\u81ea\u52a8\u8fd0\u8f6c\u8d77\u6765\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>\u67e5\u770b\u72b6\u6001<\/strong>\u200b &#8211; <code>docker-compose ps<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u67e5\u770b\u5f53\u524d\u9879\u76ee\u7684\u670d\u52a1\u72b6\u6001\ndocker-compose ps<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name                  Command               State           Ports         \n----------------------------------------------------------------------------\nmyapp_backend_1   python app.py               Up                            \nmyapp_db_1        docker-entrypoint.sh mysqld Up      3306\/tcp, 33060\/tcp    \nmyapp_nginx_1     nginx -g daemon off;        Up      0.0.0.0:80-&gt;80\/tcp<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>\u67e5\u770b\u65e5\u5fd7<\/strong>\u200b &#8211; <code>docker-compose logs<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u67e5\u770b\u6240\u6709\u670d\u52a1\u7684\u65e5\u5fd7\ndocker-compose logs\n\n# \u67e5\u770b\u7279\u5b9a\u670d\u52a1\u7684\u65e5\u5fd7\ndocker-compose logs backend\n\n# \u5b9e\u65f6\u8ddf\u8e2a\u65e5\u5fd7\ndocker-compose logs -f backend<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. <strong>\u6267\u884c\u547d\u4ee4<\/strong>\u200b &#8211; <code>docker-compose exec<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5728backend\u670d\u52a1\u4e2d\u6267\u884c\u547d\u4ee4\ndocker-compose exec backend \/bin\/bash\ndocker-compose exec backend python manage.py migrate<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5. <strong>\u505c\u6b62\u670d\u52a1<\/strong>\u200b &#8211; <code>docker-compose down<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u505c\u6b62\u5e76\u5220\u9664\u6240\u6709\u5bb9\u5668\u3001\u7f51\u7edc\uff08\u4f46\u4fdd\u7559\u6570\u636e\u5377\uff09\ndocker-compose down\n\n# \u505c\u6b62\u5e76\u5220\u9664\u6240\u6709\u5bb9\u5668\u3001\u7f51\u7edc\u3001\u6570\u636e\u5377\ndocker-compose down -v<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6. <strong>\u5176\u4ed6\u5b9e\u7528\u547d\u4ee4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u67e5\u770b\u670d\u52a1\u4f9d\u8d56\u56fe\ndocker-compose config\n\n# \u91cd\u542f\u670d\u52a1\ndocker-compose restart\n\n# \u7f29\u653e\u670d\u52a1\u5b9e\u4f8b\u6570\uff08\u6bd4\u5982\u542f\u52a83\u4e2abackend\u5b9e\u4f8b\uff09\ndocker-compose up --scale backend=3<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u9645\u9879\u76ee\u7ed3\u6784\u793a\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u5178\u578b\u7684 Docker Compose \u9879\u76ee\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my-app\/\n\u251c\u2500\u2500 docker-compose.yml          # Compose\u914d\u7f6e\u6587\u4ef6\n\u251c\u2500\u2500 .env                        # \u73af\u5883\u53d8\u91cf\u6587\u4ef6\uff08\u5bc6\u7801\u7b49\u654f\u611f\u4fe1\u606f\uff09\n\u251c\u2500\u2500 nginx\/\n\u2502   \u251c\u2500\u2500 nginx.conf             # Nginx\u914d\u7f6e\n\u2502   \u2514\u2500\u2500 Dockerfile             # \u81ea\u5b9a\u4e49Nginx\u955c\u50cf\n\u251c\u2500\u2500 backend\/\n\u2502   \u251c\u2500\u2500 Dockerfile             # \u540e\u7aefDockerfile\n\u2502   \u251c\u2500\u2500 requirements.txt\n\u2502   \u2514\u2500\u2500 app.py\n\u2514\u2500\u2500 frontend\/\n    \u251c\u2500\u2500 Dockerfile             # \u524d\u7aefDockerfile\n    \u2514\u2500\u2500 src\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>.env \u6587\u4ef6\u793a\u4f8b\uff08\u7528\u4e8e\u914d\u7f6e\u654f\u611f\u4fe1\u606f\uff09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MYSQL_ROOT_PASSWORD=mysecretpassword\nDEBUG=true\nDATABASE_URL=mysql:\/\/db:3306\/mydb<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5728 docker-compose.yml \u4e2d\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>environment:\n  - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f00\u53d1\u73af\u5883 vs \u751f\u4ea7\u73af\u5883<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Docker Compose \u7684\u4e00\u4e2a\u5f3a\u5927\u529f\u80fd\u662f\u652f\u6301\u591a\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5f00\u53d1\u73af\u5883\uff08\u4f7f\u7528\u70ed\u91cd\u8f7d\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528\u9ed8\u8ba4\u7684 docker-compose.yml \u548c\u5f00\u53d1\u73af\u5883\u914d\u7f6e\ndocker-compose -f docker-compose.yml -f docker-compose.dev.yml up<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>docker-compose.dev.yml:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>version: '3.8'\nservices:\n  backend:\n    volumes:\n      - .\/backend:\/app  # \u4ee3\u7801\u70ed\u91cd\u8f7d\n    environment:\n      - DEBUG=true<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u751f\u4ea7\u73af\u5883<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528\u751f\u4ea7\u73af\u5883\u914d\u7f6e\ndocker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>docker-compose.prod.yml:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>version: '3.8'\nservices:\n  backend:\n    environment:\n      - DEBUG=false\n      - PRODUCTION=true<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6838\u5fc3\u601d\u60f3\u603b\u7ed3<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u58f0\u660e\u5f0f\u914d\u7f6e<\/strong>\uff1a\u7528 YAML \u6587\u4ef6\u63cf\u8ff0&#8221;\u4f60\u60f3\u8981\u4ec0\u4e48&#8221;\uff0c\u800c\u4e0d\u662f\u7528\u547d\u4ee4\u6307\u5b9a&#8221;\u600e\u4e48\u505a\u5230&#8221;\u3002<\/li>\n\n\n\n<li><strong>\u4e00\u952e\u64cd\u4f5c<\/strong>\uff1a\u590d\u6742\u7684\u591a\u5bb9\u5668\u5e94\u7528\uff0c\u73b0\u5728\u53ea\u9700\u8981 <code>docker-compose up<\/code>\u548c <code>docker-compose down<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u73af\u5883\u4e00\u81f4\u6027<\/strong>\uff1a\u5f00\u53d1\u3001\u6d4b\u8bd5\u3001\u751f\u4ea7\u73af\u5883\u4f7f\u7528\u76f8\u540c\u7684\u914d\u7f6e\uff0c\u907f\u514d&#8221;\u5728\u6211\u7535\u8111\u4e0a\u662f\u597d\u7684&#8221;\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u670d\u52a1\u7f16\u6392<\/strong>\uff1a\u81ea\u52a8\u5904\u7406\u7f51\u7edc\u3001\u6570\u636e\u5377\u3001\u4f9d\u8d56\u5173\u7cfb\u7b49\u590d\u6742\u95ee\u9898\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e00\u53e5\u8bdd\u8bb0\u4f4f Docker Compose\uff1a\u5b83\u5c31\u662f\u4e00\u4e2a&#8221;\u5e94\u7528\u7ea7\u522b&#8221;\u7684\u7ba1\u7406\u5de5\u5177\uff0c\u8ba9\u4f60\u7528\u4e00\u4efd\u914d\u7f6e\u6587\u4ef6\u5c31\u80fd\u7ba1\u7406\u6574\u4e2a\u591a\u5bb9\u5668\u5e94\u7528\u7684\u751f\u547d\u5468\u671f\uff01<\/strong> \u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4e3a\u4f60\u4e4b\u524d\u5b66\u4e60\u7684\u591a\u5bb9\u5668\u5e94\u7528\u521b\u5efa\u4e00\u4e2a <code>docker-compose.yml<\/code>\u6587\u4ef6\uff0c\u4f53\u9a8c\u4e00\u4e0b\u4e00\u952e\u90e8\u7f72\u7684\u723d\u5feb\u611f\u4e86\uff01\u8fd9\u662f\u4ece Docker \u521d\u5b66\u8005\u5230\u5b9e\u8df5\u8005\u7684\u91cd\u8981\u4e00\u6b65\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dockerfile\u548c\u81ea\u5b9a\u4e49\u955c\u50cf \u592a\u597d\u4e86\uff01\u6211\u4eec\u7ee7\u7eed\u7528\u201c\u7269\u6d41\u7cfb\u7edf\u201d\u7684\u6bd4\u55bb\uff0c\u6765\u6df1\u5165\u8bb2\u89e3Dockerfile\u548c\u81ea\u5b9a\u4e49 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[31],"class_list":["post-418","post","type-post","status-publish","format-standard","hentry","category-text","tag-docker"],"_links":{"self":[{"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/posts\/418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/comments?post=418"}],"version-history":[{"count":4,"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"predecessor-version":[{"id":425,"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/posts\/418\/revisions\/425"}],"wp:attachment":[{"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/43.139.99.192\/index.php\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}