{"id":22780,"date":"2025-04-19T06:27:13","date_gmt":"2025-04-18T22:27:13","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/22780.html"},"modified":"2025-04-19T06:27:13","modified_gmt":"2025-04-18T22:27:13","slug":"%e4%bd%bf%e7%94%a8-docker-%e5%ae%b9%e5%99%a8%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa-web-%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%9a%e4%bb%8e%e5%85%a5%e9%97%a8%e5%88%b0%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/22780.html","title":{"rendered":"\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5"},"content":{"rendered":"<hr \/>\n<h3>\u76ee\u5f55<\/h3>\n<li>Docker \u4e0e\u5bb9\u5668\u6280\u672f\u7b80\u4ecb<\/li>\n<li>\u51c6\u5907\u5de5\u4f5c<\/li>\n<li>\u901a\u8fc7 Docker \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 Web \u670d\u52a1\u5668\n<ul>\n<li>\u57fa\u4e8e\u5b98\u65b9 Nginx \u955c\u50cf\u642d\u5efa Web \u670d\u52a1\u5668<\/li>\n<li>\u57fa\u4e8e\u81ea\u5b9a\u4e49 Python \u811a\u672c\u7684 Web \u670d\u52a1\u5668<\/li>\n<\/ul>\n<\/li>\n<li>\u4f18\u5316 Docker Web \u670d\u52a1\u5668\n<ul>\n<li>\u7ed1\u5b9a\u6570\u636e\u5377\u5b9e\u73b0\u52a8\u6001\u66f4\u65b0<\/li>\n<li>\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u589e\u5f3a\u7075\u6d3b\u6027<\/li>\n<li>\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u4e0e\u6269\u5c55\u6027<\/li>\n<\/ul>\n<\/li>\n<li>\u90e8\u7f72 Docker Web \u670d\u52a1\u5668\u7684\u5b9e\u9645\u6848\u4f8b<\/li>\n<li>\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/li>\n<li>\u603b\u7ed3\u4e0e\u5efa\u8bae<\/li>\n<hr \/>\n<h3>Docker \u4e0e\u5bb9\u5668\u6280\u672f\u7b80\u4ecb<\/h3>\n<p>\u5728\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u901a\u8fc7 Docker \u5bb9\u5668\u521b\u5efa Web \u670d\u52a1\u5668\u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u7b80\u5355\u4e86\u89e3\u4e00\u4e0b Docker \u548c\u5bb9\u5668\u6280\u672f\u7684\u57fa\u7840\u77e5\u8bc6\u3002<\/p>\n<h4>\u4ec0\u4e48\u662f Docker&#xff1f;<\/h4>\n<p>Docker \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5bb9\u5668\u5316\u5e73\u53f0&#xff0c;\u5b83\u901a\u8fc7\u5c06\u5e94\u7528\u7a0b\u5e8f\u53ca\u5176\u4f9d\u8d56\u9879\u6253\u5305\u5230\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5bb9\u5668\u4e2d&#xff0c;\u89e3\u51b3\u4e86\u201c\u8fd0\u884c\u73af\u5883\u4e00\u81f4\u6027\u201d\u7684\u95ee\u9898\u3002Docker \u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u9694\u79bb\u673a\u5236&#xff0c;\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u8fd0\u884c\u76f8\u540c\u7684\u5bb9\u5668\u3002<\/p>\n<h4>Docker \u7684\u4e3b\u8981\u7ec4\u4ef6<\/h4>\n<li>\u955c\u50cf (Image)&#xff1a;\u5e94\u7528\u7a0b\u5e8f\u7684\u53ea\u8bfb\u6a21\u677f&#xff0c;\u5305\u542b\u6240\u6709\u8fd0\u884c\u65f6\u6240\u9700\u7684\u4f9d\u8d56\u3002<\/li>\n<li>\u5bb9\u5668 (Container)&#xff1a;\u955c\u50cf\u7684\u5b9e\u4f8b&#xff0c;\u662f\u8fd0\u884c\u7684\u72ec\u7acb\u5355\u5143\u3002<\/li>\n<li>Dockerfile&#xff1a;\u6784\u5efa\u955c\u50cf\u7684\u811a\u672c&#xff0c;\u5b9a\u4e49\u4e86\u955c\u50cf\u7684\u6240\u6709\u6307\u4ee4\u3002<\/li>\n<li>Docker Hub&#xff1a;\u516c\u5171\u7684\u955c\u50cf\u5b58\u50a8\u4ed3\u5e93&#xff0c;\u7528\u6237\u53ef\u4ee5\u5728\u8fd9\u91cc\u4e0b\u8f7d\u6216\u4e0a\u4f20\u955c\u50cf\u3002<\/li>\n<hr \/>\n<h3>\u51c6\u5907\u5de5\u4f5c<\/h3>\n<p>\u5728\u5f00\u59cb\u64cd\u4f5c\u4e4b\u524d&#xff0c;\u8bf7\u786e\u4fdd\u60a8\u7684\u7cfb\u7edf\u73af\u5883\u6ee1\u8db3\u4ee5\u4e0b\u8981\u6c42&#xff1a;<\/p>\n<li>\u64cd\u4f5c\u7cfb\u7edf&#xff1a;Linux\u3001Windows \u6216 macOS\u3002<\/li>\n<li>Docker \u5b89\u88c5&#xff1a;\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684 Docker&#xff0c;\u5b98\u65b9\u5b89\u88c5\u6587\u6863\u53ef\u53c2\u8003 Docker \u5b98\u65b9\u6587\u6863\u3002<\/li>\n<li>\u7f51\u7edc\u73af\u5883&#xff1a;\u786e\u4fdd\u80fd\u591f\u8bbf\u95ee Docker Hub&#xff0c;\u4ee5\u4fbf\u62c9\u53d6\u955c\u50cf\u3002<\/li>\n<h4>\u5b89\u88c5 Docker<\/h4>\n<p>\u4ee5 Ubuntu \u7cfb\u7edf\u4e3a\u4f8b&#xff0c;\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5b89\u88c5 Docker&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u66f4\u65b0\u5305\u7ba1\u7406\u5668<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt-get<\/span> update<\/p>\n<p><span class=\"token comment\"># \u5b89\u88c5\u5fc5\u8981\u7684\u4f9d\u8d56<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt-get<\/span> <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> ca-certificates <span class=\"token function\">curl<\/span> gnupg<\/p>\n<p><span class=\"token comment\"># \u6dfb\u52a0 Docker \u5b98\u65b9 GPG \u5bc6\u94a5<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-m<\/span> 0755 <span class=\"token parameter variable\">-d<\/span> \/etc\/apt\/keyrings<br \/>\n<span class=\"token function\">curl<\/span> <span class=\"token parameter variable\">-fsSL<\/span> https:\/\/download.docker.com\/linux\/ubuntu\/gpg <span class=\"token operator\">|<\/span> <span class=\"token function\">sudo<\/span> gpg <span class=\"token parameter variable\">&#8211;dearmor<\/span> <span class=\"token parameter variable\">-o<\/span> \/etc\/apt\/keyrings\/docker.gpg<\/p>\n<p><span class=\"token comment\"># \u6dfb\u52a0 Docker \u4ed3\u5e93<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token punctuation\">\\\\<\/span><br \/>\n  <span class=\"token string\">&#034;deb [arch&#061;<span class=\"token variable\"><span class=\"token variable\">$(<\/span>dpkg &#8211;print-architecture<span class=\"token variable\">)<\/span><\/span> signed-by&#061;\/etc\/apt\/keyrings\/docker.gpg] https:\/\/download.docker.com\/linux\/ubuntu \\\\<br \/>\n  <span class=\"token variable\"><span class=\"token variable\">$(<\/span>lsb_release <span class=\"token parameter variable\">-cs<\/span><span class=\"token variable\">)<\/span><\/span> stable&#034;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">sudo<\/span> <span class=\"token function\">tee<\/span> \/etc\/apt\/sources.list.d\/docker.list <span class=\"token operator\">&gt;<\/span> \/dev\/null<\/p>\n<p><span class=\"token comment\"># \u5b89\u88c5 Docker<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt-get<\/span> update<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt-get<\/span> <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> docker-ce docker-ce-cli containerd.io<\/p>\n<p><span class=\"token comment\"># \u542f\u52a8\u5e76\u9a8c\u8bc1 Docker \u662f\u5426\u5b89\u88c5\u6210\u529f<\/span><br \/>\n<span class=\"token function\">sudo<\/span> systemctl start <span class=\"token function\">docker<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">docker<\/span> version<\/p>\n<hr \/>\n<h3>\u901a\u8fc7 Docker \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 Web \u670d\u52a1\u5668<\/h3>\n<h4>\u57fa\u4e8e\u5b98\u65b9 Nginx \u955c\u50cf\u642d\u5efa Web \u670d\u52a1\u5668<\/h4>\n<p>Nginx \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 Web \u670d\u52a1\u5668&#xff0c;\u5728 Web \u5e94\u7528\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002Docker \u63d0\u4f9b\u4e86\u5b98\u65b9\u7684 Nginx \u955c\u50cf&#xff0c;\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u642d\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\u3002<\/p>\n<h5>1. \u62c9\u53d6 Nginx \u5b98\u65b9\u955c\u50cf<\/h5>\n<p><span class=\"token function\">docker<\/span> pull nginx<\/p>\n<h5>2. \u8fd0\u884c Nginx \u5bb9\u5668<\/h5>\n<p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u542f\u52a8\u4e00\u4e2a\u57fa\u4e8e Nginx \u7684 Web \u670d\u52a1\u5668&#xff1a;<\/p>\n<p><span class=\"token function\">docker<\/span> run <span class=\"token parameter variable\">&#8211;name<\/span> my-nginx <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">8080<\/span>:80 <span class=\"token parameter variable\">-d<\/span> nginx<\/p>\n<p>\u89e3\u91ca&#xff1a;<\/p>\n<ul>\n<li>&#8211;name my-nginx&#xff1a;\u4e3a\u5bb9\u5668\u6307\u5b9a\u540d\u79f0\u3002<\/li>\n<li>-p 8080:80&#xff1a;\u5c06\u4e3b\u673a\u7684 8080 \u7aef\u53e3\u6620\u5c04\u5230\u5bb9\u5668\u7684 80 \u7aef\u53e3\u3002<\/li>\n<li>-d&#xff1a;\u4ee5\u540e\u53f0\u6a21\u5f0f\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p>\u8bbf\u95ee http:\/\/localhost:8080&#xff0c;\u60a8\u5c06\u770b\u5230\u9ed8\u8ba4\u7684 Nginx \u6b22\u8fce\u9875\u9762\u3002<\/p>\n<h5>3. \u81ea\u5b9a\u4e49 Nginx \u9875\u9762<\/h5>\n<p>\u4e3a\u4e86\u81ea\u5b9a\u4e49 Nginx \u7684\u9875\u9762&#xff0c;\u6211\u4eec\u9700\u8981\u6302\u8f7d\u4e00\u4e2a\u672c\u5730\u76ee\u5f55\u4f5c\u4e3a\u5bb9\u5668\u4e2d\u7684 Web \u6839\u76ee\u5f55\u3002<\/p>\n<p><span class=\"token function\">mkdir<\/span> ~\/nginx-html<br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;&lt;h1&gt;Welcome to my Dockerized Nginx server!&lt;\/h1&gt;&#034;<\/span> <span class=\"token operator\">&gt;<\/span> ~\/nginx-html\/index.html<br \/>\n<span class=\"token function\">docker<\/span> run <span class=\"token parameter variable\">&#8211;name<\/span> my-nginx <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">8080<\/span>:80 <span class=\"token parameter variable\">-v<\/span> ~\/nginx-html:\/usr\/share\/nginx\/html:ro <span class=\"token parameter variable\">-d<\/span> nginx<\/p>\n<h4>\u57fa\u4e8e\u81ea\u5b9a\u4e49 Python \u811a\u672c\u7684 Web \u670d\u52a1\u5668<\/h4>\n<p>\u5982\u679c\u60a8\u60f3\u6784\u5efa\u4e00\u4e2a\u57fa\u4e8e Python \u7684 Web \u670d\u52a1\u5668&#xff0c;\u53ef\u4ee5\u4f7f\u7528 Flask \u4f5c\u4e3a\u8f7b\u91cf\u7ea7\u6846\u67b6\u3002<\/p>\n<h5>1. \u7f16\u5199 Flask \u5e94\u7528<\/h5>\n<p>\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939 flask-app&#xff0c;\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u6587\u4ef6&#xff1a;<\/p>\n<ul>\n<li>app.py&#xff1a;<\/li>\n<\/ul>\n<p><span class=\"token keyword\">from<\/span> flask <span class=\"token keyword\">import<\/span> Flask<\/p>\n<p>app <span class=\"token operator\">&#061;<\/span> Flask<span class=\"token punctuation\">(<\/span>__name__<span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;app<span class=\"token punctuation\">.<\/span>route<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\/&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">home<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token string\">&#034;Hello, Dockerized Flask Server!&#034;<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#039;__main__&#039;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    app<span class=\"token punctuation\">.<\/span>run<span class=\"token punctuation\">(<\/span>host<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;0.0.0.0&#039;<\/span><span class=\"token punctuation\">,<\/span> port<span class=\"token operator\">&#061;<\/span><span class=\"token number\">5000<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<ul>\n<li>requirements.txt&#xff1a;<\/li>\n<\/ul>\n<p>Flask&#061;&#061;2.3.2<\/p>\n<h5>2. \u7f16\u5199 Dockerfile<\/h5>\n<p>\u521b\u5efa\u4e00\u4e2a Dockerfile \u6765\u5b9a\u4e49\u955c\u50cf\u7684\u6784\u5efa\u89c4\u5219&#xff1a;<\/p>\n<p># \u57fa\u4e8e\u5b98\u65b9 Python \u955c\u50cf<br \/>\nFROM python:3.9-slim<\/p>\n<p># \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55<br \/>\nWORKDIR \/app<\/p>\n<p># \u590d\u5236\u5e94\u7528\u4ee3\u7801<br \/>\nCOPY requirements.txt requirements.txt<br \/>\nCOPY app.py app.py<\/p>\n<p># \u5b89\u88c5\u4f9d\u8d56<br \/>\nRUN pip install -r requirements.txt<\/p>\n<p># \u66b4\u9732\u7aef\u53e3<br \/>\nEXPOSE 5000<\/p>\n<p># \u6307\u5b9a\u542f\u52a8\u547d\u4ee4<br \/>\nCMD [&#034;python&#034;, &#034;app.py&#034;]<\/p>\n<h5>3. \u6784\u5efa\u4e0e\u8fd0\u884c\u955c\u50cf<\/h5>\n<p>\u6784\u5efa\u955c\u50cf&#xff1a;<\/p>\n<p><span class=\"token function\">docker<\/span> build <span class=\"token parameter variable\">-t<\/span> flask-server <span class=\"token builtin class-name\">.<\/span><\/p>\n<p>\u8fd0\u884c\u5bb9\u5668&#xff1a;<\/p>\n<p><span class=\"token function\">docker<\/span> run <span class=\"token parameter variable\">&#8211;name<\/span> my-flask-server <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">5000<\/span>:5000 flask-server<\/p>\n<p>\u8bbf\u95ee http:\/\/localhost:5000&#xff0c;\u60a8\u5c06\u770b\u5230\u8fd4\u56de\u7684\u5185\u5bb9\u3002<\/p>\n<hr \/>\n<h3>\u4f18\u5316 Docker Web \u670d\u52a1\u5668<\/h3>\n<h4>\u7ed1\u5b9a\u6570\u636e\u5377\u5b9e\u73b0\u52a8\u6001\u66f4\u65b0<\/h4>\n<p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d&#xff0c;\u4e3a\u4e86\u65b9\u4fbf\u8c03\u8bd5\u548c\u52a8\u6001\u66f4\u65b0&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5c06\u5e94\u7528\u4ee3\u7801\u4e0e\u5bb9\u5668\u7ed1\u5b9a&#xff1a;<\/p>\n<p><span class=\"token function\">docker<\/span> run <span class=\"token parameter variable\">&#8211;name<\/span> flask-server <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">5000<\/span>:5000 <span class=\"token parameter variable\">-v<\/span> <span class=\"token variable\"><span class=\"token variable\">$(<\/span><span class=\"token builtin class-name\">pwd<\/span><span class=\"token variable\">)<\/span><\/span>:\/app flask-server<\/p>\n<h4>\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u589e\u5f3a\u7075\u6d3b\u6027<\/h4>\n<p>\u901a\u8fc7 -e \u53c2\u6570&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5c06\u73af\u5883\u53d8\u91cf\u4f20\u9012\u7ed9\u5bb9\u5668\u3002\u4f8b\u5982&#xff1a;<\/p>\n<p><span class=\"token function\">docker<\/span> run <span class=\"token parameter variable\">&#8211;name<\/span> flask-server <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">5000<\/span>:5000 <span class=\"token parameter variable\">-e<\/span> <span class=\"token assign-left variable\">APP_ENV<\/span><span class=\"token operator\">&#061;<\/span>development flask-server<\/p>\n<p>\u5728\u4ee3\u7801\u4e2d\u53ef\u4ee5\u901a\u8fc7 os.environ \u8bbf\u95ee\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<h4>\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u4e0e\u6269\u5c55\u6027<\/h4>\n<p>\u901a\u8fc7 Docker Compose \u6216 Kubernetes&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a Web \u5bb9\u5668\u7684\u8d1f\u8f7d\u5747\u8861\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Docker Compose \u793a\u4f8b&#xff1a;<\/p>\n<ul>\n<li>docker-compose.yml&#xff1a;<\/li>\n<\/ul>\n<p><span class=\"token key atrule\">version<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;3.8&#034;<\/span><br \/>\n<span class=\"token key atrule\">services<\/span><span class=\"token punctuation\">:<\/span><br \/>\n  <span class=\"token key atrule\">web<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token key atrule\">build<\/span><span class=\"token punctuation\">:<\/span> .<br \/>\n    <span class=\"token key atrule\">ports<\/span><span class=\"token punctuation\">:<\/span><br \/>\n      <span class=\"token punctuation\">&#8211;<\/span> <span class=\"token string\">&#034;5000:5000&#034;<\/span><br \/>\n    <span class=\"token key atrule\">deploy<\/span><span class=\"token punctuation\">:<\/span><br \/>\n      <span class=\"token key atrule\">replicas<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">3<\/span><br \/>\n      <span class=\"token key atrule\">resources<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token key atrule\">limits<\/span><span class=\"token punctuation\">:<\/span><br \/>\n          <span class=\"token key atrule\">cpus<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;0.5&#034;<\/span><br \/>\n          <span class=\"token key atrule\">memory<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;512M&#034;<\/span><br \/>\n      <span class=\"token key atrule\">restart_policy<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token key atrule\">condition<\/span><span class=\"token punctuation\">:<\/span> on<span class=\"token punctuation\">&#8211;<\/span>failure<\/p>\n<p>\u4f7f\u7528 docker-compose up \u542f\u52a8\u96c6\u7fa4\u540e&#xff0c;\u8bbf\u95ee\u8d1f\u8f7d\u5747\u8861\u7684 Web \u670d\u52a1\u3002<\/p>\n<hr \/>\n<h3>\u90e8\u7f72 Docker Web \u670d\u52a1\u5668\u7684\u5b9e\u9645\u6848\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u6848\u4f8b&#xff1a;\u4f7f\u7528 Nginx \u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406&#xff0c;\u5c06\u8bf7\u6c42\u5206\u53d1\u5230\u591a\u4e2a Flask \u5bb9\u5668\u3002<\/p>\n<li>\u521b\u5efa Nginx \u914d\u7f6e\u6587\u4ef6 nginx.conf&#xff1a;<\/li>\n<p>server {<br \/>\n    listen 80;<\/p>\n<p>    location \/ {<br \/>\n        proxy_pass http:\/\/flask_server;<br \/>\n    }<br \/>\n}<\/p>\n<p>upstream flask_server {<br \/>\n    server flask1:5000;<br \/>\n    server flask2:5000;<br \/>\n}<\/p>\n<li>\u4f7f\u7528 Docker Compose \u6784\u5efa\u591a\u5bb9\u5668\u73af\u5883&#xff1a;<\/li>\n<p><span class=\"token key atrule\">version<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;3.8&#034;<\/span><br \/>\n<span class=\"token key atrule\">services<\/span><span class=\"token punctuation\">:<\/span><br \/>\n  <span class=\"token key atrule\">nginx<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token key atrule\">image<\/span><span class=\"token punctuation\">:<\/span> nginx<span class=\"token punctuation\">:<\/span>latest<br \/>\n    <span class=\"token key atrule\">ports<\/span><span class=\"token punctuation\">:<\/span><br \/>\n      <span class=\"token punctuation\">&#8211;<\/span> <span class=\"token string\">&#034;8080:80&#034;<\/span><br \/>\n    <span class=\"token key atrule\">volumes<\/span><span class=\"token punctuation\">:<\/span><br \/>\n      <span class=\"token punctuation\">&#8211;<\/span><\/p>\n<p> .\/nginx.conf<span class=\"token punctuation\">:<\/span>\/etc\/nginx\/conf.d\/default.conf<br \/>\n  <span class=\"token key atrule\">flask1<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token key atrule\">build<\/span><span class=\"token punctuation\">:<\/span> .<br \/>\n  <span class=\"token key atrule\">flask2<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token key atrule\">build<\/span><span class=\"token punctuation\">:<\/span> .<\/p>\n<p>\u8fd0\u884c docker-compose up \u540e&#xff0c;\u8bbf\u95ee http:\/\/localhost:8080\u3002<\/p>\n<hr \/>\n<h3>\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<li>\u5bb9\u5668\u65e0\u6cd5\u8bbf\u95ee\u5916\u90e8\u7f51\u7edc&#xff1a;\u68c0\u67e5 Docker \u7684\u7f51\u7edc\u914d\u7f6e\u3002<\/li>\n<li>\u7aef\u53e3\u51b2\u7a81&#xff1a;\u786e\u8ba4\u4e3b\u673a\u4e0a\u6ca1\u6709\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\u76f8\u540c\u7aef\u53e3\u3002<\/li>\n<li>\u5bb9\u5668\u65e5\u5fd7\u65e0\u6cd5\u67e5\u770b&#xff1a;\u4f7f\u7528 docker logs &lt;container_name&gt; \u8c03\u8bd5\u3002<\/li>\n<hr \/>\n<h3>\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60&#xff0c;\u6211\u4eec\u4ece\u7406\u8bba\u5230\u5b9e\u8df5\u5b8c\u6574\u5730\u638c\u63e1\u4e86\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa Web \u670d\u52a1\u5668\u7684\u65b9\u6cd5\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d&#xff0c;\u5efa\u8bae\u5f00\u53d1\u8005\u7ed3\u5408 CI\/CD \u6d41\u7a0b&#xff0c;\u8fdb\u4e00\u6b65\u63d0\u5347\u5e94\u7528\u90e8\u7f72\u7684\u6548\u7387\u548c\u7a33\u5b9a\u6027\u3002Docker \u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u529f\u80fd\u5c06\u5728\u73b0\u4ee3 DevOps \u6d41\u7a0b\u4e2d\u53d1\u6325\u8d8a\u6765\u8d8a\u91cd\u8981\u7684\u4f5c\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.9k\u6b21\uff0c\u70b9\u8d5e44\u6b21\uff0c\u6536\u85cf37\u6b21\u3002\u968f\u7740\u5bb9\u5668\u5316\u6280\u672f\u7684\u53d1\u5c55\uff0cDocker \u5df2\u6210\u4e3a\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u548c\u90e8\u7f72\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u901a\u8fc7 Docker\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u3001\u90e8\u7f72\u548c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff0c\u65e0\u9700\u62c5\u5fc3\u8fd0\u884c\u73af\u5883\u7684\u5dee\u5f02\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff0c\u4ece\u57fa\u7840\u6982\u5ff5\u5230\u5b9e\u9645\u64cd\u4f5c\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624b\u5e76\u6df1\u5165\u7406\u89e3 Docker \u7684\u5e94\u7528\u3002_1.\u521b\u5efaweb\u670d\u52a1\u5668\u5bb9\u5668(\u622a\u56fe): o\u4f7f\u7528docker\u8fd0\u884c\u4e00\u4e2aweb\u670d\u52a1\u5668\u5bb9\u5668,\u4f8b\u5982nginx\u6216apa<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[27,1752,43,44],"topic":[],"class_list":["post-22780","post","type-post","status-publish","format-standard","hentry","category-server","tag-docker","tag-1752","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/22780.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.9k\u6b21\uff0c\u70b9\u8d5e44\u6b21\uff0c\u6536\u85cf37\u6b21\u3002\u968f\u7740\u5bb9\u5668\u5316\u6280\u672f\u7684\u53d1\u5c55\uff0cDocker \u5df2\u6210\u4e3a\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u548c\u90e8\u7f72\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u901a\u8fc7 Docker\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u3001\u90e8\u7f72\u548c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff0c\u65e0\u9700\u62c5\u5fc3\u8fd0\u884c\u73af\u5883\u7684\u5dee\u5f02\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff0c\u4ece\u57fa\u7840\u6982\u5ff5\u5230\u5b9e\u9645\u64cd\u4f5c\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624b\u5e76\u6df1\u5165\u7406\u89e3 Docker \u7684\u5e94\u7528\u3002_1.\u521b\u5efaweb\u670d\u52a1\u5668\u5bb9\u5668(\u622a\u56fe): o\u4f7f\u7528docker\u8fd0\u884c\u4e00\u4e2aweb\u670d\u52a1\u5668\u5bb9\u5668,\u4f8b\u5982nginx\u6216apa\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/22780.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T22:27:13+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/22780.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/22780.html\",\"name\":\"\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T22:27:13+00:00\",\"dateModified\":\"2025-04-18T22:27:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/22780.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/22780.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/22780.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/22780.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.9k\u6b21\uff0c\u70b9\u8d5e44\u6b21\uff0c\u6536\u85cf37\u6b21\u3002\u968f\u7740\u5bb9\u5668\u5316\u6280\u672f\u7684\u53d1\u5c55\uff0cDocker \u5df2\u6210\u4e3a\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u548c\u90e8\u7f72\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u901a\u8fc7 Docker\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u3001\u90e8\u7f72\u548c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff0c\u65e0\u9700\u62c5\u5fc3\u8fd0\u884c\u73af\u5883\u7684\u5dee\u5f02\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff0c\u4ece\u57fa\u7840\u6982\u5ff5\u5230\u5b9e\u9645\u64cd\u4f5c\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624b\u5e76\u6df1\u5165\u7406\u89e3 Docker \u7684\u5e94\u7528\u3002_1.\u521b\u5efaweb\u670d\u52a1\u5668\u5bb9\u5668(\u622a\u56fe): o\u4f7f\u7528docker\u8fd0\u884c\u4e00\u4e2aweb\u670d\u52a1\u5668\u5bb9\u5668,\u4f8b\u5982nginx\u6216apa","og_url":"https:\/\/www.wsisp.com\/helps\/22780.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T22:27:13+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/22780.html","url":"https:\/\/www.wsisp.com\/helps\/22780.html","name":"\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T22:27:13+00:00","dateModified":"2025-04-18T22:27:13+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/22780.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/22780.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/22780.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4f7f\u7528 Docker \u5bb9\u5668\u521b\u5efa\u4e00\u4e2a Web \u670d\u52a1\u5668\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/22780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=22780"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/22780\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=22780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=22780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=22780"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=22780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}