{"id":1328,"date":"2021-08-03T17:34:28","date_gmt":"2021-08-03T09:34:28","guid":{"rendered":"https:\/\/www.siediyer.cn\/?p=1328"},"modified":"2021-08-03T17:34:28","modified_gmt":"2021-08-03T09:34:28","slug":"docker%e5%ae%b9%e5%99%a8%e7%bb%91%e5%ae%9a%e5%a4%96%e9%83%a8ip%e5%92%8c%e7%ab%af%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=1328","title":{"rendered":"Docker\u5bb9\u5668\u7ed1\u5b9a\u5916\u90e8IP\u548c\u7aef\u53e3"},"content":{"rendered":"<p>Docker\u5141\u8bb8\u901a\u8fc7\u5916\u90e8\u8bbf\u95ee\u5bb9\u5668\u6216\u8005\u5bb9\u5668\u4e4b\u95f4\u4e92\u8054\u7684\u65b9\u5f0f\u6765\u63d0\u4f9b\u7f51\u7edc\u670d\u52a1\u3002<\/p>\n<p>\u4ee5\u4e0b\u64cd\u4f5c\u901a\u8fc7myfirstapp\u955c\u50cf\u6a21\u62df\uff0c<a href=\"http:\/\/www.cnblogs.com\/linjiqin\/p\/8669360.html\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>\u5982\u4f55\u5236\u4f5cmyfirstapp\u955c\u50cf\u8bf7\u70b9\u51fb\u6b64\u5904<\/strong><\/a>\u3002<\/p>\n<p><strong>1\u3001\u5916\u90e8\u8bbf\u95ee\u5bb9\u5668<\/strong><br \/>\n\u5bb9\u5668\u542f\u52a8\u4e4b\u540e\uff0c\u5bb9\u5668\u4e2d\u53ef\u4ee5\u8fd0\u884c\u4e00\u4e9b\u7f51\u7edc\u5e94\u7528\uff0c\u901a\u8fc7-p\u6216-P\u53c2\u6570\u6765\u6307\u5b9a\u7aef\u53e3\u6620\u5c04\u3002<br \/>\na\u3001\u7528-P\uff08\u5927\u5199\uff09\u6807\u8bb0\u65f6\uff0cdocker\u4f1a\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u7aef\u53e3\u6620\u5c04\u5230\u5bb9\u5668\u5185\u90e8\u5f00\u653e\u7684\u7f51\u7edc\u7aef\u53e3\u4e0a\u3002<\/p>\n<pre class=\"lang:default decode:true \">$ docker run -d -P myfirstapp python app.py \r\n$ docker ps\r\nCONTAINER ID  IMAGE       COMMAND  CREATED      STATUS                           PORTS             NAMES\r\ndbcf5d3290ba  myfirstapp  \"python app.py\"     4 seconds ago  Up 4 seconds  0.0.0.0:32770-&gt;80\/tcp   quizzical_engelbart<\/pre>\n<p>\u6b64\u65f6\u8bbf\u95ee\u672c\u673a\u768432770\u7aef\u53e3\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5230\u5bb9\u5668\u5185web\u5e94\u7528\u63d0\u4f9b\u7684\u754c\u9762\u3002<\/p>\n<pre class=\"lang:default decode:true \">$ curl http:\/\/192.168.1.160:32770\/\r\n&lt;h3&gt;Hello HELLO!&lt;\/h3&gt;&lt;b&gt;Hostname:&lt;\/b&gt; dbcf5d3290ba&lt;br\/&gt;&lt;b&gt;Visits:&lt;\/b&gt; &lt;i&gt;cannot connect to Redis, counter disabled&lt;\/i&gt;<\/pre>\n<pre class=\"lang:default decode:true \">\u4e5f\u53ef\u4ee5\u4f7f\u7528docker logs\u6765\u67e5\u770b\u5e94\u7528\u7684\u4fe1\u606f<\/pre>\n<pre class=\"lang:default decode:true \">$ docker logs dbcf5d3290ba\r\n* Running on http:\/\/0.0.0.0:80\/ (Press CTRL+C to quit)\r\n192.168.1.5 - - [29\/Mar\/2018 06:17:34] \"GET \/ HTTP\/1.1\" 200 -\r\n192.168.1.5 - - [29\/Mar\/2018 06:17:34] \"GET \/favicon.ico HTTP\/1.1\" 404 -\r\n192.168.1.160 - - [29\/Mar\/2018 06:17:43] \"GET \/ HTTP\/1.1\" 200 -\r\n192.168.1.5 - - [29\/Mar\/2018 06:18:59] \"GET \/ HTTP\/1.1\" 200 -<\/pre>\n<p>b\u3001\u4f7f\u7528-p\uff08\u5c0f\u5199)\u6807\u8bb0\u65f6\u5219\u53ef\u4ee5\u6307\u5b9a\u8981\u6620\u5c04\u7684\u7aef\u53e3\uff0c\u5e76\u4e14\u5728\u4e00\u4e2a\u6307\u5b9a\u7aef\u53e3\u4e0a\u53ea\u53ef\u4ee5\u7ed1\u5b9a\u4e00\u4e2a\u5bb9\u5668\uff0c\u652f\u6301\u7684\u683c\u5f0f\u6709\uff1a<\/p>\n<pre class=\"lang:default decode:true \">ip:hostport:containerport \r\nip::containerport \r\nhostport:containerport\r\n\r\n$ docker run -d -p 4000:80 myfirstapp python app.py\r\n$ docker run -d -p 4001:80 myfirstapp python app.py\r\n$ curl http:\/\/192.168.1.160:4000\/\r\n&lt;h3&gt;Hello HELLO!&lt;\/h3&gt;&lt;b&gt;Hostname:&lt;\/b&gt; f43ed2810353&lt;br\/&gt;&lt;b&gt;Visits:&lt;\/b&gt; &lt;i&gt;cannot connect to Redis, counter disabled&lt;\/i&gt; \r\n$ curl http:\/\/192.168.1.160:4001\/\r\n&lt;h3&gt;Hello HELLO!&lt;\/h3&gt;&lt;b&gt;Hostname:&lt;\/b&gt; f43ed2810353&lt;br\/&gt;&lt;b&gt;Visits:&lt;\/b&gt; &lt;i&gt;cannot connect to Redis, counter disabled&lt;\/i&gt;<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u672c\u57304000\u7aef\u53e3\u5df2\u7ecf\u88ab\u6620\u5c04\uff0c\u6362\u4e00\u4e2a\u7aef\u53e34001\u4e5f\u88ab\u6620\u5c04\u3002<\/p>\n<p>docker\u9ed8\u8ba4\u4f1a\u6620\u5c04\u672c\u5730\u6240\u6709\u7684\u5730\u5740\u3002\u73b0\u5728\u6211\u4eec\u6765\u5c1d\u8bd5\u4e00\u4e0b\u6620\u5c04\u5230\u6307\u5b9a\u5730\u5740\u7684\u6307\u5b9a\u7aef\u53e3<\/p>\n<pre class=\"lang:default decode:true \">$ docker run -d -p 127.0.0.1:4000:80 myfirstapp python app.py\r\n$ curl http:\/\/127.0.0.1:4000\/ ##\u8bbf\u95ee\u901a\u8fc7\r\n$ curl http:\/\/localhost:4000\/ ##\u8bbf\u95ee\u901a\u8fc7\r\n$ curl http:\/\/192.168.1.160:4000\/ ##\u8bbf\u95ee\u62d2\u7edd\r\ncurl: (7) Failed connect to 192.168.1.160:4000; \u62d2\u7edd\u8fde\u63a5<\/pre>\n<p>\u7136\u540e\u8981\u8bbf\u95ee\u5bb9\u5668\u4e2d\u7684\u5e94\u7528\u53ea\u80fd\u901a\u8fc7127.0.0.1\u8fd9\u4e2aip\u8bbf\u95ee\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u662f\u7ed1\u5b9a\u672c\u673a\u7684\u4efb\u610f\u7aef\u53e3\u5230\u5bb9\u5668\u768480\u7aef\u53e3\uff0c\u968f\u673a\u5206\u914d\u4e00\u4e2a\u7aef\u53e3<\/p>\n<pre class=\"lang:default decode:true \">$ docker run -d -p 127.0.0.1::80 myfirstapp python app.py\r\n$ docker ps ##127.0.0.1:32770-&gt;80\/tcp\r\n$ curl http:\/\/127.0.0.1:32770\/ ##\u8bbf\u95ee\u901a\u8fc7\r\n$ curl http:\/\/localhost:32770\/ ##\u8bbf\u95ee\u901a\u8fc7\r\n$ curl http:\/\/192.168.1.160:32770\/ ##\u8bbf\u95ee\u62d2\u7edd<\/pre>\n<p>\u8fd8\u53ef\u4ee5\u6307\u5b9a\u901a\u4fe1\u534f\u8bae<\/p>\n<pre class=\"lang:default decode:true \">docker run -d -p 5003:80\/udp myfirstapp python app.py<\/pre>\n<p><strong>2.\u67e5\u770b\u5bb9\u5668\u7ed1\u5b9a\u548c\u6620\u5c04\u7684\u7aef\u53e3\u53caIp\u5730\u5740<\/strong><\/p>\n<pre class=\"lang:default decode:true \">$ docker port 44de1b0b5312(\u5bb9\u5668ID)\r\n80\/tcp -&gt; 127.0.0.1:32770<\/pre>\n<p><strong>3.\u6ce8\u610f\u4e8b\u9879<\/strong><br \/>\na.docker inspect\u67e5\u770b\u5bb9\u5668\u81ea\u5df1\u7684\u5185\u90e8\u7f51\u7edc\u548cip\u5730\u5740<\/p>\n<pre class=\"lang:default decode:true \">$ docker inspect 44de1b0b5312(\u5bb9\u5668ID)<\/pre>\n<p>b.\u5bb9\u5668\u542f\u52a8\u7ed1\u5b9a\u591aIP<\/p>\n<pre class=\"lang:default decode:true \">$ docker run -d -p 5005:5000 -p 5006:80 myfirstapp python app.py #\u5bb9\u5668ID:44e703c1279a\r\n$ docker port 44e703c1279a\r\n5000\/tcp -&gt; 0.0.0.0:5005\r\n80\/tcp -&gt; 0.0.0.0:5006<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker\u5141\u8bb8\u901a\u8fc7\u5916\u90e8\u8bbf\u95ee\u5bb9\u5668\u6216\u8005\u5bb9\u5668\u4e4b\u95f4\u4e92\u8054\u7684\u65b9\u5f0f\u6765\u63d0\u4f9b\u7f51\u7edc\u670d\u52a1\u3002 \u4ee5\u4e0b\u64cd\u4f5c\u901a\u8fc7myfirstapp\u955c\u50cf\u6a21 [&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":[79],"class_list":["post-1328","post","type-post","status-publish","format-standard","hentry","category-linux","tag-docker"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1328"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1328\/revisions"}],"predecessor-version":[{"id":1329,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1328\/revisions\/1329"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}