{"id":8951,"date":"2022-12-02T16:34:20","date_gmt":"2022-12-02T08:34:20","guid":{"rendered":"http:\/\/123.57.164.21\/?p=8951"},"modified":"2022-12-02T16:34:20","modified_gmt":"2022-12-02T08:34:20","slug":"docker%e7%bd%91%e7%bb%9c","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=8951","title":{"rendered":"Docker\u7f51\u7edc"},"content":{"rendered":"\n<p><strong>1. \u7b80\u4ecb<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5bb9\u5668\u7f51\u7edc\u5b9e\u8d28\u4e0a\u662f\u7531 Dokcer \u4e3a\u5e94\u7528\u7a0b\u5e8f\u6240\u521b\u9020\u7684\u865a\u62df\u73af\u5883\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u80fd\u8ba9\u5e94\u7528\u4ece\u5bbf\u4e3b\u673a\u64cd\u4f5c\u7cfb\u7edf\u7684\u7f51\u7edc\u73af\u5883\u4e2d\u72ec\u7acb\u51fa\u6765\uff0c\u5f62\u6210\u5bb9\u5668\u81ea\u6709\u7684\u7f51\u7edc\u8bbe\u5907\u3001IP \u534f\u8bae\u6808\u3001\u7aef\u53e3\u5957\u63a5\u5b57\u3001IP \u8def\u7531\u8868\u3001\u9632\u706b\u5899\u7b49\u7b49\u4e0e\u7f51\u7edc\u76f8\u5173\u7684\u6a21\u5757\u3002<\/p>\n\n\n\n<p>Docker \u4e3a\u5b9e\u73b0\u5bb9\u5668\u7f51\u7edc\uff0c\u4e3b\u8981\u91c7\u7528\u7684\u67b6\u6784\u7531\u4e09\u90e8\u5206\u7ec4\u6210\uff1aCNM\u3001Libnetwork \u548c\u9a71\u52a8\u3002<\/p>\n\n\n\n<p><strong>1.1 CNM<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=Container&amp;spm=1001.2101.3001.7020\" target=\"_blank\">Container<\/a> Network Model\uff0c\u5b83\u662f Docker \u7f51\u7edc\u67b6\u6784\u91c7\u7528\u7684\u8bbe\u8ba1\u89c4\u8303\u3002\u53ea\u8981\u7b26\u5408\u8be5\u6a21\u578b\u7684\u7f51\u7edc\u63a5\u53e3\u5c31\u80fd\u88ab\u7528\u4e8e\u5bb9\u5668\u4e4b\u95f4\u901a\u4fe1\uff0c\u800c\u901a\u4fe1\u7684\u8fc7\u7a0b\u548c\u7ec6\u8282\u53ef\u4ee5\u5b8c\u5168\u7531\u7f51\u7edc\u63a5\u53e3\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-26-1024x633.png\" alt=\"\" class=\"wp-image-8952\" width=\"482\" height=\"298\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-26-1024x633.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-26-300x185.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-26-768x475.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-26-830x513.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-26-230x142.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-26-350x216.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-26-480x297.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-26.png 1094w\" sizes=\"(max-width: 482px) 100vw, 482px\" \/><\/figure><\/div>\n\n\n\n<p>CNM \u7684\u7f51\u7edc\u7ec4\u6210:<\/p>\n\n\n\n<ul><li>Sandbox: \u63d0\u4f9b\u5bb9\u5668\u7684\u865a\u62df\u7f51\u7edc\u6808\uff0c\u5373\u7aef\u53e3\u5957\u63a5\u5b57\uff0cIP\u8def\u7531\u8868\u3001iptables\u914d\u7f6e\uff0cDNS\u7b49\u3002\u7528\u4e8e\u9694\u79bb\u5bb9\u5668\u7f51\u7edc\u548c\u5bbf\u4e3b\u673a\u7f51\u7edc<\/li><li>Network: Docker \u865a\u62df\u7f51\u7edc\uff0c\u4e0e\u5bbf\u4e3b\u673a\u7f51\u7edc\u9694\u79bb\uff0c\u53ea\u6709\u53c2\u4e0e\u8005\u80fd\u591f\u901a\u4fe1<\/li><li>Endpoint: \u5bb9\u5668\u5185\u7684\u865a\u62df\u7f51\u7edc\u63a5\u53e3\uff0c\u8d1f\u8d23\u4e0eDocker\u865a\u62df\u7f51\u7edc\u8fde\u63a5<br><\/li><\/ul>\n\n\n\n<p><strong>1.2 Libnetwork<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>CNM \u7684\u6807\u51c6\u5b9e\u73b0\uff0c\u7531Golang\u5f00\u53d1\uff0c\u5b83\u5b9e\u73b0\u4e86CNM\u5b9a\u4e49\u7684\u5168\u90e8\u4e09\u4e2a\u7ec4\u4ef6\uff0c\u8fd8\u5b9e\u73b0\u4e86\u672c\u5730\u670d\u52a1\u53d1\u73b0\uff0c\u57fa\u4e8e Ingress \u7684\u5bb9\u5668\u8d1f\u8f7d\u5747\u8861\uff0c\u53ca\u7f51\u7edc\u63a7\u5236\u5c42\u548c\u7ba1\u7406\u5c42\u529f\u80fd<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-27-1024x541.png\" alt=\"\" class=\"wp-image-8953\" width=\"535\" height=\"282\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-27-1024x541.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-27-300x158.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-27-768x406.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-27-830x438.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-27-230x121.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-27-350x185.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-27-480x253.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-27.png 1178w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/figure><\/div>\n\n\n\n<p><strong>1.3 \u652f\u6491\u6280\u672f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>network namespace\uff1a\u7528\u4e8e\u9694\u79bb\u5bb9\u5668\u7f51\u7edc\u8d44\u6e90\uff08IP\u3001\u7f51\u5361\u3001\u8def\u7531\u7b49\uff09\u3002netns\u53ef\u786e\u4fdd\u540c\u4e00\u4e3b\u673a\u4e0a\u7684\u4e24\u4e2a\u5bb9\u5668\u65e0\u6cd5\u76f8\u4e92\u901a\u4fe1\uff0c\u751a\u81f3\u4e0d\u80fd\u4e0e\u4e3b\u673a\u672c\u8eab\u8fdb\u884c\u901a\u4fe1\uff0c\u9664\u975e\u914d\u7f6e\u4e3a\u901a\u8fc7docker\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\u3002CNM\u7f51\u7edc\u9a71\u52a8\u7a0b\u5e8f\u4e3a\u6bcf\u4e2a\u5bb9\u5668\u5b9e\u73b0\u5355\u72ec\u7684netns\u3002\u4f46\u662f\uff0c\u5bb9\u5668\u53ef\u4ee5\u5171\u4eab\u76f8\u540c\u7684netns\uff0c\u751a\u81f3\u53ef\u4ee5\u662f\u4e3b\u673a\u7684netns\u7684\u4e00\u90e8\u5206\u3002<\/li><li>veth pair\uff1a\u7528\u4e8e\u4e0d\u540c netns \u95f4\u8fdb\u884c\u901a\u4fe1\u3002veth\u662f\u5168\u53cc\u5de5\u94fe\u63a5\uff0c\u5728\u6bcf\u4e2a\u540d\u79f0\u7a7a\u95f4\u4e2d\u90fd\u6709\u4e00\u4e2a\u63a5\u53e3\uff0c\u5145\u5f53\u4e24\u4e2a\u7f51\u7edc\u540d\u79f0\u7a7a\u95f4\u4e4b\u95f4\u7684\u8fde\u63a5\u7ebf\uff0c\u8d1f\u8d23\u5c06\u4e00\u4e2a netns \u6570\u636e\u53d1\u5f80\u53e6\u4e00\u4e2a netns \u7684 veth\u3002\u5982\u5f53\u5bb9\u5668\u8fde\u63a5\u5230docker\u7f51\u7edc\u65f6\uff0cveth\u7684\u4e00\u7aef\u653e\u7f6e\u5728\u5bb9\u5668\u5185\u90e8\uff08\u901a\u5e38\u89c6\u4e3aethX\u63a5\u53e3\uff09\uff0c\u800c\u53e6\u4e00\u7aef\u8fde\u63a5\u5230Docker\u7f51\u7edc(vethxxx)\u3002<\/li><li>iptables\uff1a\u5305\u8fc7\u6ee4\uff0c\u7aef\u53e3\u6620\u5c04\u548c\u8d1f\u8f7d\u5747\u8861<br><\/li><\/ul>\n\n\n\n<p><strong>1.4 Docker \u7f51\u7edc\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9996\u5148\uff0c\u8981\u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1\uff0c\u673a\u5668\u9700\u8981\u81f3\u5c11\u4e00\u4e2a\u7f51\u7edc\u63a5\u53e3\uff08\u7269\u7406\u63a5\u53e3\u6216\u865a\u62df\u63a5\u53e3\uff09\u6765\u6536\u53d1\u6570\u636e\u5305\uff1b\u6b64\u5916\uff0c\u5982\u679c\u4e0d\u540c\u5b50\u7f51\u4e4b\u95f4\u8981\u8fdb\u884c\u901a\u4fe1\uff0c\u9700\u8981\u8def\u7531\u673a\u5236\u3002<\/p>\n\n\n\n<p>Docker \u4e2d\u7684\u7f51\u7edc\u63a5\u53e3\u9ed8\u8ba4\u90fd\u662f\u865a\u62df\u7684\u63a5\u53e3\u3002\u865a\u62df\u63a5\u53e3\u7684\u4f18\u52bf\u4e4b\u4e00\u662f\u8f6c\u53d1\u6548\u7387\u8f83\u9ad8\u3002 Linux \u901a\u8fc7\u5728\u5185\u6838\u4e2d\u8fdb\u884c\u6570\u636e\u590d\u5236\u6765\u5b9e\u73b0\u865a\u62df\u63a5\u53e3\u4e4b\u95f4\u7684\u6570\u636e\u8f6c\u53d1\uff0c\u53d1\u9001\u63a5\u53e3\u7684\u53d1\u9001\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u5305\u88ab\u76f4\u63a5\u590d\u5236\u5230\u63a5\u6536\u63a5\u53e3\u7684\u63a5\u6536\u7f13\u5b58\u4e2d\u3002\u5bf9\u4e8e\u672c\u5730\u7cfb\u7edf\u548c\u5bb9\u5668\u5185\u7cfb\u7edf\u770b\u6765\u5c31\u50cf\u662f\u4e00\u4e2a\u6b63\u5e38\u7684\u4ee5\u592a\u7f51\u5361\uff0c\u53ea\u662f\u5b83\u4e0d\u9700\u8981\u771f\u6b63\u540c\u5916\u90e8\u7f51\u7edc\u8bbe\u5907\u901a\u4fe1\uff0c\u901f\u5ea6\u8981\u5feb\u5f88\u591a\u3002<\/p>\n\n\n\n<p>Docker \u5bb9\u5668\u7f51\u7edc\u5c31\u5229\u7528\u4e86\u8fd9\u9879\u6280\u672f\u3002\u5b83\u5728\u672c\u5730\u4e3b\u673a\u548c\u5bb9\u5668\u5185\u5206\u522b\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u63a5\u53e3\uff0c\u5e76\u8ba9\u5b83\u4eec\u5f7c\u6b64\u8fde\u901a\uff08\u8fd9\u6837\u7684\u4e00\u5bf9\u63a5\u53e3\u53eb\u505a veth pair\uff09\u3002<\/p>\n\n\n\n<p>Docker \u521b\u5efa\u4e00\u4e2a\u5bb9\u5668\u7684\u65f6\u5019\uff0c\u4f1a\u6267\u884c\u5982\u4e0b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1.\u521b\u5efa\u4e00\u5bf9\u865a\u62df\u63a5\u53e3\uff0c\u5206\u522b\u653e\u5230\u672c\u5730\u4e3b\u673a\u548c\u65b0\u5bb9\u5668\u4e2d\uff1b \n2.\u672c\u5730\u4e3b\u673a\u4e00\u7aef\u6865\u63a5\u5230\u9ed8\u8ba4\u7684 docker0 \u6216\u6307\u5b9a\u7f51\u6865\u4e0a\uff0c\u5e76\u5177\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u540d\u5b57\uff0c\u5982 veth65f9\uff1b \n3.\u5bb9\u5668\u4e00\u7aef\u653e\u5230\u65b0\u5bb9\u5668\u4e2d\uff0c\u5e76\u4fee\u6539\u540d\u5b57\u4f5c\u4e3a eth0\uff0c\u8fd9\u4e2a\u63a5\u53e3\u53ea\u5728\u5bb9\u5668\u7684\u540d\u79f0\u7a7a\u95f4\u53ef\u89c1\uff1b \u4ece\u7f51\u6865\u53ef\u7528\u5730\u5740\u6bb5\u4e2d\u83b7\u53d6\u4e00\u4e2a\u7a7a\u95f2\u5730\u5740\u5206\u914d\u7ed9\u5bb9\u5668\u7684 eth0\uff0c\u5e76\u914d\u7f6e\u9ed8\u8ba4\u8def\u7531\u5230\u6865\u63a5\u7f51\u5361 veth65f9\u3002\n4.\u5b8c\u6210\u8fd9\u4e9b\u4e4b\u540e\uff0c\u5bb9\u5668\u5c31\u53ef\u4ee5\u4f7f\u7528 eth0 \u865a\u62df\u7f51\u5361\u6765\u8fde\u63a5\u5176\u4ed6\u5bb9\u5668\u548c\u5176\u4ed6\u7f51\u7edc\u3002<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u5728 docker run \u7684\u65f6\u5019\u901a\u8fc7 &#8211;net \u53c2\u6570\u6765\u6307\u5b9a\u5bb9\u5668\u7684\u7f51\u7edc\u914d\u7f6e\uff0c\u67094\u4e2a\u53ef\u9009\u503c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1.--net=bridge\uff1a\u8fd9\u4e2a\u662f\u9ed8\u8ba4\u503c\uff0c\u8fde\u63a5\u5230\u9ed8\u8ba4\u7684\u7f51\u6865\u3002\n2.--net=host\uff1a\u544a\u8bc9 Docker \u4e0d\u8981\u5c06\u5bb9\u5668\u7f51\u7edc\u653e\u5230\u9694\u79bb\u7684\u540d\u5b57\u7a7a\u95f4\u4e2d\uff0c\u5373\u4e0d\u8981\u5bb9\u5668\u5316\u5bb9\u5668\u5185\u7684\u7f51\u7edc\u3002\u6b64\u65f6\u5bb9\u5668\u4f7f\u7528\u672c\u5730\u4e3b\u673a\u7684\u7f51\u7edc\uff0c\u5b83\u62e5\u6709\u5b8c\u5168\u7684\u672c\u5730\u4e3b\u673a\u63a5\u53e3\u8bbf\u95ee\u6743\u9650\u3002\u5bb9\u5668\u8fdb\u7a0b\u53ef\u4ee5\u8ddf\u4e3b\u673a\u5176 \u5b83 root \u8fdb\u7a0b\u4e00\u6837\u53ef\u4ee5\u6253\u5f00\u4f4e\u8303\u56f4\u7684\u7aef\u53e3\uff0c\u53ef\u4ee5\u8bbf\u95ee\u672c\u5730\u7f51\u7edc\u670d\u52a1\u6bd4\u5982 D-bus\uff0c\u8fd8\u53ef\u4ee5\u8ba9\u5bb9\u5668\u505a\u4e00\u4e9b\u5f71\u54cd\u6574\u4e2a\u4e3b\u673a\u7cfb\u7edf\u7684\u4e8b\u60c5\uff0c\u6bd4\u5982\u91cd\u542f\u4e3b\u673a\u3002\u56e0\u6b64\u4f7f\u7528\u8fd9\u4e2a\u9009\u9879\u7684\u65f6\u5019\u8981\u975e\u5e38\u5c0f\u5fc3\u3002\u5982\u679c\u8fdb\u4e00\u6b65\u7684\u4f7f\u7528 --privileged=true\uff0c\u5bb9\u5668\u4f1a\u88ab\u5141\u8bb8\u76f4\u63a5\u914d\u7f6e\u4e3b\u673a\u7684\u7f51\u7edc\u5806\u6808\u3002\n3.--net=container:NAME_or_ID\uff1a\u8ba9 Docker \u5c06\u65b0\u5efa\u5bb9\u5668\u7684\u8fdb\u7a0b\u653e\u5230\u4e00\u4e2a\u5df2\u5b58\u5728\u5bb9\u5668\u7684\u7f51\u7edc\u6808\u4e2d\uff0c\u65b0\u5bb9\u5668\u8fdb\u7a0b\u6709\u81ea\u5df1\u7684\u6587\u4ef6\u7cfb\u7edf\u3001\u8fdb\u7a0b\u5217\u8868\u548c\u8d44\u6e90\u9650\u5236\uff0c\u4f46\u4f1a\u548c\u5df2\u5b58\u5728\u7684\u5bb9\u5668\u5171\u4eab IP \u5730\u5740\u548c\u7aef\u53e3\u7b49\u7f51\u7edc\u8d44\u6e90\uff0c\u4e24\u8005\u8fdb\u7a0b\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 lo \u73af\u56de\u63a5\u53e3\u901a\u4fe1\u3002\n4.--net=none\uff1a\u8ba9 Docker \u5c06\u65b0\u5bb9\u5668\u653e\u5230\u9694\u79bb\u7684\u7f51\u7edc\u6808\u4e2d\uff0c\u4f46\u662f\u4e0d\u8fdb\u884c\u7f51\u7edc\u914d\u7f6e\u3002\u4e4b\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u81ea\u5df1\u8fdb\u884c\u914d\u7f6e\u3002<\/pre>\n\n\n\n<p><strong>2. \u56db\u79cd\u7f51\u7edc\u6a21\u5f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Docker \u5b89\u88c5\u540e\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\u4e09\u4e2a\u7f51\u7edc<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"147\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28-1024x147.png\" alt=\"\" class=\"wp-image-8954\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28-1024x147.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28-300x43.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28-768x110.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28-1536x221.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28-830x119.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28-230x33.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28-350x50.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28-480x69.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-28.png 1766w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29-1024x230.png\" alt=\"\" class=\"wp-image-8955\" width=\"605\" height=\"135\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29-1024x230.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29-300x67.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29-768x173.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29-1536x345.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29-830x187.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29-230x52.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29-350x79.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29-480x108.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-29.png 1770w\" sizes=\"(max-width: 605px) 100vw, 605px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2.1 bridge<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>bridge\u6a21\u5f0f\u662fdocker\u7684\u9ed8\u8ba4\u7f51\u7edc\u6a21\u5f0f\uff0c\u4e0d\u5199\u2013net\u53c2\u6570\uff0c\u5c31\u662fbridge\u6a21\u5f0f\u3002\u4f7f\u7528docker run -p\u65f6\uff0cdocker\u5b9e\u9645\u662f\u5728iptables\u505a\u4e86DNAT\u89c4\u5219\uff0c\u5b9e\u73b0\u7aef\u53e3\u8f6c\u53d1\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528iptables -t nat -vnL\u67e5\u770b\u3002bridge\u6a21\u5f0f\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-30.png\" alt=\"\" class=\"wp-image-8956\" width=\"414\" height=\"276\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-30.png 962w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-30-300x200.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-30-768x513.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-30-830x554.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-30-230x153.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-30-350x234.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-30-480x320.png 480w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/figure><\/div>\n\n\n\n<p>\u5f53Docker\u8fdb\u7a0b\u542f\u52a8\u65f6\uff0c\u4f1a\u5728\u4e3b\u673a\u4e0a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3adocker0\u7684\u865a\u62df\u7f51\u6865\uff0c\u6b64\u4e3b\u673a\u4e0a\u542f\u52a8\u7684Docker\u5bb9\u5668\u90fd\u4f1a\u8fde\u63a5\u5230\u8fd9\u4e2a\u865a\u62df\u7f51\u6865\u4e0a\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1.\u521b\u5efa\u865a\u62df\u7f51\u6865 docker0\uff0c\u65b0\u5efa\u7684\u5bb9\u5668\u4f1a\u81ea\u52a8\u6865\u63a5\u5230\u8be5\u63a5\u53e3\u4e0b\uff0c\u9644\u52a0\u5728\u5176\u4e0a\u7684\u4efb\u4f55\u7f51\u5361\u4e4b\u95f4\u90fd\u80fd\u81ea\u52a8\u8f6c\u53d1\u6570\u636e\u5305\u3002\n2.\u521b\u5efa\u4e00\u5bf9\u865a\u62df\u8bbe\u5907\u63a5\u53e3 veth pair\uff0c\u5c06\u5176\u4e2d\u4e00\u4e2a\u63a5\u53e3\u8bbe\u7f6e\u4e3a\u5bb9\u5668\u7684 eth0 \u63a5\u53e3\uff08\u5bb9\u5668\u7f51\u5361\uff09\uff0c\u53e6\u4e00\u4e2a\u63a5\u53e3\u653e\u7f6e\u5728\u5bbf\u4e3b\u673a\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u4ee5 vethxxx \u8fd9\u6837\u7684\u540d\u5b57\u547d\u540d\uff0c\u5bbf\u4e3b\u673a\u4e0a\u76843.\u6240\u6709\u5bb9\u5668\u90fd\u8fde\u63a5\u5230\u8fd9\u4e2a\u5185\u90e8\u7f51\u7edc\u4e0a\n4.\u5206\u914d\u4e00\u4e2a\u548c\u7f51\u6865 docker0\u5728\u540c\u7f51\u6bb5\u7684IP\u5730\u5740\u7ed9\u5bb9\u5668\uff0c\u5e76\u8bbe\u7f6e docker0 \u7684 IP \u5730\u5740\u4e3a\u5bb9\u5668\u7684\u9ed8\u8ba4\u7f51\u5173<\/pre>\n\n\n\n<p>Docker \u5bb9\u5668\u9ed8\u8ba4\u4f7f\u7528 bridge \u6a21\u5f0f\u7684\u7f51\u7edc\u3002\u5176\u7279\u70b9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>\u4f7f\u7528\u4e00\u4e2a linux bridge\uff0c\u9ed8\u8ba4\u4e3a docker0\u3002<\/li><li>\u4f7f\u7528 veth \u5bf9\uff0c\u4e00\u5934\u5728\u5bb9\u5668\u7684\u7f51\u7edc namespace \u4e2d\uff0c\u4e00\u5934\u5728 docker0 \u4e0a\u3002<\/li><li>\u8be5\u6a21\u5f0f\u4e0bDocker Container\u4e0d\u5177\u6709\u4e00\u4e2a\u516c\u6709IP\uff0c\u56e0\u4e3a\u5bbf\u4e3b\u673a\u7684IP\u5730\u5740\u4e0eveth pair\u7684 IP\u5730\u5740\u4e0d\u5728\u540c\u4e00\u4e2a\u7f51\u6bb5\u5185\u3002<\/li><li>Docker\u91c7\u7528 NAT \u65b9\u5f0f\uff0c\u5c06\u5bb9\u5668\u5185\u90e8\u7684\u670d\u52a1\u76d1\u542c\u7684\u7aef\u53e3\u4e0e\u5bbf\u4e3b\u673a\u7684\u67d0\u4e00\u4e2a\u7aef\u53e3port \u8fdb\u884c\u201c\u7ed1\u5b9a\u201d\uff0c\u4f7f\u5f97\u5bbf\u4e3b\u673a\u4ee5\u5916\u7684\u4e16\u754c\u53ef\u4ee5\u4e3b\u52a8\u5c06\u7f51\u7edc\u62a5\u6587\u53d1\u9001\u81f3\u5bb9\u5668\u5185\u90e8<\/li><li>\u5916\u754c\u8bbf\u95ee\u5bb9\u5668\u5185\u7684\u670d\u52a1\u65f6\uff0c\u9700\u8981\u8bbf\u95ee\u5bbf\u4e3b\u673a\u7684 IP \u4ee5\u53ca\u5bbf\u4e3b\u673a\u7684\u7aef\u53e3 port\u3002<\/li><li>NAT \u6a21\u5f0f\u7531\u4e8e\u662f\u5728\u4e09\u5c42\u7f51\u7edc\u4e0a\u7684\u5b9e\u73b0\u624b\u6bb5\uff0c\u6545\u80af\u5b9a\u4f1a\u5f71\u54cd\u7f51\u7edc\u7684\u4f20\u8f93\u6548\u7387\u3002<\/li><li>\u5bb9\u5668\u62e5\u6709\u72ec\u7acb\u3001\u9694\u79bb\u7684\u7f51\u7edc\u6808\uff1b\u8ba9\u5bb9\u5668\u548c\u5bbf\u4e3b\u673a\u4ee5\u5916\u7684\u4e16\u754c\u901a\u8fc7NAT\u5efa\u7acb\u901a\u4fe1<\/li><\/ul>\n\n\n\n<p>bridge\u6a21\u5f0f\u662fdocker\u9ed8\u8ba4\u7684\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0cdocker\u4e3a\u5bb9\u5668\u521b\u5efa\u72ec\u7acb\u7684\u7f51\u7edc\u6808\uff0c\u4fdd\u8bc1\u5bb9\u5668\u5185\u7684\u8fdb\u7a0b\u4f7f\u7528\u72ec\u7acb\u7684\u7f51\u7edc\u73af\u5883\uff0c\u5b9e\u73b0\u5bb9\u5668\u4e4b\u95f4\u3001\u5bb9\u5668\u4e0e\u5bbf\u4e3b\u673a\u4e4b\u95f4\u7684\u7f51\u7edc\u6808\u9694\u79bb\u3002\u540c\u65f6\uff0c\u901a\u8fc7\u5bbf\u4e3b\u673a\u4e0a\u7684docker0\u7f51\u6865\uff0c\u5bb9\u5668\u53ef\u4ee5\u4e0e\u5bbf\u4e3b\u673a\u4e43\u81f3\u5916\u754c\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\u3002\u5176\u7f51\u7edc\u6a21\u578b\u53ef\u4ee5\u53c2\u8003\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-31.png\" alt=\"\" class=\"wp-image-8959\" width=\"439\" height=\"231\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-31.png 874w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-31-300x158.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-31-768x404.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-31-830x437.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-31-230x121.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-31-350x184.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-31-480x253.png 480w\" sizes=\"(max-width: 439px) 100vw, 439px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u7f51\u7edc\u6a21\u578b\u53ef\u4ee5\u770b\u51fa\uff0c\u5bb9\u5668\u4ece\u539f\u7406\u4e0a\u662f\u53ef\u4ee5\u4e0e\u5bbf\u4e3b\u673a\u4e43\u81f3\u5916\u754c\u7684\u5176\u4ed6\u673a\u5668\u901a\u4fe1\u7684\u3002\u540c\u4e00\u5bbf\u4e3b\u673a\u4e0a\uff0c\u5bb9\u5668\u4e4b\u95f4\u90fd\u662f\u8fde\u63a5\u6389docker0\u8fd9\u4e2a\u7f51\u6865\u4e0a\u7684\uff0c\u5b83\u53ef\u4ee5\u4f5c\u4e3a\u865a\u62df\u4ea4\u6362\u673a\u4f7f\u5bb9\u5668\u53ef\u4ee5\u76f8\u4e92\u901a\u4fe1\u3002\u7136\u800c\uff0c\u7531\u4e8e\u5bbf\u4e3b\u673a\u7684IP\u5730\u5740\u4e0e\u5bb9\u5668veth pair\u7684 IP\u5730\u5740\u5747\u4e0d\u5728\u540c\u4e00\u4e2a\u7f51\u6bb5\uff0c\u6545\u4ec5\u4ec5\u4f9d\u9760veth pair\u548cnamespace\u7684\u6280\u672f\uff0c\u8fd8\u4e0d\u8db3\u4ee5\u4f7f\u5bbf\u4e3b\u673a\u4ee5\u5916\u7684\u7f51\u7edc\u4e3b\u52a8\u53d1\u73b0\u5bb9\u5668\u7684\u5b58\u5728\u3002\u4e3a\u4e86\u4f7f\u5916\u754c\u53ef\u4ee5\u65b9\u4f4d\u5bb9\u5668\u4e2d\u7684\u8fdb\u7a0b\uff0cdocker\u91c7\u7528\u4e86\u7aef\u53e3\u7ed1\u5b9a\u7684\u65b9\u5f0f\uff0c\u4e5f\u5c31\u662f\u901a\u8fc7iptables\u7684NAT\uff0c\u5c06\u5bbf\u4e3b\u673a\u4e0a\u7684\u7aef\u53e3\u7aef\u53e3\u6d41\u91cf\u8f6c\u53d1\u5230\u5bb9\u5668\u5185\u7684\u7aef\u53e3\u4e0a\u3002\u4e3e\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u521b\u5efa\u5bb9\u5668\uff0c\u5e76\u5c06\u5bbf\u4e3b\u673a\u76843306\u7aef\u53e3\u7ed1\u5b9a\u5230\u5bb9\u5668\u76843306\u7aef\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># docker run -tid --name db -p 3306:3306 MySQL<\/pre>\n\n\n\n<p>\u5728\u5bbf\u4e3b\u673a\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7iptables -t nat -L -n\uff0c\u67e5\u5230\u4e00\u6761DNAT\u89c4\u5219\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># DNAT tcp -- 0.0.0.0\/0 0.0.0.0\/0 tcp dpt:3306 to:172.17.0.5:3306<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684172.17.0.5\u5373\u4e3abridge\u6a21\u5f0f\u4e0b\uff0c\u521b\u5efa\u7684\u5bb9\u5668IP\u3002<br>\u5f88\u660e\u663e\uff0cbridge\u6a21\u5f0f\u7684\u5bb9\u5668\u4e0e\u5916\u754c\u901a\u4fe1\u65f6\uff0c\u5fc5\u5b9a\u4f1a\u5360\u7528\u5bbf\u4e3b\u673a\u4e0a\u7684\u7aef\u53e3\uff0c\u4ece\u800c\u4e0e\u5bbf\u4e3b\u673a\u7ade\u4e89\u7aef\u53e3\u8d44\u6e90\uff0c\u5bf9\u5bbf\u4e3b\u673a\u7aef\u53e3\u7684\u7ba1\u7406\u4f1a\u662f\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u95ee\u9898\u3002\u540c\u65f6\uff0c\u7531\u4e8e\u5bb9\u5668\u4e0e\u5916\u754c\u901a\u4fe1\u662f\u57fa\u4e8e\u4e09\u5c42\u4e0aiptables NAT\uff0c\u6027\u80fd\u548c\u6548\u7387\u4e0a\u7684\u635f\u8017\u662f\u53ef\u4ee5\u9884\u89c1\u7684\u3002<\/p>\n\n\n\n<p><strong>2.2\u3001Host \u6a21\u5f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5982\u679c\u542f\u52a8\u5bb9\u5668\u65f6\u4f7f\u7528host\u6a21\u5f0f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5bb9\u5668\u5c06\u4e0d\u4f1a\u83b7\u5f97\u4e00\u4e2a\u72ec\u7acb\u7684Network Namespace\uff0c\u800c\u662f\u548c\u5bbf\u4e3b\u673a\u5171\u7528\u4e00\u4e2aNetwork Namespace\u3002\u5bb9\u5668\u5c06\u4e0d\u4f1a\u865a\u62df\u51fa\u81ea\u5df1\u7684\u7f51\u5361\uff0c\u914d\u7f6e\u81ea\u5df1\u7684IP\u7b49\uff0c\u800c\u662f\u4f7f\u7528\u5bbf\u4e3b\u673a\u7684IP\u548c\u7aef\u53e3\u3002\u4f46\u662f\uff0c\u5bb9\u5668\u7684\u5176\u4ed6\u65b9\u9762\uff0c\u5982\u6587\u4ef6\u7cfb\u7edf\u3001\u8fdb\u7a0b\u5217\u8868\u7b49\u8fd8\u662f\u548c\u5bbf\u4e3b\u673a\u9694\u79bb\u7684\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528host\u6a21\u5f0f\u7684\u5bb9\u5668\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5bbf\u4e3b\u673a\u7684IP\u5730\u5740\u4e0e\u5916\u754c\u901a\u4fe1\uff0c\u5bb9\u5668\u5185\u90e8\u7684\u670d\u52a1\u7aef\u53e3\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5bbf\u4e3b\u673a\u7684\u7aef\u53e3\uff0chost\u6700\u5927\u7684\u4f18\u52bf\u5c31\u662f\u7f51\u7edc\u6027\u80fd\u6bd4\u8f83\u597d\uff0c\u4f46\u662fdocker host\u4e0a\u5df2\u7ecf\u4f7f\u7528\u7684\u7aef\u53e3\u5c31\u4e0d\u80fd\u518d\u7528\u4e86\uff0c\u7f51\u7edc\u7684\u9694\u79bb\u6027\u4e0d\u597d\u3002<\/p>\n\n\n\n<p>Host \u6a21\u5f0f\u5e76\u6ca1\u6709\u4e3a\u5bb9\u5668\u521b\u5efa\u4e00\u4e2a\u9694\u79bb\u7684\u7f51\u7edc\u73af\u5883\u3002\u800c\u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3ahost\u6a21\u5f0f\uff0c\u662f\u56e0\u4e3a\u8be5\u6a21\u5f0f\u4e0b\u7684 Docker \u5bb9\u5668\u4f1a\u548c host \u5bbf\u4e3b\u673a\u5171\u4eab\u540c\u4e00\u4e2a\u7f51\u7edc namespace\uff0c\u6545 Docker Container\u53ef\u4ee5\u548c\u5bbf\u4e3b\u673a\u4e00\u6837\uff0c\u4f7f\u7528\u5bbf\u4e3b\u673a\u7684eth0\uff0c\u5b9e\u73b0\u548c\u5916\u754c\u7684\u901a\u4fe1\u3002\u6362\u8a00\u4e4b\uff0cDocker Container\u7684 IP \u5730\u5740\u5373\u4e3a\u5bbf\u4e3b\u673a eth0 \u7684 IP \u5730\u5740\u3002\u5176\u7279\u70b9\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul><li>\u8fd9\u79cd\u6a21\u5f0f\u4e0b\u7684\u5bb9\u5668\u6ca1\u6709\u9694\u79bb\u7684 network namespace\u3002<\/li><li>\u5bb9\u5668\u7684 IP \u5730\u5740\u540c Docker host \u7684 IP \u5730\u5740\u3002<\/li><li>\u9700\u8981\u6ce8\u610f\u5bb9\u5668\u4e2d\u670d\u52a1\u7684\u7aef\u53e3\u53f7\u4e0d\u80fd\u4e0e Docker host \u4e0a\u5df2\u7ecf\u4f7f\u7528\u7684\u7aef\u53e3\u53f7\u76f8\u51b2\u7a81\u3002<\/li><li>host \u6a21\u5f0f\u80fd\u591f\u548c\u5176\u5b83\u6a21\u5f0f\u5171\u5b58\u3002<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-32.png\" alt=\"\" class=\"wp-image-8960\" width=\"452\" height=\"241\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-32.png 1008w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-32-300x160.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-32-768x410.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-32-830x443.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-32-230x123.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-32-350x187.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-32-480x256.png 480w\" sizes=\"(max-width: 452px) 100vw, 452px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-33.png\" alt=\"\" class=\"wp-image-8961\" width=\"467\" height=\"357\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-33.png 986w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-33-300x229.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-33-768x587.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-33-830x635.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-33-230x176.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-33-350x268.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-33-480x367.png 480w\" sizes=\"(max-width: 467px) 100vw, 467px\" \/><\/figure><\/div>\n\n\n\n<p>\u603b\u7ed3\uff1a\u91c7\u7528host\u6a21\u5f0f\u7684\u5bb9\u5668\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5bbf\u4e3b\u673a\u7684IP\u5730\u5740\u4e0e\u5916\u754c\u8fdb\u884c\u901a\u4fe1\uff0c\u82e5\u5bbf\u4e3b\u673a\u5177\u6709\u516c\u6709IP\uff0c\u90a3\u4e48\u5bb9\u5668\u4e5f\u62e5\u6709\u8fd9\u4e2a\u516c\u6709IP\u3002\u540c\u65f6\u5bb9\u5668\u5185\u670d\u52a1\u7684\u7aef\u53e3\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5bbf\u4e3b\u673a\u7684\u7aef\u53e3\uff0c\u65e0\u9700\u989d\u5916\u8fdb\u884cNAT\u8f6c\u6362\uff0c\u800c\u4e14\u7531\u4e8e\u5bb9\u5668\u901a\u4fe1\u65f6\uff0c\u4e0d\u518d\u9700\u8981\u901a\u8fc7linuxbridge\u7b49\u65b9\u5f0f\u8f6c\u53d1\u6216\u6570\u636e\u5305\u7684\u62c6\u5c01\uff0c\u6027\u80fd\u4e0a\u6709\u5f88\u5927\u4f18\u52bf\u3002\u5f53\u7136\uff0c\u8fd9\u79cd\u6a21\u5f0f\u6709\u4f18\u52bf\uff0c\u4e5f\u5c31\u6709\u52a3\u52bf\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n\n\n\n<ul><li>\u6700\u660e\u663e\u7684\u5c31\u662f\u5bb9\u5668\u4e0d\u518d\u62e5\u6709\u9694\u79bb\u3001\u72ec\u7acb\u7684\u7f51\u7edc\u6808\u3002\u5bb9\u5668\u4f1a\u4e0e\u5bbf\u4e3b\u673a\u7ade\u4e89\u7f51\u7edc\u6808\u7684\u4f7f\u7528\uff0c\u5e76\u4e14\u5bb9\u5668\u7684\u5d29\u6e83\u5c31\u53ef\u80fd\u5bfc\u81f4\u5bbf\u4e3b\u673a\u5d29\u6e83\uff0c\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u8fd9\u79cd\u95ee\u9898\u53ef\u80fd\u662f\u4e0d\u88ab\u5141\u8bb8\u7684\u3002<\/li><li>\u5bb9\u5668\u5185\u90e8\u5c06\u4e0d\u518d\u62e5\u6709\u6240\u6709\u7684\u7aef\u53e3\u8d44\u6e90\uff0c\u56e0\u4e3a\u4e00\u4e9b\u7aef\u53e3\u5df2\u7ecf\u88ab\u5bbf\u4e3b\u673a\u670d\u52a1\u3001bridge\u6a21\u5f0f\u7684\u5bb9\u5668\u7aef\u53e3\u7ed1\u5b9a\u7b49\u5176\u4ed6\u670d\u52a1\u5360\u7528\u6389\u4e86\u3002<\/li><\/ul>\n\n\n\n<p><strong>2.3\u3001container \u6a21\u5f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5176\u4ed6\u7f51\u7edc\u6a21\u5f0f\u662fdocker\u4e2d\u4e00\u79cd\u8f83\u4e3a\u7279\u522b\u7684\u7f51\u7edc\u7684\u6a21\u5f0f\u3002\u5728\u8fd9\u4e2a\u6a21\u5f0f\u4e0b\u7684\u5bb9\u5668\uff0c\u4f1a\u4f7f\u7528\u5176\u4ed6\u5bb9\u5668\u7684\u7f51\u7edc\u547d\u540d\u7a7a\u95f4\uff0c\u5176\u7f51\u7edc\u9694\u79bb\u6027\u4f1a\u5904\u4e8ebridge\u6865\u63a5\u6a21\u5f0f\u4e0ehost\u6a21\u5f0f\u4e4b\u95f4\u3002\u5f53\u5bb9\u5668\u5171\u4eab\u5176\u4ed6\u5bb9\u5668\u7684\u7f51\u7edc\u547d\u540d\u7a7a\u95f4\uff0c\u5219\u5728\u8fd9\u4e24\u4e2a\u5bb9\u5668\u4e4b\u95f4\u4e0d\u5b58\u5728\u7f51\u7edc\u9694\u79bb\uff0c\u800c\u5979\u4eec\u53c8\u4e0e\u5bbf\u4e3b\u673a\u4ee5\u53ca\u9664\u6b64\u4e4b\u5916\u5176\u4ed6\u7684\u5bb9\u5668\u5b58\u5728\u7f51\u7edc\u9694\u79bb\u3002\u5176\u7f51\u7edc\u6a21\u578b\u53ef\u4ee5\u53c2\u8003\u4e0b\u56fe\uff1a<br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-34.png\" alt=\"\" class=\"wp-image-8962\" width=\"372\" height=\"231\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-34.png 880w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-34-300x187.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-34-768x478.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-34-830x517.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-34-230x143.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-34-350x218.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-34-480x299.png 480w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-35.png\" alt=\"\" class=\"wp-image-8963\" width=\"379\" height=\"347\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-35.png 894w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-35-300x275.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-35-768x704.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-35-830x761.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-35-230x211.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-35-350x321.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-35-480x440.png 480w\" sizes=\"(max-width: 379px) 100vw, 379px\" \/><\/figure><\/div>\n\n\n\n<p>\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\u7684\u5bb9\u5668\u53ef\u4ee5\u901a\u8fc7localhost\u6765\u540c\u4e00\u7f51\u7edc\u547d\u540d\u7a7a\u95f4\u4e0b\u7684\u5176\u4ed6\u5bb9\u5668\uff0c\u4f20\u8f93\u6548\u7387\u8f83\u9ad8\u3002\u800c\u4e14\u8fd9\u79cd\u6a21\u5f0f\u8fd8\u8282\u7ea6\u4e86\u4e00\u5b9a\u6570\u91cf\u7684\u7f51\u7edc\u8d44\u6e90\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u6539\u53d8\u5bb9\u5668\u4e0e\u5916\u754c\u901a\u4fe1\u7684\u65b9\u5f0f\u3002\u5728\u4e00\u4e9b\u7279\u6b8a\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff0ckubernetes\u7684pod\uff0ckubernetes\u4e3apod\u521b\u5efa\u4e00\u4e2a\u57fa\u7840\u8bbe\u65bd\u5bb9\u5668\uff0c\u540c\u4e00pod\u4e0b\u7684\u5176\u4ed6\u5bb9\u5668\u90fd\u4ee5\u5176\u4ed6\u5bb9\u5668\u6a21\u5f0f\u5171\u4eab\u8fd9\u4e2a\u57fa\u7840\u8bbe\u65bd\u5bb9\u5668\u7684\u7f51\u7edc\u547d\u540d\u7a7a\u95f4\uff0c\u76f8\u4e92\u4e4b\u95f4\u4ee5localhost\u8bbf\u95ee\uff0c\u6784\u6210\u4e00\u4e2a\u7edf\u4e00\u7684\u6574\u4f53\u3002<\/p>\n\n\n\n<p><strong>2.4 none \u6a21\u5f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7f51\u7edc\u6a21\u5f0f\u4e3a none\uff0c\u5373\u4e0d\u4e3a Docker \u5bb9\u5668\u6784\u9020\u4efb\u4f55\u7f51\u7edc\u73af\u5883\u3002\u4e00\u65e6Docker \u5bb9\u5668\u91c7\u7528\u4e86none \u7f51\u7edc\u6a21\u5f0f\uff0c\u90a3\u4e48\u5bb9\u5668\u5185\u90e8\u5c31\u53ea\u80fd\u4f7f\u7528loopback\u7f51\u7edc\u8bbe\u5907\uff0c\u4e0d\u4f1a\u518d\u6709\u5176\u4ed6\u7684\u7f51\u7edc\u8d44\u6e90\u3002Docker Container\u7684none\u7f51\u7edc\u6a21\u5f0f\u610f\u5473\u7740\u4e0d\u7ed9\u8be5\u5bb9\u5668\u521b\u5efa\u4efb\u4f55\u7f51\u7edc\u73af\u5883\uff0c\u5bb9\u5668\u53ea\u80fd\u4f7f\u7528127.0.0.1\u7684\u672c\u673a\u7f51\u7edc\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u5bb9\u5668\u6709\u72ec\u7acb\u7684\u7f51\u7edc\u6808\uff0c\u4f46\u4e0d\u5305\u542b\u4efb\u4f55\u7f51\u7edc\u914d\u7f6e\uff0c\u53ea\u5177\u6709lo\u8fd9\u4e2aloopback\u7f51\u5361\u7528\u4e8e\u8fdb\u7a0b\u901a\u4fe1\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cnone\u6a21\u5f0f\u4e3a\u5bb9\u5668\u505a\u4e86\u6700\u5c11\u7684\u7f51\u7edc\u8bbe\u7f6e\uff0c\u4f46\u662f\u4fd7\u8bdd\u8bf4\u5f97\u597d\u201c\u5c11\u5373\u662f\u591a\u201d\uff0c\u5728\u6ca1\u6709\u7f51\u7edc\u914d\u7f6e\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u7b2c\u4e09\u65b9\u5de5\u5177\u6216\u8005\u624b\u5de5\u7684\u65b9\u5f0f\uff0c\u5f00\u53d1\u8fd9\u4efb\u610f\u5b9a\u5236\u5bb9\u5668\u7684\u7f51\u7edc\uff0c\u63d0\u4f9b\u4e86\u6700\u9ad8\u7684\u7075\u6d3b\u6027\u3002<br><\/p>\n\n\n\n<p><strong>2.5. \u66b4\u9732\u7aef\u53e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u540c\u4e00\u4e2a\u7f51\u7edc\u4e2d\u7684\u5bb9\u5668\u4e4b\u95f4\u867d\u7136\u53ef\u4ee5\u4e92\u76f8 ping \u901a\uff0c\u4f46\u662f\u5e76\u4e0d\u610f\u5473\u7740\u53ef\u4ee5\u4efb\u610f\u8bbf\u95ee\u5bb9\u5668\u4e2d\u7684\u4efb\u4f55\u670d\u52a1\u3002Docker \u4e3a\u5bb9\u5668\u589e\u52a0\u4e86\u4e00\u5957\u5b89\u5168\u673a\u5236\uff0c\u53ea\u6709\u5bb9\u5668\u81ea\u8eab\u5141\u8bb8\u7684\u7aef\u53e3\uff0c\u624d\u80fd\u88ab\u5176\u4ed6\u5bb9\u5668\u6240\u8bbf\u95ee\u3002\u5982\u4e0b\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker container ls<\/pre>\n\n\n\n<p>\u547d\u4ee4\u53ef\u4ee5\u770b\u5230\u5bb9\u5668\u66b4\u9732\u7ed9\u5176\u4ed6\u5bb9\u5668\u8bbf\u95ee\u7684\u7aef\u53e3\u662f 80\uff0c\u90a3\u4e48\u6211\u4eec\u53ea\u80fd\u5bb9\u5668\u7684 80 \u7aef\u53e3\u8fdb\u884c\u8bbf\u95ee\uff0c\u800c\u4e0d\u80fd\u5bf9\u6ca1\u6709\u5f00\u653e\u7684 22 \u7aef\u53e3\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ docker container ls\nCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES\n5a8dece3841d        nginx               \"\/docker-entrypoint.\u2026\"   3 minutes ago       Up 3 minutes        80\/tcp              web\n\n$ telnet 172.18.0.2 80\nTrying 172.18.0.2...\nConnected to 172.18.0.2.\nEscape character is '^]'.\n\n$ telnet 172.18.0.2 20\nTrying 172.18.0.2...\ntelnet: Unable to connect to remote host: Connection refused<\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u955c\u50cf\u521b\u5efa\u7684\u65f6\u5019\u5b9a\u4e49\u8981\u66b4\u9732\u7684\u7aef\u53e3\uff0c\u4e5f\u53ef\u4ee5\u5728\u5bb9\u5668\u521b\u5efa\u65f6\u5b9a\u4e49\u8981\u66b4\u9732\u7684\u7aef\u53e3\uff0c\u4f7f\u7528 &#8211;expose\u3002\u5982\u4e0b\u6240\u793a\uff0c\u5c31\u989d\u5916\u66b4\u9732\u4e86 20\u300122 \u8fd9\u4e24\u4e2a\u7aef\u53e3\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ docker container run -d --name web --expose 22 --expose 20 nginx\n\n$ docker container ls\nCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES\n4749dac32711        nginx               \"\/docker-entrypoint.\u2026\"   12 seconds ago      Up 10 seconds       20\/tcp, 22\/tcp, 80\/tcp   web\n<\/pre>\n\n\n\n<p>\u5bb9\u5668\u7684\u7aef\u53e3\u66b4\u9732\u7c7b\u4f3c\u4e8e\u6253\u5f00\u4e86\u5bb9\u5668\u7684\u9632\u706b\u5899\uff0c\u5177\u4f53\u80fd\u4e0d\u80fd\u901a\u8fc7\u8fd9\u4e2a\u7aef\u53e3\u8bbf\u95ee\u5bb9\u5668\u4e2d\u7684\u670d\u52a1\uff0c\u8fd8\u5f97\u770b\u5bb9\u5668\u4e2d\u6709\u65e0\u5e94\u7528\u76d1\u542c\u5e76\u5904\u7406\u6765\u81ea\u8fd9\u4e2a\u7aef\u53e3\u7684\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p><strong>2.6. \u7aef\u53e3\u6620\u5c04<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e0a\u9762\u63d0\u5230\u7684\u6865\u63a5\u7f51\u7edc\u4e2d\u7684\u5bb9\u5668\u53ea\u80fd\u4e0e\u4f4d\u4e8e\u76f8\u540c\u7f51\u7edc\u4e2d\u7684\u5bb9\u5668\u8fdb\u884c\u901a\u4fe1\uff0c\u5047\u5982\u4e00\u4e2a\u5bb9\u5668\u60f3\u5bf9\u5916\u63d0\u4f9b\u670d\u52a1\u7684\u8bdd\uff0c\u9700\u8981\u8fdb\u884c\u7aef\u53e3\u6620\u5c04\u3002\u7aef\u53e3\u6620\u5c04\u5c06\u5bb9\u5668\u7684\u67d0\u4e2a\u7aef\u53e3\u6620\u5c04\u5230 Docker \u4e3b\u673a\u7aef\u53e3\u4e0a\u3002\u90a3\u4e48\u4efb\u4f55\u53d1\u9001\u5230\u8be5\u7aef\u53e3\u7684\u6d41\u91cf\uff0c\u90fd\u4f1a\u88ab\u8f6c\u53d1\u5230\u5bb9\u5668\u4e2d\u3002\u5982\u56fe\u6240\u793a\uff0c\u5bb9\u5668\u5185\u90e8\u5f00\u653e\u7aef\u53e3\u4e3a 80\uff0c\u8be5\u7aef\u53e3\u88ab\u6620\u5c04\u5230\u4e86 Docker \u4e3b\u673a\u7684 10.0.0.15 \u7684 5000 \u7aef\u53e3\u4e0a\u3002\u6700\u7ec8\u8bbf\u95ee 10.0.0.15:5000 \u7684\u6240\u6709\u6d41\u91cf\u90fd\u4f1a\u88ab\u8f6c\u53d1\u5230\u5bb9\u5668\u7684 80 \u7aef\u53e3\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-36-840x1024.png\" alt=\"\" class=\"wp-image-8964\" width=\"249\" height=\"304\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-36-840x1024.png 840w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-36-246x300.png 246w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-36-768x936.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-36-830x1011.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-36-230x280.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-36-350x426.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-36-480x585.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-36.png 1034w\" sizes=\"(max-width: 249px) 100vw, 249px\" \/><\/figure><\/div>\n\n\n\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u8fd0\u884c\u4e86\u4e00\u4e2a\u65b0\u7684 web \u670d\u52a1\u5bb9\u5668\uff0c\u5e76\u4e14\u5c06\u5bb9\u5668 80 \u7aef\u53e3\u6620\u5c04\u5230 Dokcer \u4e3b\u673a\u7684 5000 \u7aef\u53e3\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ docker container run -d --name web --network localnet -p 5000:80 nginx<\/pre>\n\n\n\n<p>\u90a3\u4e48\uff0c\u5f53\u6211\u4eec\u901a\u8fc7 web \u6d4f\u89c8\u5668\u8bbf\u95ee Docker \u4e3b\u673a\u7684 5000 \u7aef\u53e3\u65f6\uff0c\u4f1a\u5f97\u5230\u5982\u56fe\u6240\u793a\u7684\u7ed3\u679c\u3002\u5916\u90e8\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee Docker \u4e3b\u673a\u7684 TCP \u7aef\u53e3 5000\uff0c\u6765\u8bbf\u95ee\u8fd0\u884c\u5728\u6865\u63a5\u7f51\u7edc\u4e0a\u7684 Nginx \u5bb9\u5668\u4e86\u3002<\/p>\n\n\n\n<p>\u7aef\u53e3\u6620\u5c04\u4e4b\u540e\uff0c\u5047\u5982\u4e3b\u673a\u7684 5000 \u7aef\u53e3\u88ab\u5360\u7528\u4e86\uff0c\u90a3\u4e48\u5176\u4ed6\u5bb9\u5668\u5c31\u4e0d\u80fd\u518d\u4f7f\u7528\u8fd9\u4e2a\u7aef\u53e3\u4e86\u3002<\/p>\n\n\n\n<p><strong>2.7 \u76f8\u5173\u547d\u4ee4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u5217\u51fa\u8fd0\u884c\u5728\u672c\u5730 docker \u4e3b\u673a\u4e0a\u7684\u5168\u90e8\u7f51\u7edc\ndocker network ls\n\n# \u63d0\u4f9b Docker \u7f51\u7edc\u7684\u8be6\u7ec6\u914d\u7f6e\u4fe1\u606f\ndocker network inspect &lt;NETWORK_NAME>\n\n# \u521b\u5efa\u65b0\u7684\u5355\u673a\u6865\u63a5\u7f51\u7edc\uff0c\u540d\u4e3a localnet\uff0c\u5176\u4e2d -d \u4e0d\u6307\u5b9a\u7684\u8bdd\uff0c\u9ed8\u8ba4\u662f bridge \u9a71\u52a8\u3002\u5e76\u4e14\u4e3b\u673a\u5185\u6838\u4e2d\u4e5f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7f51\u6865\u3002\ndocker network create -d bridge localnet\n\n# \u5220\u9664 Docker \u4e3b\u673a\u4e0a\u6307\u5b9a\u7684\u7f51\u7edc\ndocker network rm\n\n# \u5220\u9664\u4e3b\u673a\u4e0a\u5168\u90e8\u672a\u4f7f\u7528\u7684\u7f51\u7edc\ndocker network prune\n\n# \u8fd0\u884c\u4e00\u4e2a\u65b0\u7684\u5bb9\u5668\uff0c\u5e76\u4e14\u8ba9\u8fd9\u4e2a\u5bb9\u5668\u52a0\u5165 Docker \u7684 localnet \u8fd9\u4e2a\u7f51\u7edc\u4e2d\ndocker container run -d --name demo1 --network localnet alpine sleep 3600\n\n# \u8fd0\u884c\u4e00\u4e2a\u65b0\u7684\u5bb9\u5668\uff0c\u5e76\u4e14\u8ba9\u8fd9\u4e2a\u5bb9\u5668\u66b4\u9732 22\u300120 \u4e24\u4e2a\u7aef\u53e3\ndocker container run -d --name web --expose 22 --expose 20 nginx\n\n# \u8fd0\u884c\u4e00\u4e2a\u65b0\u7684\u5bb9\u5668\uff0c\u5e76\u4e14\u5c06\u8fd9\u4e2a\u5bb9\u5668\u7684 80 \u7aef\u53e3\u6620\u5c04\u5230\u4e3b\u673a\u7684 5000 \u7aef\u53e3\ndocker container run -d --name web --network localnet -p 5000:80 nginx\n\n# \u67e5\u770b\u7cfb\u7edf\u4e2d\u7684\u7f51\u6865\nbrctl show<\/pre>\n\n\n\n<p><strong>3. docker\u901a\u8fc7\u5bb9\u5668\u8bbf\u95ee\u5bbf\u4e3b\u673a\u5668<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-37-1024x105.png\" alt=\"\" class=\"wp-image-8973\" width=\"585\" height=\"60\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-37-1024x105.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-37-300x31.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-37-768x79.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-37-830x85.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-37-230x24.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-37-350x36.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-37-480x49.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-37.png 1322w\" sizes=\"(max-width: 585px) 100vw, 585px\" \/><figcaption>\u901a\u8fc7\u5bb9\u5668\u91cc\u9762\u8bbf\u95ee\u5bbf\u4e3b\u673aip\u53d1\u73b0\u662f\u8bbf\u95ee\u4e0d\u901a\u7684\uff0c<\/figcaption><\/figure><\/div>\n\n\n\n<p>\u5f53\u6211\u4eec\u5728\u5bb9\u5668\u4e2d\uff0c\u60f3\u4f7f\u7528localhost\u8bbf\u95ee\u5bbf\u4e3b\u673a\uff0c\u53d1\u73b0\u662f\u8bbf\u95ee\u4e0d\u901a\u7684\u3002<\/p>\n\n\n\n<p>\u5206\u6790\uff0c\u5728docker\u5bb9\u5668\u91cc\u9762\u4f7f\u7528localhost\u8bbf\u95ee\u7684\u662fdocker\u5bb9\u5668\u7684\u672c\u5730ip\uff0c\u800c\u4e0d\u662f\u5bbf\u4e3b\u673a\u7684\u3002<\/p>\n\n\n\n<p><strong>\u65b9\u6cd51: \u5bfb\u627edocker0\u865a\u62df\u7f51\u6865\u7684ip<\/strong><\/p>\n\n\n\n<p>\u56e0\u4e3a\u5bb9\u5668\u9ed8\u8ba4\u662fbridge\u6865\u63a5\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u627edocker0\u865a\u62df\u7f51\u6865\u7684ip\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ip addr show docker0<\/pre>\n\n\n\n<p>docker0\u8fd9\u4e2a\u7f51\u5361\u4e0a\u6709IP\u4fe1\u606f<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-38-1024x170.png\" alt=\"\" class=\"wp-image-8974\" width=\"568\" height=\"94\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-38-1024x170.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-38-300x50.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-38-768x127.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-38-830x138.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-38-230x38.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-38-350x58.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-38-480x80.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-38.png 1472w\" sizes=\"(max-width: 568px) 100vw, 568px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230ip\uff1a172.17.0.1<\/p>\n\n\n\n<p>\u5bb9\u5668\u4e0a\u628alocalhost\u6539\u6210172.17.0.1,\u5373\u53ef\u8bbf\u95ee\u5230\u5bbf\u4e3b\u673a\u4e0a\u7684\u670d\u52a1<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl http:\/\/172.17.0.1:8883\/test<\/pre>\n\n\n\n<p><strong>\u65b9\u6cd52: \u4f7f\u7528<code>host.docker.internal<\/code> \u6216\u8005 <code>gateway.docker.internal<\/code> \u6765\u66ff\u6362localhost<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl http:\/\/host.docker.internal:8000\ncurl http:\/\/gateway.docker.internal:8000<\/pre>\n\n\n\n<p><strong>\u65b9\u6cd53:\u4f7f\u7528Host<\/strong><\/p>\n\n\n\n<p>\u8fd8\u6709\u4e00\u79cd\u662f\u4f7f\u7528host\uff0cdocker \u5bb9\u5668\u8fd0\u884c\u6709\u4e09\u79cd\u7f51\u7edc\u914d\u7f6e\uff1ahost\uff0c bridge\uff0cnone\uff0c\u9ed8\u8ba4\u662fbridge\uff0c none\u8868\u793a\u5bb9\u5668\u65e0\u6cd5\u4f7f\u7528\u7f51\u7edc\uff0cbridge \u9700\u8981\u7528-p \u53c2\u6570\u628a\u7aef\u53e3\u6620\u5c04\u51fa\u6765\u3002\u5982\u679c\u7528host\uff0c\u5373\u8868\u793a\u5bbf\u4e3b\u673a\u4e0e\u5bb9\u5668\u5171\u7528\u7f51\u7edc\uff0c\u90a3\u4e48\u5bb9\u5668\u7684localhost \u5c31\u662f \u5bbf\u4e3b\u673a\u7684localhost\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker run -d --name my_docker --network host ubuntu:18<\/pre>\n\n\n\n<p>\u4f7f\u7528&#8211;network host \u5c31\u4e0d\u9700\u8981\u8fdb\u884c-p\u7aef\u53e3\u5f71\u5c04\u4e86\u3002\u5bb9\u5668\u7684\u5730\u5740\u5c31\u662f\u5bbf\u4e3b\u673a\u7684\u5730\u5740\uff0c\u7aef\u53e3\u4e5f\u662f\u4e00\u6837\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u7b80\u4ecb \u5bb9\u5668\u7f51\u7edc\u5b9e\u8d28\u4e0a\u662f\u7531 Dokcer \u4e3a\u5e94\u7528\u7a0b\u5e8f\u6240\u521b\u9020\u7684\u865a\u62df\u73af\u5883\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u80fd\u8ba9\u5e94\u7528\u4ece\u5bbf\u4e3b\u673a\u64cd\u4f5c\u7cfb\u7edf\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8951"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8951"}],"version-history":[{"count":11,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8951\/revisions"}],"predecessor-version":[{"id":8975,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8951\/revisions\/8975"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}