﻿{"id":9218,"date":"2021-08-29T21:14:14","date_gmt":"2021-08-29T13:14:14","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=9218"},"modified":"2021-12-24T15:10:36","modified_gmt":"2021-12-24T07:10:36","slug":"docker-run-nginx","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/docker-run-nginx.html","title":{"rendered":"\u5982\u4f55\u5728 Docker \u5bb9\u5668\u4e2d\u8fd0\u884c Nginx"},"content":{"rendered":"<p><strong>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u5c06 Nginx \u4f5c\u4e3a Docker \u5bb9\u5668\u8fd0\u884c\uff0c\u5e76\u5c06\u5176\u516c\u5f00\u7ed9\u60a8\u7684\u672c\u5730\u7f51\u7edc\u3002\u6b64\u5916\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u521b\u5efa Docker \u6570\u636e\u5377\u4ee5\u5728\u5bb9\u5668\u548c\u4e3b\u673a\u6587\u4ef6\u7cfb\u7edf\u4e4b\u95f4\u5171\u4eab\u4fe1\u606f\u3002<\/strong><\/p>\n<p>Nginx \u662f\u4e00\u79cd\u6d41\u884c\u7684\u5f00\u6e90\u8f6f\u4ef6\uff0c\u7528\u4e8e Web \u670d\u52a1\u3001\u53cd\u5411\u4ee3\u7406\u3001\u7f13\u5b58\u3001\u8d1f\u8f7d\u5e73\u8861\u7b49\u3002\u5b83\u975e\u5e38\u6d41\u884c\u5e76\u5728\u5f53\u4eca\u8bb8\u591a\u9ad8\u6d41\u91cf\u7f51\u7ad9\u4e0a\u4f7f\u7528\u3002Docker \u6700\u5e38\u89c1\u7684\u5de5\u4f5c\u8d1f\u8f7d\u4e4b\u4e00\u662f\u4f7f\u7528\u5b83\u6765\u5bb9\u5668\u5316 Web \u670d\u52a1\u5668\u3002\u6211\u4eec\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Nginx \u8fdb\u884c\u8bbe\u7f6e\u3002\u56e0\u6b64\uff0c\u8ba9\u6211\u4eec\u5e26\u60a8\u5b8c\u6210\u6574\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<p><a href=\"https:\/\/www.linuxmi.com\/docker-container.html\">Docker \u662f\u4e00\u4e2a\u5bb9\u5668\u5316\u5e73\u53f0<\/a>\uff0c\u7528\u4e8e\u5c06\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u4e00\u4e2a\u6613\u4e8e\u7ba1\u7406\u7684\u5bb9\u5668\u6620\u50cf\u3002<\/p>\n<h2 id=\"h-prerequisites\">\u5148\u51b3\u6761\u4ef6<\/h2>\n<p>\u60a8\u5fc5\u987b\u6ee1\u8db3\u4ee5\u4e0b\u8981\u6c42\u624d\u80fd\u5b8c\u6210\u672c\u6559\u7a0b\uff1a<\/p>\n<ol>\n<li>Docker \u5e94\u8be5\u5df2\u5b89\u88c5\u5e76\u5728\u60a8\u7684\u7cfb\u7edf\u4e0a\u672c\u5730\u8fd0\u884c\u3002<\/li>\n<li>\u60a8\u9700\u8981\u4e00\u4e2a root \u5e10\u6237\uff0c\u6216\u8005\u7528\u6237\u53ef\u4ee5\u8fd0\u884c sudo \u547d\u4ee4\u3002<\/li>\n<\/ol>\n<h2 id=\"h-setting-up-nginx-inside-docker\">\u5728 Docker \u4e2d\u8bbe\u7f6e Nginx<\/h2>\n<h3 id=\"h-pulling-the-image\">\u62c9\u53d6\u955c\u50cf<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5c06\u62c9\u53d6\u5b98\u65b9\u7684 Nginx \u955c\u50cf\u3002\u5728\u60a8\u7684 docker \u4e3b\u673a\u4e0a\uff0c\u5728\u7ec8\u7aef\u4e0a\u8fd0\u884c\u4ee5\u4e0b Docker pull \u547d\u4ee4\u4ee5\u4ece Docker hub\u4e0b\u8f7d Nginx<a href=\"https:\/\/hub.docker.com\/_\/nginx\">\u6700\u65b0\u955c\u50cf<\/a>\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ sudo docker pull nginx<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9219\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/08\/docker-run-nginx-1.png\" alt=\"\" width=\"1026\" height=\"501\" \/><\/p>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>Using default tag: latest<br \/>\nlatest: Pulling from library\/nginx<br \/>\ne1acddbe380c: Pull complete<br \/>\ne21006f71c6f: Pull complete<br \/>\nf3341cc17e58: Pull complete<br \/>\n2a53fa598ee2: Pull complete<br \/>\n12455f71a9b5: Pull complete<br \/>\nb86f2ba62d17: Pull complete<br \/>\nDigest: sha256:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef<br \/>\nStatus: Downloaded newer image for nginx:latest<br \/>\ndocker.io\/library\/nginx:latest<\/p>\n<h3 id=\"h-run-the-nginx-docker-container\">\u8fd0\u884c Nginx Docker \u5bb9\u5668<\/h3>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u51c6\u5907\u597d\u8fd0\u884c Nginx Docker \u5bb9\u5668\u5e76\u5c06\u5176\u7aef\u53e3\u516c\u5f00\u7ed9\u60a8\u7684\u672c\u5730\u7f51\u7edc\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u955c\u50cf\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ docker run -d -p 80:80 &#8211;name my-nginx-server nginx<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-9220 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/08\/docker-run-nginx-2.png\" alt=\"\" width=\"1026\" height=\"301\" \/><\/p>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<br \/>\nf204ea685629dbc1d09b4a2d5f6fb64f5ef84be7d58a1b7449cc144c65c90c92<br \/>\ndocker: Error response from daemon: driver failed programming external connectivity on endpoint my-nginx-server (27d91a249b151ea87841c46775647d7af43d12c3e753061531173f05016e485b): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use.<\/p>\n<ul>\n<li><code>-d<\/code>\u00a0\u2013 \u4ee5\u5206\u79bb\u6a21\u5f0f\u542f\u52a8\u5bb9\u5668\uff08\u5bb9\u5668\u5728\u540e\u53f0\u8fd0\u884c\uff09\u3002<\/li>\n<li><code>-p<\/code>\u00a0\u2013 \u5c06\u4e00\u4e2a\u7aef\u53e3\u4ece\u5bb9\u5668\u7ed1\u5b9a\u5230\u4e3b\u673a\uff08\u5c06\u4e3b\u673a\u6d41\u91cf\u7aef\u53e3 80 \u8def\u7531\u5230\u5bb9\u5668\u7aef\u53e3 80\uff09\u3002<\/li>\n<li><code>-name<\/code>\u00a0\u2013 \u6211\u4eec\u7684 Docker \u5bb9\u5668\u7684\u540d\u79f0\u3002<\/li>\n<\/ul>\n<p>\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u201c\u00a0<em>nginx\u201d<\/em>\u544a\u8bc9 Docker \u5bb9\u5668\u4f7f\u7528\u54ea\u4e2a\u955c\u50cf\u3002<\/p>\n<p>\u73b0\u5728\u6253\u5f00\u6d4f\u89c8\u5668\u8bbf\u95ee<em>http:\/\/localhost<\/em>\uff0c\u60a8\u5e94\u8be5\u4f1a\u770b\u5230\u4e00\u4e2a Nginx Web \u670d\u52a1\u5668\u63d0\u4f9b\u5176\u9ed8\u8ba4\u7f51\u9875\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9222\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/08\/ubuntu-20.04.2-2021-08-29-20-22-35.png\" alt=\"\" width=\"1920\" height=\"1080\" \/><\/p>\n<p>\u5c31\u662f\u8fd9\u6837\u3002\u4f60\u6709\u4e00\u4e2a NGINX \u7684\u5de5\u4f5c\u5b9e\u4f8b\uff0c\u901a\u8fc7 Docker \u5bb9\u5668\u3002<\/p>\n<h3 id=\"h-list-docker-containers\">\u5217\u51fa Docker \u5bb9\u5668<\/h3>\n<p>\u8981\u5217\u51fa<strong>\u6240\u6709\u6b63\u5728\u8fd0\u884c\u7684<\/strong>Docker \u5bb9\u5668\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ sudo docker container ls -all<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-9221 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/08\/docker-run-nginx-3.png\" alt=\"\" width=\"1290\" height=\"251\" \/><\/p>\n<p>\u6839\u636e\u4e0a\u9762\u7684\u8f93\u51fa\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u7684\u5176\u4e2d\u4e00\u79cd\u505c\u6b62\u6211\u4eec\u7684 Nginx Docker \u5bb9\u5668\uff1a<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ docker stop f204ea685629<br \/>\nf204ea685629<br \/>\nlinuxmi@linuxmi:~\/www.linuxmi.com$ docker stop my-nginx-server<br \/>\nmy-nginx-server<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-9225 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/08\/docker-run-nginx-5.png\" alt=\"\" width=\"1026\" height=\"201\" \/><\/p>\n<p>\u8981\u67e5\u770b\u6240\u6709\u5bb9\u5668\uff0c<strong>\u751a\u81f3\u90a3\u4e9b\u672a\u8fd0\u884c\u7684<\/strong>\u5bb9\u5668\uff0c\u60a8\u9700\u8981\u6dfb\u52a0\u00a0<code>-a<\/code>\u00a0\u6807\u5fd7\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ sudo docker container ls -a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9224\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/08\/docker-run-nginx-4.png\" alt=\"\" width=\"1026\" height=\"326\" \/><\/p>\n<h2 id=\"h-sharing-data-between-the-nginx-docker-container-and-the-host\">\u5728 Nginx Docker \u5bb9\u5668\u548c\u4e3b\u673a\u4e4b\u95f4\u5171\u4eab\u6570\u636e<\/h2>\n<p><strong>Docker \u5bb9\u5668\u662f\u77ed\u6682\u7684\u3002<\/strong>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5728\u5bb9\u5668\u5185\u521b\u5efa\u7684\u4efb\u4f55\u6570\u636e\u4ec5\u5728\u5bb9\u5668\u5185\u4e14\u4ec5\u5728\u5bb9\u5668\u8fd0\u884c\u65f6\u53ef\u7528\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u8ba9\u6211\u4eec\u5411\u60a8\u5c55\u793a\u5982\u4f55\u5728\u4e3b\u673a\u4e0a\u8bbf\u95ee\u5bb9\u5668\u5185\u90e8\u7684\u6570\u636e\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 HTML \u6587\u4ef6\uff0c\u5c06\u5176\u6258\u7ba1\u5728\u5bb9\u5668\u4e2d\uff0c\u5e76\u4f7f\u7528 Nginx Docker \u5bb9\u5668\u5c06\u5176\u63d0\u4f9b\u7ed9\u5916\u90e8\u3002<strong>\u6b64\u8bbe\u7f6e\u5141\u8bb8\u6211\u4eec\u62e5\u6709\u5728\u5bb9\u5668\u5916\u90e8\u6258\u7ba1\u7684\u6301\u4e45\u7f51\u7ad9\u5185\u5bb9\u3002<\/strong><\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Docker \u4e2d\u7684<strong>\u7ed1\u5b9a\u6302\u8f7d<\/strong>\u529f\u80fd\u3002<strong>\u4f7f\u7528\u7ed1\u5b9a\u6302\u8f7d\u65f6\uff0c\u4e3b\u673a\u4e0a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u4f1a\u6302\u8f7d\u5230\u5bb9\u5668\u4e2d\u3002<\/strong><\/p>\n<p>\u56e0\u6b64\uff0c\u8ba9\u6211\u4eec\u9996\u5148\u5728\u4e3b\u76ee\u5f55\u4e2d\u4e3a\u60a8\u7684\u7f51\u7ad9\u5185\u5bb9\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ mkdir ~\/www<\/p>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 HTML \u6587\u4ef6\u5e76\u5728\u60a8\u7684\u7d22\u5f15\u9875\u9762\u4e0a\u653e\u7f6e\u4e00\u4e9b\u6587\u672c\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ vim ~\/www\/index.html<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9226\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/08\/linuxmi.png\" alt=\"\" width=\"1026\" height=\"801\" \/><\/p>\n<p>\u7f16\u5199HTML\u4ee3\u7801\u7247\u6bb5\u5230\u91cc\u9762\u3002\u4fdd\u5b58\u6587\u4ef6\u5e76\u9000\u51fa Vim\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5728\u5bb9\u5668\u4e0a\u8fd0\u884c\u5e26\u6709\u9644\u52a0\u5377\u7684 Nginx Docker \u5bb9\u5668<code>\/usr\/share\/nginx\/html<\/code>\u5230\u5f53\u524d\u4fdd\u5b58\u6587\u4ef6\u7684\u4e3b\u673a<code>www<\/code>\u76ee\u5f55\u4e0a<code>index.html<\/code>\u3002<\/p>\n<p>linuxmi@linuxmi:~\/www.linuxmi.com$ docker run -d -p <span class=\"token number\">80<\/span>:80 -v ~\/www:\/usr\/share\/nginx\/html\/ &#8211;name my-nginx-server nginx<\/p>\n<p>Nginx \u5bb9\u5668\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a\u5728\u00a0<code>\/usr\/share\/nginx\/html\/<\/code>.\u00a0\u5728\u4e0a\u9762\u7684\u547d\u4ee4\u4e2d\uff0c\u8be5<code>-v<\/code>\u00a0\u9009\u9879\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u7ed1\u5b9a\u6302\u8f7d\u5377\uff0c\u5b83\u5c06\u00a0Nginx Docker \u5bb9\u5668\u5185\u90e8\u7684\u76ee\u5f55\u94fe\u63a5\u00a0\u5230\u00a0\u00a0\u4e3b\u673a\u4e0a\u7684\u76ee\u5f55\u3002<code>\/usr\/share\/nginx\/html\/<\/code><code>~\/www<\/code><\/p>\n<p>Docker \u4f7f\u7528 a\u00a0<code>:<\/code>\u00a0\u5c06\u4e3b\u673a\u8def\u5f84\u4e0e\u5bb9\u5668\u8def\u5f84\u5206\u5f00\u3002<strong>\u4e3b\u673a\u8def\u5f84\u59cb\u7ec8\u6392\u5728\u7b2c\u4e00\u4f4d\u3002<\/strong><\/p>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u60a8\u6d4f\u89c8<em>http:\/\/localhost<\/em>\uff0c\u60a8\u5c06\u5728\u663e\u793a\u7a97\u53e3\u4e0a\u770b\u5230\u4e0b\u9762\u7ed9\u51fa\u7684\u8f93\u51fa\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9228\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/08\/ubuntu-20.04.2-2021-08-29-21-13-12.png\" alt=\"\" width=\"1920\" height=\"1080\" \/><\/p>\n<p>\u5c31\u8fd9\u6837\u3002\u60a8\u73b0\u5728\u6709\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684 Nginx Docker \u5bb9\u5668\uff0c\u5b83\u4e3a\u81ea\u5b9a\u4e49\u7f51\u9875\u63d0\u4f9b\u670d\u52a1\u3002<\/p>\n<h2 id=\"h-conclucsion\">\u7ed3\u8bba<\/h2>\n<p>Nginx \u548c Docker \u53ef\u4ee5\u5f88\u597d\u5730\u534f\u540c\u5de5\u4f5c\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u6f14\u793a\u4e86\u5982\u4f55\u8bbe\u7f6e\u548c\u4f7f\u7528 Nginx Docker \u5bb9\u5668\u3002\u73b0\u5728\uff0c\u60a8\u8fd8\u77e5\u9053\u5982\u4f55\u5728\u5bb9\u5668\u548c\u4e3b\u673a\u6587\u4ef6\u7cfb\u7edf\u4e4b\u95f4\u5171\u4eab\u4fe1\u606f\u3002<\/p>\n<p>\u5982\u679c\u8fd8\u6709\u4ec0\u4e48\u7591\u95ee\uff0c\u8bf7\u5728\u4e0b\u9762\u7684\u8bc4\u8bba\u680f\u91cc\u7ed9\u6211\u4eec\u7559\u8a00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u5c06 Nginx \u4f5c\u4e3a Docker \u5bb9\u5668\u8fd0\u884c\uff0c\u5e76\u5c06\u5176\u516c\u5f00\u7ed9\u60a8\u7684\u672c\u5730\u7f51\u7edc\u3002\u6b64\u5916 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-9218","post","type-post","status-publish","format-standard","hentry","category-zixun"],"aioseo_notices":[],"views":1406,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/9218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=9218"}],"version-history":[{"count":4,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/9218\/revisions"}],"predecessor-version":[{"id":11636,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/9218\/revisions\/11636"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=9218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=9218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=9218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}