{"id":11745,"date":"2024-11-15T12:31:29","date_gmt":"2024-11-15T04:31:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/11745\/"},"modified":"2024-11-15T12:31:29","modified_gmt":"2024-11-15T04:31:29","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8docker%e4%b8%ad%e6%9e%84%e5%bb%ba%e5%92%8c%e9%83%a8%e7%bd%b2python%e5%ba%94%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/11745\/","title":{"rendered":"\u5982\u4f55\u5728Docker\u4e2d\u6784\u5efa\u548c\u90e8\u7f72Python\u5e94\u7528\uff1f"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8docker%e4%b8%ad%e6%9e%84%e5%bb%ba%e5%92%8c%e9%83%a8%e7%bd%b2python%e5%ba%94%e7%94%a8%ef%bc%9f-NmBbWRBBsv\">\u5982\u4f55\u5728Docker\u4e2d\u6784\u5efa\u548c\u90e8\u7f72Python\u5e94\u7528\uff1f<\/h1>\n<p>\u968f\u7740\u5bb9\u5668\u5316\u6280\u672f\u7684\u666e\u53ca\uff0cDocker\u5df2\u6210\u4e3a\u5f00\u53d1\u548c\u90e8\u7f72\u5e94\u7528\u7a0b\u5e8f\u7684\u91cd\u8981\u5de5\u5177\u3002\u7279\u522b\u662f\u5728Python\u5f00\u53d1\u4e2d\uff0cDocker\u80fd\u591f\u63d0\u4f9b\u4e00\u81f4\u7684\u73af\u5883\uff0c\u7b80\u5316\u4f9d\u8d56\u7ba1\u7406\uff0c\u5e76\u63d0\u9ad8\u5e94\u7528\u7684\u53ef\u79fb\u690d\u6027\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Docker\u4e2d\u6784\u5efa\u548c\u90e8\u7f72Python\u5e94\u7528\u3002<\/p>\n<h2 id=\"1-%e5%ae%89%e8%a3%85docker-NmBbWRBBsv\">1. \u5b89\u88c5Docker<\/h2>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5df2\u5b89\u88c5Docker\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5Docker\u662f\u5426\u5df2\u5b89\u88c5\uff1a<\/p>\n<pre><code>docker --version<\/code><\/pre>\n<p>\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u8bbf\u95ee<a href=\"https:\/\/docs.docker.com\/get-docker\/\">Docker\u5b98\u7f51<\/a>\uff0c\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8f7d\u5e76\u5b89\u88c5Docker\u3002<\/p>\n<h2 id=\"2-%e5%88%9b%e5%bb%bapython%e5%ba%94%e7%94%a8-NmBbWRBBsv\">2. \u521b\u5efaPython\u5e94\u7528<\/h2>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python\u5e94\u7528\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a`app.py`\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code>from flask import Flask\n\napp = Flask(__name__)\n\n@app.route('\/')\ndef hello():\n    return \"Hello, Docker!\"\n\nif __name__ == '__main__':\n    app.run(host='0.0.0.0', port=5000)<\/code><\/pre>\n<p>\u8fd9\u4e2a\u5e94\u7528\u4f7f\u7528Flask\u6846\u67b6\uff0c\u76d1\u542c5000\u7aef\u53e3\u5e76\u8fd4\u56de\u201cHello, Docker!\u201d\u7684\u6d88\u606f\u3002<\/p>\n<h2 id=\"3-%e5%88%9b%e5%bb%badockerfile-NmBbWRBBsv\">3. \u521b\u5efaDockerfile<\/h2>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aDockerfile\u6765\u5b9a\u4e49\u5982\u4f55\u6784\u5efa\u6211\u4eec\u7684Docker\u955c\u50cf\u3002\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a`Dockerfile`\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code>FROM python:3.9-slim\n\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\nWORKDIR \/app\n\n# \u590d\u5236\u4f9d\u8d56\u6587\u4ef6\nCOPY requirements.txt .\n\n# \u5b89\u88c5\u4f9d\u8d56\nRUN pip install --no-cache-dir -r requirements.txt\n\n# \u590d\u5236\u5e94\u7528\u4ee3\u7801\nCOPY . .\n\n# \u66b4\u9732\u7aef\u53e3\nEXPOSE 5000\n\n# \u542f\u52a8\u5e94\u7528\nCMD [\"python\", \"app.py\"]<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2aDockerfile\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Python 3.9\u7684\u8f7b\u91cf\u7ea7\u7248\u672c\u4f5c\u4e3a\u57fa\u7840\u955c\u50cf\uff0c\u8bbe\u7f6e\u4e86\u5de5\u4f5c\u76ee\u5f55\uff0c\u5e76\u5b89\u88c5\u4e86\u5e94\u7528\u6240\u9700\u7684\u4f9d\u8d56\u3002<\/p>\n<h2 id=\"4-%e5%88%9b%e5%bb%ba%e4%be%9d%e8%b5%96%e6%96%87%e4%bb%b6-NmBbWRBBsv\">4. \u521b\u5efa\u4f9d\u8d56\u6587\u4ef6<\/h2>\n<p>\u4e3a\u4e86\u8ba9Docker\u77e5\u9053\u9700\u8981\u5b89\u88c5\u54ea\u4e9b\u4f9d\u8d56\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a`requirements.txt`\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code>Flask==2.0.1<\/code><\/pre>\n<h2 id=\"5-%e6%9e%84%e5%bb%badocker%e9%95%9c%e5%83%8f-NmBbWRBBsv\">5. \u6784\u5efaDocker\u955c\u50cf<\/h2>\n<p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6784\u5efaDocker\u955c\u50cf\uff1a<\/p>\n<pre><code>docker build -t my-python-app .<\/code><\/pre>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u6839\u636eDockerfile\u7684\u5b9a\u4e49\u6784\u5efa\u4e00\u4e2a\u540d\u4e3a`my-python-app`\u7684\u955c\u50cf\u3002<\/p>\n<h2 id=\"6-%e8%bf%90%e8%a1%8cdocker%e5%ae%b9%e5%99%a8-NmBbWRBBsv\">6. \u8fd0\u884cDocker\u5bb9\u5668<\/h2>\n<p>\u6784\u5efa\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884cDocker\u5bb9\u5668\uff1a<\/p>\n<pre><code>docker run -d -p 5000:5000 my-python-app<\/code><\/pre>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5728\u540e\u53f0\u8fd0\u884c\u5bb9\u5668\uff0c\u5e76\u5c06\u5bb9\u5668\u76845000\u7aef\u53e3\u6620\u5c04\u5230\u4e3b\u673a\u76845000\u7aef\u53e3\u3002<\/p>\n<h2 id=\"7-%e8%ae%bf%e9%97%ae%e5%ba%94%e7%94%a8-NmBbWRBBsv\">7. \u8bbf\u95ee\u5e94\u7528<\/h2>\n<p>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee <a href=\"http:\/\/localhost:5000\">http:\/\/localhost:5000<\/a> \u6765\u67e5\u770b\u4f60\u7684Python\u5e94\u7528\u3002\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u4f60\u5e94\u8be5\u80fd\u770b\u5230\u201cHello, Docker!\u201d\u7684\u6d88\u606f\u3002<\/p>\n<h2 id=\"%e6%80%bb%e7%bb%93-NmBbWRBBsv\">\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u5730\u5728Docker\u4e2d\u6784\u5efa\u548c\u90e8\u7f72\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Python\u5e94\u7528\u3002Docker\u4e0d\u4ec5\u7b80\u5316\u4e86\u73af\u5883\u914d\u7f6e\uff0c\u8fd8\u63d0\u9ad8\u4e86\u5e94\u7528\u7684\u53ef\u79fb\u690d\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u5bf9\u4e8e\u9700\u8981\u5728\u4e91\u73af\u5883\u4e2d\u8fd0\u884c\u7684\u5e94\u7528\uff0c\u4f7f\u7528Docker\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u548c\u8fd0\u7ef4\u7684\u6548\u7387\u3002\u5982\u679c\u4f60\u6b63\u5728\u5bfb\u627e\u9ad8\u6027\u80fd\u7684<a href=\"https:\/\/fwq.ai\">\u4e91\u670d\u52a1\u5668<\/a>\u89e3\u51b3\u65b9\u6848\uff0c\u7c73\u4e91\u63d0\u4f9b\u591a\u79cd\u9009\u62e9\uff0c\u5305\u62ec\u7f8e\u56fdVPS\u548c\u533f\u540d\u670d\u52a1\u5668\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u90e8\u7f72\u548c\u7ba1\u7406\u4f60\u7684\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728Docker\u4e2d\u6784\u5efa\u548c\u90e8\u7f72Python\u5e94\u7528\uff1f \u968f\u7740\u5bb9\u5668\u5316\u6280\u672f\u7684\u666e\u53ca\uff0cDocker\u5df2\u6210\u4e3a\u5f00\u53d1\u548c\u90e8\u7f72\u5e94\u7528\u7a0b\u5e8f\u7684\u91cd\u8981\u5de5\u5177\u3002\u7279\u522b\u662f\u5728Python\u5f00\u53d1\u4e2d\uff0cDocker\u80fd\u591f\u63d0\u4f9b\u4e00\u81f4\u7684\u73af\u5883\uff0c\u7b80\u5316\u4f9d\u8d56\u7ba1\u7406\uff0c\u5e76\u63d0\u9ad8\u5e94\u7528\u7684\u53ef\u79fb\u690d\u6027\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Docker\u4e2d\u6784\u5efa\u548c\u90e8\u7f72Python\u5e94\u7528\u3002 1. \u5b89\u88c5Docker \u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5df2\u5b89\u88c5Docker\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5Docker\u662f\u5426\u5df2\u5b89\u88c5\uff1a docker &#8211;version \u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u8bbf\u95eeDocker\u5b98\u7f51\uff0c\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8f7d\u5e76\u5b89\u88c5Docker\u3002 2. \u521b\u5efaPython\u5e94\u7528 \u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python\u5e94\u7528\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a`app.py`\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a from flask import Flask app = Flask(__name__) @app.route(&#8216;\/&#8217;) def hello(): return &#8220;Hello, Docker!&#8221; if __name__ == &#8216;__main__&#8217;: app.run(host=&#8217;0.0.0.0&#8242;, port=5000) \u8fd9\u4e2a\u5e94\u7528\u4f7f\u7528Flask\u6846\u67b6\uff0c\u76d1\u542c5000\u7aef\u53e3\u5e76\u8fd4\u56de\u201cHello, Docker!\u201d\u7684\u6d88\u606f\u3002 3. \u521b\u5efaDockerfile \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aDockerfile\u6765\u5b9a\u4e49\u5982\u4f55\u6784\u5efa\u6211\u4eec\u7684Docker\u955c\u50cf\u3002\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a`Dockerfile`\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a FROM python:3.9-slim # \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55 WORKDIR \/app # \u590d\u5236\u4f9d\u8d56\u6587\u4ef6 COPY requirements.txt . # \u5b89\u88c5\u4f9d\u8d56 RUN pip install &#8211;no-cache-dir -r requirements.txt # [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-11745","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/11745","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=11745"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/11745\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=11745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=11745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=11745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}