{"id":10007040,"date":"2023-01-04T12:16:48","date_gmt":"2023-01-04T04:16:48","guid":{"rendered":"https:\/\/ityet.com\/?p=10007040"},"modified":"2023-01-04T12:16:48","modified_gmt":"2023-01-04T04:16:48","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9adocker-%e6%9c%89%e5%87%a0%e7%a7%8d%e7%bd%91%e7%bb%9c%e6%a8%a1%e5%bc%8f%ef%bc%9f5-%e5%b9%b4%e5%b7%a5%e4%bd%9c%e7%bb%8f%e9%aa%8c%e9%83%bd%e8%a1%a8%e7%a4%ba%e7%ad%94","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10007040","title":{"rendered":"\u9762\u8bd5\u5b98\uff1aDocker \u6709\u51e0\u79cd\u7f51\u7edc\u6a21\u5f0f\uff1f5 \u5e74\u5de5\u4f5c\u7ecf\u9a8c\u90fd\u8868\u793a\u7b54\u4e0d\u4e0a\u6765\u3002\u3002"},"content":{"rendered":"<p>\u672c\u6587\u4f5c\u8005\uff1a\u77e5\u77e5\u884c\u884c \/<br \/>\u672c\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=mFN5Q9oIM1vA5%2Bvdk4QwkA%3D%3D.Z%2F3LTHlUXihuSXHr8h3LeM95jfEUdGNqb7MAZVj7aHXaFWARX4%2B0Pp2NEYUzJRoM\" rel=\"nofollow\">https:\/\/www.cnblogs.com\/loron&#8230;<\/a><\/p>\n<h3>docker\u5bb9\u5668\u7f51\u7edc<\/h3>\n<p>Docker\u5728\u5b89\u88c5\u540e\u81ea\u52a8\u63d0\u4f9b3\u79cd\u7f51\u7edc\uff0c\u53ef\u4ee5\u4f7f\u7528docker network ls\u547d\u4ee4\u67e5\u770b<\/p>\n<pre><code class=\"sql\">[root@localhost ~]# docker network ls\nNETWORK ID          NAME                DRIVER              SCOPE\ncd97bb997b84        bridge              bridge              local\n0a04824fc9b6        host                host                local\n4dcb8fbdb599        none                null                local<\/code><\/pre>\n<p>Docker\u4f7f\u7528Linux\u6865\u63a5\uff0c\u5728\u5bbf\u4e3b\u673a\u865a\u62df\u4e00\u4e2aDocker\u5bb9\u5668\u7f51\u6865(docker0)\uff0cDocker\u542f\u52a8\u4e00\u4e2a\u5bb9\u5668\u65f6\u4f1a\u6839\u636eDocker\u7f51\u6865\u7684\u7f51\u6bb5\u5206\u914d\u7ed9\u5bb9\u5668\u4e00\u4e2aIP\u5730\u5740\uff0c\u79f0\u4e3aContainer-IP\uff0c\u540c\u65f6Docker\u7f51\u6865\u662f\u6bcf\u4e2a\u5bb9\u5668\u7684\u9ed8\u8ba4\u7f51\u5173\u3002\u56e0\u4e3a\u5728\u540c\u4e00\u5bbf\u4e3b\u673a\u5185\u7684\u5bb9\u5668\u90fd\u63a5\u5165\u540c\u4e00\u4e2a\u7f51\u6865\uff0c\u8fd9\u6837\u5bb9\u5668\u4e4b\u95f4\u5c31\u80fd\u591f\u901a\u8fc7\u5bb9\u5668\u7684Container-IP\u76f4\u63a5\u901a\u4fe1\u3002<\/p>\n<h3>docker\u76844\u79cd\u7f51\u7edc\u6a21\u5f0f<\/h3>\n<table>\n<thead>\n<tr>\n<th align=\"left\">\u7f51\u7edc\u6a21\u5f0f<\/th>\n<th align=\"left\">\u914d\u7f6e<\/th>\n<th align=\"left\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\">host<\/td>\n<td align=\"left\">&#8211;network host<\/td>\n<td align=\"left\">\u5bb9\u5668\u548c\u5bbf\u4e3b\u673a\u5171\u4eabNetwork namespace<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">container<\/td>\n<td align=\"left\">&#8211;network container:NAME_OR_ID<\/td>\n<td align=\"left\">\u5bb9\u5668\u548c\u53e6\u5916\u4e00\u4e2a\u5bb9\u5668\u5171\u4eabNetwork namespace<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">none<\/td>\n<td align=\"left\">&#8211;network none<\/td>\n<td align=\"left\">\u5bb9\u5668\u6709\u72ec\u7acb\u7684Network namespace\uff0c\u4f46\u5e76\u6ca1\u6709\u5bf9\u5176\u8fdb\u884c\u4efb\u4f55\u7f51\u7edc\u8bbe\u7f6e\uff0c\u5982\u5206\u914dveth pair \u548c\u7f51\u6865\u8fde\u63a5\uff0c\u914d\u7f6eIP\u7b49<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">bridge<\/td>\n<td align=\"left\">&#8211;network<\/td>\n<td align=\"left\">bridge \u9ed8\u8ba4\u6a21\u5f0f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc5xPB\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>bridge\u6a21\u5f0f<\/h3>\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\u4f1a\u8fde\u63a5\u5230\u8fd9\u4e2a\u865a\u62df\u7f51\u6865\u4e0a\u3002\u865a\u62df\u7f51\u6865\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c\u7269\u7406\u4ea4\u6362\u673a\u7c7b\u4f3c\uff0c\u8fd9\u6837\u4e3b\u673a\u4e0a\u7684\u6240\u6709\u5bb9\u5668\u5c31\u901a\u8fc7\u4ea4\u6362\u673a\u8fde\u5728\u4e86\u4e00\u4e2a\u4e8c\u5c42\u7f51\u7edc\u4e2d\u3002<\/p>\n<p>\u4ecedocker0\u5b50\u7f51\u4e2d\u5206\u914d\u4e00\u4e2aIP\u7ed9\u5bb9\u5668\u4f7f\u7528\uff0c\u5e76\u8bbe\u7f6edocker0\u7684IP\u5730\u5740\u4e3a\u5bb9\u5668\u7684\u9ed8\u8ba4\u7f51\u5173\u3002\u5728\u4e3b\u673a\u4e0a\u521b\u5efa\u4e00\u5bf9\u865a\u62df\u7f51\u5361veth pair\u8bbe\u5907\uff0cDocker\u5c06veth pair\u8bbe\u5907\u7684\u4e00\u7aef\u653e\u5728\u65b0\u521b\u5efa\u7684\u5bb9\u5668\u4e2d\uff0c\u5e76\u547d\u540d\u4e3aeth0\uff08\u5bb9\u5668\u7684\u7f51\u5361\uff09\uff0c\u53e6\u4e00\u7aef\u653e\u5728\u4e3b\u673a\u4e2d\uff0c\u4ee5vethxxx\u8fd9\u6837\u7c7b\u4f3c\u7684\u540d\u5b57\u547d\u540d\uff0c\u5e76\u5c06\u8fd9\u4e2a\u7f51\u7edc\u8bbe\u5907\u52a0\u5165\u5230docker0\u7f51\u6865\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7brctl show\u547d\u4ee4\u67e5\u770b\u3002<\/p>\n<p>bridge\u6a21\u5f0f\u662fdocker\u7684\u9ed8\u8ba4\u7f51\u7edc\u6a21\u5f0f\uff0c\u4e0d\u5199&#8211;network\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\u3002<\/p>\n<p>bridge\u6a21\u5f0f\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc5xPE\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5047\u8bbe\u4e0a\u56fe\u7684docker2\u4e2d\u8fd0\u884c\u4e86\u4e00\u4e2anginx\uff0c\u5927\u5bb6\u6765\u60f3\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u540c\u4e3b\u673a\u95f4\u4e24\u4e2a\u5bb9\u5668\u95f4\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u901a\u4fe1\uff1f\u6bd4\u5982\u5728docker1\u4e0a\u80fd\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5230docker2\u7684nginx\u7ad9\u70b9\uff1f<\/li>\n<li>\u5728\u5bbf\u4e3b\u673a\u4e0a\u80fd\u5426\u76f4\u63a5\u8bbf\u95ee\u5230docker2\u7684nginx\u7ad9\u70b9\uff1f<\/li>\n<li>\u5728\u53e6\u4e00\u53f0\u4e3b\u673a\u4e0a\u5982\u4f55\u8bbf\u95eenode1\u4e0a\u7684\u8fd9\u4e2anginx\u7ad9\u70b9\u5462\uff1fDNAT\u53d1\u5e03\uff1f<\/li>\n<\/ul>\n<p>Docker\u7f51\u6865\u662f\u5bbf\u4e3b\u673a\u865a\u62df\u51fa\u6765\u7684\uff0c\u5e76\u4e0d\u662f\u771f\u5b9e\u5b58\u5728\u7684\u7f51\u7edc\u8bbe\u5907\uff0c\u5916\u90e8\u7f51\u7edc\u662f\u65e0\u6cd5\u5bfb\u5740\u5230\u7684\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u5916\u90e8\u7f51\u7edc\u65e0\u6cd5\u901a\u8fc7\u76f4\u63a5Container-IP\u8bbf\u95ee\u5230\u5bb9\u5668\u3002\u5982\u679c\u5bb9\u5668\u5e0c\u671b\u5916\u90e8\u8bbf\u95ee\u80fd\u591f\u8bbf\u95ee\u5230\uff0c\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u5bb9\u5668\u7aef\u53e3\u5230\u5bbf\u4e3b\u4e3b\u673a\uff08\u7aef\u53e3\u6620\u5c04\uff09\uff0c\u5373docker run\u521b\u5efa\u5bb9\u5668\u65f6\u5019\u901a\u8fc7 -p \u6216 -P \u53c2\u6570\u6765\u542f\u7528\uff0c\u8bbf\u95ee\u5bb9\u5668\u7684\u65f6\u5019\u5c31\u901a\u8fc7[\u5bbf\u4e3b\u673aIP]:[\u5bb9\u5668\u7aef\u53e3]\u8bbf\u95ee\u5bb9\u5668\u3002<\/p>\n<h3>container\u6a21\u5f0f<\/h3>\n<p>\u8fd9\u4e2a\u6a21\u5f0f\u6307\u5b9a\u65b0\u521b\u5efa\u7684\u5bb9\u5668\u548c\u5df2\u7ecf\u5b58\u5728\u7684\u4e00\u4e2a\u5bb9\u5668\u5171\u4eab\u4e00\u4e2a Network Namespace\uff0c\u800c\u4e0d\u662f\u548c\u5bbf\u4e3b\u673a\u5171\u4eab\u3002\u65b0\u521b\u5efa\u7684\u5bb9\u5668\u4e0d\u4f1a\u521b\u5efa\u81ea\u5df1\u7684\u7f51\u5361\uff0c\u914d\u7f6e\u81ea\u5df1\u7684 IP\uff0c\u800c\u662f\u548c\u4e00\u4e2a\u6307\u5b9a\u7684\u5bb9\u5668\u5171\u4eab IP\u3001\u7aef\u53e3\u8303\u56f4\u7b49\u3002\u540c\u6837\uff0c\u4e24\u4e2a\u5bb9\u5668\u9664\u4e86\u7f51\u7edc\u65b9\u9762\uff0c\u5176\u4ed6\u7684\u5982\u6587\u4ef6\u7cfb\u7edf\u3001\u8fdb\u7a0b\u5217\u8868\u7b49\u8fd8\u662f\u9694\u79bb\u7684\u3002\u4e24\u4e2a\u5bb9\u5668\u7684\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7 lo \u7f51\u5361\u8bbe\u5907\u901a\u4fe1\u3002<br \/>container\u6a21\u5f0f\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc5xPF\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>host\u6a21\u5f0f<\/h3>\n<p>\u5982\u679c\u542f\u52a8\u5bb9\u5668\u7684\u65f6\u5019\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<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\uff0c\u4e0d\u9700\u8981\u8fdb\u884cNAT\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<p>Host\u6a21\u5f0f\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc5xPG\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>none\u6a21\u5f0f<br \/>\u4f7f\u7528none\u6a21\u5f0f\uff0cDocker\u5bb9\u5668\u62e5\u6709\u81ea\u5df1\u7684Network Namespace\uff0c\u4f46\u662f\uff0c\u5e76\u4e0d\u4e3aDocker\u5bb9\u5668\u8fdb\u884c\u4efb\u4f55\u7f51\u7edc\u914d\u7f6e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u4e2aDocker\u5bb9\u5668\u6ca1\u6709\u7f51\u5361\u3001IP\u3001\u8def\u7531\u7b49\u4fe1\u606f\u3002\u9700\u8981\u6211\u4eec\u81ea\u5df1\u4e3aDocker\u5bb9\u5668\u6dfb\u52a0\u7f51\u5361\u3001\u914d\u7f6eIP\u7b49\u3002<\/p>\n<p>\u8fd9\u79cd\u7f51\u7edc\u6a21\u5f0f\u4e0b\u5bb9\u5668\u53ea\u6709lo\u56de\u73af\u7f51\u7edc\uff0c\u6ca1\u6709\u5176\u4ed6\u7f51\u5361\u3002none\u6a21\u5f0f\u53ef\u4ee5\u5728\u5bb9\u5668\u521b\u5efa\u65f6\u901a\u8fc7&#8211;network none\u6765\u6307\u5b9a\u3002\u8fd9\u79cd\u7c7b\u578b\u7684\u7f51\u7edc\u6ca1\u6709\u529e\u6cd5\u8054\u7f51\uff0c\u5c01\u95ed\u7684\u7f51\u7edc\u80fd\u5f88\u597d\u7684\u4fdd\u8bc1\u5bb9\u5668\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<ul>\n<li>\u542f\u52a8\u4e00\u4e2a\u5bb9\u5668\u5904\u7406\u6570\u636e\uff0c\u6bd4\u5982\u8f6c\u6362\u6570\u636e\u683c\u5f0f<\/li>\n<li>\u4e00\u4e9b\u540e\u53f0\u7684\u8ba1\u7b97\u548c\u5904\u7406\u4efb\u52a1<\/li>\n<\/ul>\n<p>none\u6a21\u5f0f\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc5xPH\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<pre><code class=\"mipsasm\">docker network inspect bridge   #\u67e5\u770bbridge\u7f51\u7edc\u7684\u8be6\u7ec6\u914d\u7f6e<\/code><\/pre>\n<h2>docker\u5bb9\u5668\u7f51\u7edc\u914d\u7f6e<\/h2>\n<h3>Linux\u5185\u6838\u5b9e\u73b0\u540d\u79f0\u7a7a\u95f4\u7684\u521b\u5efa<\/h3>\n<h4>ip netns\u547d\u4ee4<\/h4>\n<p>\u53ef\u4ee5\u501f\u52a9ip netns\u547d\u4ee4\u6765\u5b8c\u6210\u5bf9 Network Namespace \u7684\u5404\u79cd\u64cd\u4f5c\u3002ip netns\u547d\u4ee4\u6765\u81ea\u4e8eiproute\u5b89\u88c5\u5305\uff0c\u4e00\u822c\u7cfb\u7edf\u4f1a\u9ed8\u8ba4\u5b89\u88c5\uff0c\u5982\u679c\u6ca1\u6709\u7684\u8bdd\uff0c\u8bf7\u81ea\u884c\u5b89\u88c5\u3002<\/p>\n<p>\u6ce8\u610f\uff1aip netns\u547d\u4ee4\u4fee\u6539\u7f51\u7edc\u914d\u7f6e\u65f6\u9700\u8981 sudo \u6743\u9650\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7ip netns\u547d\u4ee4\u5b8c\u6210\u5bf9Network Namespace \u7684\u76f8\u5173\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7ip netns help\u67e5\u770b\u547d\u4ee4\u5e2e\u52a9\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"x86asm\">[root@localhost ~]# ip netns help\nUsage: ip netns list\n       ip netns add NAME\n       ip netns set NAME NETNSID\n       ip [-all] netns delete [NAME]\n       ip netns identify [PID]\n       ip netns pids NAME\n       ip [-all] netns exec [NAME] cmd ...\n       ip netns monitor\n       ip netns list-id<\/code><\/pre>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cLinux\u7cfb\u7edf\u4e2d\u662f\u6ca1\u6709\u4efb\u4f55 Network Namespace\u7684\uff0c\u6240\u4ee5ip netns list\u547d\u4ee4\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u4fe1\u606f\u3002<\/p>\n<h3>\u521b\u5efaNetwork Namespace<\/h3>\n<p>\u901a\u8fc7\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u540d\u4e3ans0\u7684\u547d\u540d\u7a7a\u95f4\uff1a<\/p>\n<pre><code class=\"csharp\">[root@localhost ~]# ip netns list\n[root@localhost ~]# ip netns add ns0\n[root@localhost ~]# ip netns list\nns0<\/code><\/pre>\n<p>\u65b0\u521b\u5efa\u7684 Network Namespace \u4f1a\u51fa\u73b0\u5728\/var\/run\/netns\/\u76ee\u5f55\u4e0b\u3002\u5982\u679c\u76f8\u540c\u540d\u5b57\u7684 namespace \u5df2\u7ecf\u5b58\u5728\uff0c\u547d\u4ee4\u4f1a\u62a5Cannot create namespace file &#8220;\/var\/run\/netns\/ns0&#8221;: File exists\u7684\u9519\u8bef\u3002<\/p>\n<pre><code class=\"perl\">[root@localhost ~]# ls \/var\/run\/netns\/\nns0\n[root@localhost ~]# ip netns add ns0\nCannot create namespace file &quot;\/var\/run\/netns\/ns0&quot;: File exists<\/code><\/pre>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a Network Namespace \u6765\u8bf4\uff0c\u5b83\u4f1a\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u7f51\u5361\u3001\u8def\u7531\u8868\u3001ARP \u8868\u3001iptables \u7b49\u548c\u7f51\u7edc\u76f8\u5173\u7684\u8d44\u6e90\u3002<\/p>\n<h3>\u64cd\u4f5cNetwork Namespace<\/h3>\n<p>ip\u547d\u4ee4\u63d0\u4f9b\u4e86ip netns exec\u5b50\u547d\u4ee4\u53ef\u4ee5\u5728\u5bf9\u5e94\u7684 Network Namespace \u4e2d\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<p>\u67e5\u770b\u65b0\u521b\u5efa Network Namespace \u7684\u7f51\u5361\u4fe1\u606f<\/p>\n<pre><code class=\"csharp\">[root@localhost ~]# ip netns exec ns0 ip addr\n1: lo: &lt;LOOPBACK&gt; mtu 65536 qdisc noop state DOWN group default qlen 1000\n    link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u65b0\u521b\u5efa\u7684Network Namespace\u4e2d\u4f1a\u9ed8\u8ba4\u521b\u5efa\u4e00\u4e2alo\u56de\u73af\u7f51\u5361\uff0c\u6b64\u65f6\u7f51\u5361\u5904\u4e8e\u5173\u95ed\u72b6\u6001\u3002\u6b64\u65f6\uff0c\u5c1d\u8bd5\u53bb ping \u8be5lo\u56de\u73af\u7f51\u5361\uff0c\u4f1a\u63d0\u793aNetwork is unreachable<\/p>\n<pre><code class=\"csharp\">[root@localhost ~]# ip netns exec ns0 ping 127.0.0.1\nconnect: Network is unreachable\n127.0.0.1\u662f\u9ed8\u8ba4\u56de\u73af\u7f51\u5361<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u542f\u7528lo\u56de\u73af\u7f51\u5361\uff1a<\/p>\n<pre><code class=\"sql\">[root@localhost ~]# ip netns exec ns0 ip link set lo up\n[root@localhost ~]# ip netns exec ns0 ping 127.0.0.1\nPING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.\n64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.029 ms\n64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.029 ms\n^C\n--- 127.0.0.1 ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1036ms\nrtt min\/avg\/max\/mdev = 0.029\/0.029\/0.029\/0.000 ms<\/code><\/pre>\n<h3>\u8f6c\u79fb\u8bbe\u5907<\/h3>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u540c\u7684 Network Namespace \u4e4b\u95f4\u8f6c\u79fb\u8bbe\u5907\uff08\u5982veth\uff09\u3002\u7531\u4e8e\u4e00\u4e2a\u8bbe\u5907\u53ea\u80fd\u5c5e\u4e8e\u4e00\u4e2a Network Namespace \uff0c\u6240\u4ee5\u8f6c\u79fb\u540e\u5728\u8fd9\u4e2a Network Namespace \u5185\u5c31\u770b\u4e0d\u5230\u8fd9\u4e2a\u8bbe\u5907\u4e86\u3002<\/p>\n<p>\u5176\u4e2d\uff0cveth\u8bbe\u5907\u5c5e\u4e8e\u53ef\u8f6c\u79fb\u8bbe\u5907\uff0c\u800c\u5f88\u591a\u5176\u5b83\u8bbe\u5907\uff08\u5982lo\u3001vxlan\u3001ppp\u3001bridge\u7b49\uff09\u662f\u4e0d\u53ef\u4ee5\u8f6c\u79fb\u7684\u3002<\/p>\n<h3>veth pair<\/h3>\n<p>veth pair \u5168\u79f0\u662f Virtual Ethernet Pair\uff0c\u662f\u4e00\u4e2a\u6210\u5bf9\u7684\u7aef\u53e3\uff0c\u6240\u6709\u4ece\u8fd9\u5bf9\u7aef\u53e3\u4e00 \u7aef\u8fdb\u5165\u7684\u6570\u636e\u5305\u90fd\u5c06\u4ece\u53e6\u4e00\u7aef\u51fa\u6765\uff0c\u53cd\u4e4b\u4e5f\u662f\u4e00\u6837\u3002<br \/>\u5f15\u5165veth pair\u662f\u4e3a\u4e86\u5728\u4e0d\u540c\u7684 Network Namespace \u76f4\u63a5\u8fdb\u884c\u901a\u4fe1\uff0c\u5229\u7528\u5b83\u53ef\u4ee5\u76f4\u63a5\u5c06\u4e24\u4e2a Network Namespace \u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc5xPI\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>\u521b\u5efaveth pair<\/h3>\n<pre><code class=\"perl\">[root@localhost ~]# ip link add type veth\n[root@localhost ~]# ip a\n\n4: veth0@veth1: &lt;BROADCAST,MULTICAST,M-DOWN&gt; mtu 1500 qdisc noop state DOWN group default qlen 1000\n    link\/ether 0a:f4:e2:2d:37:fb brd ff:ff:ff:ff:ff:ff\n5: veth1@veth0: &lt;BROADCAST,MULTICAST,M-DOWN&gt; mtu 1500 qdisc noop state DOWN group default qlen 1000\n    link\/ether 5e:7e:f6:59:f0:4f brd ff:ff:ff:ff:ff:ff<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6b64\u65f6\u7cfb\u7edf\u4e2d\u65b0\u589e\u4e86\u4e00\u5bf9veth pair\uff0c\u5c06veth0\u548cveth1\u4e24\u4e2a\u865a\u62df\u7f51\u5361\u8fde\u63a5\u4e86\u8d77\u6765\uff0c\u6b64\u65f6\u8fd9\u5bf9 veth pair \u5904\u4e8e\u201d\u672a\u542f\u7528\u201c\u72b6\u6001\u3002<\/p>\n<h3>\u5b9e\u73b0Network Namespace\u95f4\u901a\u4fe1<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u5229\u7528veth pair\u5b9e\u73b0\u4e24\u4e2a\u4e0d\u540c\u7684 Network Namespace \u4e4b\u95f4\u7684\u901a\u4fe1\u3002\u521a\u624d\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3ans0\u7684 Network Namespace\uff0c\u4e0b\u9762\u518d\u521b\u5efa\u4e00\u4e2a\u4fe1\u606fNetwork Namespace\uff0c\u547d\u540d\u4e3ans1<\/p>\n<pre><code class=\"csharp\">[root@localhost ~]# ip netns add ns1\n[root@localhost ~]# ip netns list\nns1\nns0<\/code><\/pre>\n<p>\u7136\u540e\u6211\u4eec\u5c06veth0\u52a0\u5165\u5230ns0\uff0c\u5c06veth1\u52a0\u5165\u5230ns1<\/p>\n<pre><code class=\"csharp\">[root@localhost ~]# ip link set veth0 netns ns0\n[root@localhost ~]# ip link set veth1 netns ns1<\/code><\/pre>\n<p>\u7136\u540e\u6211\u4eec\u5206\u522b\u4e3a\u8fd9\u5bf9veth pair\u914d\u7f6e\u4e0aip\u5730\u5740\uff0c\u5e76\u542f\u7528\u5b83\u4eec<\/p>\n<pre><code class=\"sql\">[root@localhost ~]# ip netns exec ns0 ip link set veth0 up\n[root@localhost ~]# ip netns exec ns0 ip addr add 192.0.0.1\/24 dev veth0\n[root@localhost ~]# ip netns exec ns1 ip link set veth1 up\n[root@localhost ~]# ip netns exec ns1 ip addr add 192.0.0.2\/24 dev veth1<\/code><\/pre>\n<p>\u67e5\u770b\u8fd9\u5bf9veth pair\u7684\u72b6\u6001<\/p>\n<pre><code class=\"sql\">[root@localhost ~]# ip netns exec ns0 ip a\n1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000\n    link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n    inet 127.0.0.1\/8 scope host lo\n       valid_lft forever preferred_lft forever\n    inet6 ::1\/128 scope host\n       valid_lft forever preferred_lft forever\n4: veth0@if5: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default qlen 1000\n    link\/ether 0a:f4:e2:2d:37:fb brd ff:ff:ff:ff:ff:ff link-netns ns1\n    inet 192.0.0.1\/24 scope global veth0\n       valid_lft forever preferred_lft forever\n    inet6 fe80::8f4:e2ff:fe2d:37fb\/64 scope link\n       valid_lft forever preferred_lft forever<\/code><\/pre>\n<pre><code class=\"sql\">[root@localhost ~]# ip netns exec ns1 ip a\n1: lo: &lt;LOOPBACK&gt; mtu 65536 qdisc noop state DOWN group default qlen 1000\n    link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n5: veth1@if4: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default qlen 1000\n    link\/ether 5e:7e:f6:59:f0:4f brd ff:ff:ff:ff:ff:ff link-netns ns0\n    inet 192.0.0.2\/24 scope global veth1\n       valid_lft forever preferred_lft forever\n    inet6 fe80::5c7e:f6ff:fe59:f04f\/64 scope link\n       valid_lft forever preferred_lft forever<\/code><\/pre>\n<p>\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u51fa\uff0c\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u542f\u7528\u4e86\u8fd9\u4e2aveth pair\uff0c\u5e76\u4e3a\u6bcf\u4e2aveth\u8bbe\u5907\u5206\u914d\u4e86\u5bf9\u5e94\u7684ip\u5730\u5740\u3002\u6211\u4eec\u5c1d\u8bd5\u5728ns1\u4e2d\u8bbf\u95eens0\u4e2d\u7684ip\u5730\u5740<\/p>\n<pre><code class=\"python\">[root@localhost ~]# ip netns exec ns1 ping 192.0.0.1\nPING 192.0.0.1 (192.0.0.1) 56(84) bytes of data.\n64 bytes from 192.0.0.1: icmp_seq=1 ttl=64 time=0.033 ms\n64 bytes from 192.0.0.1: icmp_seq=2 ttl=64 time=0.041 ms\n^C\n--- 192.0.0.1 ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1001ms\nrtt min\/avg\/max\/mdev = 0.033\/0.037\/0.041\/0.004 ms\n[root@localhost ~]# ip netns exec ns0 ping 192.0.0.2\nPING 192.0.0.2 (192.0.0.2) 56(84) bytes of data.\n64 bytes from 192.0.0.2: icmp_seq=1 ttl=64 time=0.025 ms\n64 bytes from 192.0.0.2: icmp_seq=2 ttl=64 time=0.025 ms\n^C\n--- 192.0.0.2 ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1038ms\nrtt min\/avg\/max\/mdev = 0.025\/0.025\/0.025\/0.000 ms<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0cveth pair\u6210\u529f\u5b9e\u73b0\u4e86\u4e24\u4e2a\u4e0d\u540cNetwork Namespace\u4e4b\u95f4\u7684\u7f51\u7edc\u4ea4\u4e92\u3002<\/p>\n<h2>\u56db\u79cd\u7f51\u7edc\u6a21\u5f0f\u914d\u7f6e<\/h2>\n<h3>bridge\u6a21\u5f0f\u914d\u7f6e<\/h3>\n<pre><code class=\"python\">[root@localhost ~]# docker run -it --name ti --rm busybox\n\/ # ifconfig\neth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02\n          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:12 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:1032 (1.0 KiB)  TX bytes:0 (0.0 B)\n\nlo        Link encap:Local Loopback\n          inet addr:127.0.0.1  Mask:255.0.0.0\n          UP LOOPBACK RUNNING  MTU:65536  Metric:1\n          RX packets:0 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:1000\n          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)<\/code><\/pre>\n<p>\u5728\u521b\u5efa\u5bb9\u5668\u65f6\u6dfb\u52a0&#8211;network bridge\u4e0e\u4e0d\u52a0&#8211;network\u9009\u9879\u6548\u679c\u662f\u4e00\u81f4\u7684<\/p>\n<pre><code class=\"python\"> [root@localhost ~]# docker run -it --name t1 --network bridge --rm busybox\n\/ # ifconfig\neth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02\n          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:8 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:696 (696.0 B)  TX bytes:0 (0.0 B)\n\nlo        Link encap:Local Loopback\n          inet addr:127.0.0.1  Mask:255.0.0.0\n          UP LOOPBACK RUNNING  MTU:65536  Metric:1\n          RX packets:0 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:1000\n          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)<\/code><\/pre>\n<h3>none\u6a21\u5f0f\u914d\u7f6e<\/h3>\n<pre><code class=\"css\">[root@localhost ~]# docker run -it --name t1 --network none --rm busybox\n\/ # ifconfig -a\nlo        Link encap:Local Loopback\n          inet addr:127.0.0.1  Mask:255.0.0.0\n          UP LOOPBACK RUNNING  MTU:65536  Metric:1\n          RX packets:0 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:1000\n          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)<\/code><\/pre>\n<h3>container\u6a21\u5f0f\u914d\u7f6e<\/h3>\n<p>\u542f\u52a8\u7b2c\u4e00\u4e2a\u5bb9\u5668<\/p>\n<pre><code class=\"ruby\">[root@localhost ~]# docker run -dit --name b3 busybox\naf5ba32f990ebf5a46d7ecaf1eec67f1712bbef6ad7df37d52b7a8a498a592a0\n\n[root@localhost ~]# docker exec -it b3 \/bin\/sh\n\/ # ifconfig\neth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02\n          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:11 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:906 (906.0 B)  TX bytes:0 (0.0 B)<\/code><\/pre>\n<p>\u542f\u52a8\u7b2c\u4e8c\u4e2a\u5bb9\u5668<\/p>\n<pre><code class=\"python\">[root@localhost ~]# docker run -it --name b2 --rm busybox\n\/ # ifconfig\neth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:03\n          inet addr:172.17.0.3  Bcast:172.17.255.255  Mask:255.255.0.0\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:6 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:516 (516.0 B)  TX bytes:0 (0.0 B)<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u540d\u4e3ab2\u7684\u5bb9\u5668IP\u5730\u5740\u662f10.0.0.3\uff0c\u4e0e\u7b2c\u4e00\u4e2a\u5bb9\u5668\u7684IP\u5730\u5740\u4e0d\u662f\u4e00\u6837\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u5e76\u6ca1\u6709\u5171\u4eab\u7f51\u7edc\uff0c\u6b64\u65f6\u5982\u679c\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a\u5bb9\u5668\u7684\u542f\u52a8\u65b9\u5f0f\u6539\u53d8\u4e00\u4e0b\uff0c\u5c31\u53ef\u4ee5\u4f7f\u540d\u4e3ab2\u7684\u5bb9\u5668IP\u4e0eB3\u5bb9\u5668IP\u4e00\u81f4\uff0c\u4e5f\u5373\u5171\u4eabIP\uff0c\u4f46\u4e0d\u5171\u4eab\u6587\u4ef6\u7cfb\u7edf\u3002<\/p>\n<pre><code class=\"python\">[root@localhost ~]# docker run -it --name b2 --rm --network container:b3 busybox\n\/ # ifconfig\neth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02\n          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:14 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:1116 (1.0 KiB)  TX bytes:0 (0.0 B)\n\n\u6b64\u65f6\u6211\u4eec\u5728b1\u5bb9\u5668\u4e0a\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\n\/ # mkdir \/tmp\/data\n\/ # ls \/tmp\ndata<\/code><\/pre>\n<p>\u5230b2\u5bb9\u5668\u4e0a\u68c0\u67e5\/tmp\u76ee\u5f55\u4f1a\u53d1\u73b0\u5e76\u6ca1\u6709\u8fd9\u4e2a\u76ee\u5f55\uff0c\u56e0\u4e3a\u6587\u4ef6\u7cfb\u7edf\u662f\u5904\u4e8e\u9694\u79bb\u72b6\u6001\uff0c\u4ec5\u4ec5\u662f\u5171\u4eab\u4e86\u7f51\u7edc\u800c\u5df2\u3002<br \/>\u5728b2\u5bb9\u5668\u4e0a\u90e8\u7f72\u4e00\u4e2a\u7ad9\u70b9<\/p>\n<pre><code class=\"ruby\">\/ # echo 'hello world' &gt; \/tmp\/index.html\n\/ # ls \/tmp\nindex.html\n\/ # httpd -h \/tmp\n\/ # netstat -antl\nActive Internet connections (servers and established)\nProto Recv-Q Send-Q Local Address           Foreign Address         State\ntcp        0      0 :::80                   :::*                    LISTEN<\/code><\/pre>\n<p>\u5728b1\u5bb9\u5668\u4e0a\u7528\u672c\u5730\u5730\u5740\u53bb\u8bbf\u95ee\u6b64\u7ad9\u70b9<\/p>\n<pre><code class=\"bash\">\/ # wget -O - -q 172.17.0.2:80\nhello world<\/code><\/pre>\n<h3>host\u6a21\u5f0f\u914d\u7f6e<\/h3>\n<p>\u542f\u52a8\u5bb9\u5668\u65f6\u76f4\u63a5\u6307\u660e\u6a21\u5f0f\u4e3ahost<\/p>\n<pre><code class=\"sql\">[root@localhost ~]# docker run -it --name b2 --rm --network host busybox\n\/ # ifconfig\ndocker0   Link encap:Ethernet  HWaddr 02:42:B8:7F:8E:2C\n          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0\n          inet6 addr: fe80::42:b8ff:fe7f:8e2c\/64 Scope:Link\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:3 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:20 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:116 (116.0 B)  TX bytes:1664 (1.6 KiB)\n\nens33     Link encap:Ethernet  HWaddr 00:0C:29:95:19:47\n          inet addr:192.168.203.138  Bcast:192.168.203.255  Mask:255.255.255.0\n          inet6 addr: fe80::2e61:1ea3:c05a:3d9b\/64 Scope:Link\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:9626 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:3950 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:1000\n          RX bytes:3779562 (3.6 MiB)  TX bytes:362386 (353.8 KiB)\n\nlo        Link encap:Local Loopback\n          inet addr:127.0.0.1  Mask:255.0.0.0\n          inet6 addr: ::1\/128 Scope:Host\n          UP LOOPBACK RUNNING  MTU:65536  Metric:1\n          RX packets:0 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:1000\n          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)\n\nveth09ee47e Link encap:Ethernet  HWaddr B2:10:53:7B:66:AE\n          inet6 addr: fe80::b010:53ff:fe7b:66ae\/64 Scope:Link\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:3 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:19 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:158 (158.0 B)  TX bytes:1394 (1.3 KiB)<\/code><\/pre>\n<p>\u6b64\u65f6\u5982\u679c\u6211\u4eec\u5728\u8fd9\u4e2a\u5bb9\u5668\u4e2d\u542f\u52a8\u4e00\u4e2ahttp\u7ad9\u70b9\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u7528\u5bbf\u4e3b\u673a\u7684IP\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\u8fd9\u4e2a\u5bb9\u5668\u4e2d\u7684\u7ad9\u70b9\u4e86\u3002<\/p>\n<h2>\u5bb9\u5668\u7684\u5e38\u7528\u64cd\u4f5c<\/h2>\n<h3>\u67e5\u770b\u5bb9\u5668\u7684\u4e3b\u673a\u540d<\/h3>\n<pre><code class=\"css\">[root@localhost ~]# docker run -it --name t1 --network bridge --rm busybox\n\/ # hostname\n48cb45a0b2e7<\/code><\/pre>\n<h3>\u5728\u5bb9\u5668\u542f\u52a8\u65f6\u6ce8\u5165\u4e3b\u673a\u540d<\/h3>\n<pre><code class=\"python\">[root@localhost ~]# docker run -it --name t1 --network bridge --hostname ljl --rm busybox\n\/ # hostname\nljl\n\/ # cat \/etc\/hosts\n127.0.0.1    localhost\n::1    localhost ip6-localhost ip6-loopback\nfe00::0    ip6-localnet\nff00::0    ip6-mcastprefix\nff02::1    ip6-allnodes\nff02::2    ip6-allrouters\n172.17.0.3    ljl\n\/ # cat \/etc\/resolv.conf\n# Generated by NetworkManager\nsearch localdomain\nnameserver 192.168.203.2\n\/ # ping www.baidu.com\nPING www.baidu.com (182.61.200.7): 56 data bytes\n64 bytes from 182.61.200.7: seq=0 ttl=127 time=31.929 ms\n64 bytes from 182.61.200.7: seq=1 ttl=127 time=41.062 ms\n64 bytes from 182.61.200.7: seq=2 ttl=127 time=31.540 ms\n^C\n--- www.baidu.com ping statistics ---\n3 packets transmitted, 3 packets received, 0% packet loss\nround-trip min\/avg\/max = 31.540\/34.843\/41.062 ms<\/code><\/pre>\n<h3>\u624b\u52a8\u6307\u5b9a\u5bb9\u5668\u8981\u4f7f\u7528\u7684DNS<\/h3>\n<pre><code class=\"yaml\">[root@localhost ~]# docker run -it --name t1 --network bridge --hostname ljl --dns 114.114.114.114 --rm busybox\n\/ # cat \/etc\/resolv.conf\nsearch localdomain\nnameserver 114.114.114.114\n\/ # nslookup -type=a www.baidu.com\nServer:        114.114.114.114\nAddress:    114.114.114.114:53\n\nNon-authoritative answer:\nwww.baidu.com    canonical name = www.a.shifen.com\nName:    www.a.shifen.com\nAddress: 182.61.200.6\nName:    www.a.shifen.com\nAddress: 182.61.200.7<\/code><\/pre>\n<h3>\u624b\u52a8\u5f80\/etc\/hosts\u6587\u4ef6\u4e2d\u6ce8\u5165\u4e3b\u673a\u540d\u5230IP\u5730\u5740\u7684\u6620\u5c04<\/h3>\n<pre><code class=\"makefile\">[root@localhost ~]# docker run -it --name t1 --network bridge --hostname ljl --add-host www.a.com:1.1.1.1 --rm busybox\n\/ # cat \/etc\/hosts\n127.0.0.1    localhost\n::1    localhost ip6-localhost ip6-loopback\nfe00::0    ip6-localnet\nff00::0    ip6-mcastprefix\nff02::1    ip6-allnodes\nff02::2    ip6-allrouters\n1.1.1.1    www.a.com\n172.17.0.3    ljl<\/code><\/pre>\n<h3>\u5f00\u653e\u5bb9\u5668\u7aef\u53e3<\/h3>\n<p>\u6267\u884cdocker run\u7684\u65f6\u5019\u6709\u4e2a-p\u9009\u9879\uff0c\u53ef\u4ee5\u5c06\u5bb9\u5668\u4e2d\u7684\u5e94\u7528\u7aef\u53e3\u6620\u5c04\u5230\u5bbf\u4e3b\u673a\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u8ba9\u5916\u90e8\u4e3b\u673a\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5bbf\u4e3b\u673a\u7684\u67d0\u7aef\u53e3\u6765\u8bbf\u95ee\u5bb9\u5668\u5185\u5e94\u7528\u7684\u76ee\u7684\u3002<\/p>\n<p>-p\u9009\u9879\u80fd\u591f\u4f7f\u7528\u591a\u6b21\uff0c\u5176\u6240\u80fd\u591f\u66b4\u9732\u7684\u7aef\u53e3\u5fc5\u987b\u662f\u5bb9\u5668\u786e\u5b9e\u5728\u76d1\u542c\u7684\u7aef\u53e3\u3002<\/p>\n<p>-p\u9009\u9879\u7684\u4f7f\u7528\u683c\u5f0f\uff1a<\/p>\n<ul>\n<li>-p containerPort<\/li>\n<li>\u5c06\u6307\u5b9a\u7684\u5bb9\u5668\u7aef\u53e3\u6620\u5c04\u81f3\u4e3b\u673a\u6240\u6709\u5730\u5740\u7684\u4e00\u4e2a\u52a8\u6001\u7aef\u53e3<\/li>\n<li>-p hostPort : containerPort<\/li>\n<li>\u5c06\u5bb9\u5668\u7aef\u53e3 containerPort \u6620\u5c04\u81f3\u6307\u5b9a\u7684\u4e3b\u673a\u7aef\u53e3 hostPort<\/li>\n<li>-p ip :: containerPort<\/li>\n<li>\u5c06\u6307\u5b9a\u7684\u5bb9\u5668\u7aef\u53e3 containerPort \u6620\u5c04\u81f3\u4e3b\u673a\u6307\u5b9a ip \u7684\u52a8\u6001\u7aef\u53e3<\/li>\n<li>-p ip : hostPort : containerPort<\/li>\n<li>\u5c06\u6307\u5b9a\u7684\u5bb9\u5668\u7aef\u53e3 containerPort \u6620\u5c04\u81f3\u4e3b\u673a\u6307\u5b9a ip \u7684\u7aef\u53e3 hostPort<\/li>\n<\/ul>\n<p>\u52a8\u6001\u7aef\u53e3\u6307\u7684\u662f\u968f\u673a\u7aef\u53e3\uff0c\u5177\u4f53\u7684\u6620\u5c04\u7ed3\u679c\u53ef\u4f7f\u7528docker port\u547d\u4ee4\u67e5\u770b\u3002<\/p>\n<pre><code class=\"ruby\">[root@localhost ~]# docker run -dit --name web1 -p 192.168.203.138::80 httpd\ne97bc1774e40132659990090f0e98a308a7f83986610ca89037713e9af8a6b9f\n[root@localhost ~]# docker ps\nCONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                           NAMES\ne97bc1774e40   httpd     &quot;httpd-foreground&quot;   6 seconds ago    Up 5 seconds    192.168.203.138:49153-&gt;80\/tcp   web1\naf5ba32f990e   busybox   &quot;sh&quot;                 48 minutes ago   Up 48 minutes                                   b3\n[root@localhost ~]# ss -antl\nState    Recv-Q   Send-Q        Local Address:Port        Peer Address:Port   Process\nLISTEN   0        128         192.168.203.138:49153            0.0.0.0:*\nLISTEN   0        128                 0.0.0.0:22               0.0.0.0:*\nLISTEN   0        128                    [::]:22                  [::]:*<\/code><\/pre>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u6267\u884c\u540e\u4f1a\u4e00\u76f4\u5360\u7528\u7740\u524d\u7aef\uff0c\u6211\u4eec\u65b0\u5f00\u4e00\u4e2a\u7ec8\u7aef\u8fde\u63a5\u6765\u770b\u4e00\u4e0b\u5bb9\u5668\u768480\u7aef\u53e3\u88ab\u6620\u5c04\u5230\u4e86\u5bbf\u4e3b\u673a\u7684\u4ec0\u4e48\u7aef\u53e3\u4e0a<\/p>\n<pre><code class=\"bash\">[root@localhost ~]# docker port web1\n80\/tcp -&gt; 192.168.203.138:49153<\/code><\/pre>\n<p>\u7531\u6b64\u53ef\u89c1\uff0c\u5bb9\u5668\u768480\u7aef\u53e3\u88ab\u66b4\u9732\u5230\u4e86\u5bbf\u4e3b\u673a\u768449153\u7aef\u53e3\u4e0a\uff0c\u6b64\u65f6\u6211\u4eec\u5728\u5bbf\u4e3b\u673a\u4e0a\u8bbf\u95ee\u4e00\u4e0b\u8fd9\u4e2a\u7aef\u53e3\u770b\u662f\u5426\u80fd\u8bbf\u95ee\u5230\u5bb9\u5668\u5185\u7684\u7ad9\u70b9<\/p>\n<pre><code class=\"xml\">[root@localhost ~]# curl http:\/\/192.168.203.138:49153\n&lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;<\/code><\/pre>\n<p>iptables\u9632\u706b\u5899\u89c4\u5219\u5c06\u968f\u5bb9\u5668\u7684\u521b\u5efa\u81ea\u52a8\u751f\u6210\uff0c\u968f\u5bb9\u5668\u7684\u5220\u9664\u81ea\u52a8\u5220\u9664\u89c4\u5219\u3002<\/p>\n<pre><code class=\"sql\">[root@localhost ~]# iptables -t nat -nvL\nChain PREROUTING (policy ACCEPT 0 packets, 0 bytes)\n pkts bytes target     prot opt in     out     source               destination\n    3   164 DOCKER     all  --  *      *       0.0.0.0\/0            0.0.0.0\/0            ADDRTYPE match dst-type LOCAL\n\nChain INPUT (policy ACCEPT 0 packets, 0 bytes)\n pkts bytes target     prot opt in     out     source               destination\n\nChain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)\n pkts bytes target     prot opt in     out     source               destination\n    4   261 MASQUERADE  all  --  *      !docker0  172.17.0.0\/16        0.0.0.0\/0\n    0     0 MASQUERADE  tcp  --  *      *       172.17.0.3           172.17.0.3           tcp dpt:80\n\nChain OUTPUT (policy ACCEPT 0 packets, 0 bytes)\n pkts bytes target     prot opt in     out     source               destination\n    2   120 DOCKER     all  --  *      *       0.0.0.0\/0           !127.0.0.0\/8          ADDRTYPE match dst-type LOCAL\n\nChain DOCKER (2 references)\n pkts bytes target     prot opt in     out     source               destination\n    1    60 RETURN     all  --  docker0 *       0.0.0.0\/0            0.0.0.0\/0\n    1    60 DNAT       tcp  --  !docker0 *       0.0.0.0\/0            192.168.203.138      tcp dpt:49153 to:172.17.0.3:80<\/code><\/pre>\n<p>\u5c06\u5bb9\u5668\u7aef\u53e3\u6620\u5c04\u5230\u6307\u5b9aIP\u7684\u968f\u673a\u7aef\u53e3<\/p>\n<pre><code class=\"css\">[root@localhost ~]# docker run -dit --name web1 -p 192.168.203.138::80 httpd<\/code><\/pre>\n<p>\u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u4e0a\u67e5\u770b\u7aef\u53e3\u6620\u5c04\u60c5\u51b5<\/p>\n<pre><code class=\"bash\">[root@localhost ~]# docker port web1\n80\/tcp -&gt; 192.168.203.138:49153<\/code><\/pre>\n<h3>\u81ea\u5b9a\u4e49docker0\u6865\u7684\u7f51\u7edc\u5c5e\u6027\u4fe1\u606f<\/h3>\n<p>\u81ea\u5b9a\u4e49docker0\u6865\u7684\u7f51\u7edc\u5c5e\u6027\u4fe1\u606f\u9700\u8981\u4fee\u6539\/etc\/docker\/daemon.json\u914d\u7f6e\u6587\u4ef6<\/p>\n<pre><code class=\"csharp\">[root@localhost ~]# cd \/etc\/docker\/\n[root@localhost docker]# vim daemon.json\n[root@localhost docker]# systemctl daemon-reload\n[root@localhost docker]# systemctl restart docker\n\n{\n    &quot;registry-mirrors&quot;: [&quot;https:\/\/4hygggbu.mirror.aliyuncs.com\/&quot;],\n    &quot;bip&quot;: &quot;192.168.1.5\/24&quot;\n}\nEOF<\/code><\/pre>\n<pre><code class=\"ruby\">[root@localhost ~]# vim \/lib\/systemd\/system\/docker.service\n\nExecStart=\/usr\/bin\/dockerd -H fd:\/\/ --containerd=\/run\/containerd\/containerd.sock -H tcp:\/\/0.0.0.0:2375  -H unix:\/\/\/var\/run\/docker.sock\n[root@localhost ~]# systemctl daemon-reload\n[root@localhost ~]# systemctl restart docker<\/code><\/pre>\n<p>\u5728\u5ba2\u6237\u7aef\u4e0a\u5411dockerd\u76f4\u63a5\u4f20\u9012\u201c-H|&#8211;host\u201d\u9009\u9879\u6307\u5b9a\u8981\u63a7\u5236\u54ea\u53f0\u4e3b\u673a\u4e0a\u7684docker\u5bb9\u5668<\/p>\n<pre><code class=\"bash\">[root@localhost ~]# docker -H 192.168.203.138:2375 ps\nCONTAINER ID   IMAGE     COMMAND              CREATED             STATUS          PORTS                           NAMES\ne97bc1774e40   httpd     &quot;httpd-foreground&quot;   30 minutes ago      Up 11 seconds   192.168.203.138:49153-&gt;80\/tcp   web1\naf5ba32f990e   busybox   &quot;sh&quot;                 About an hour ago   Up 14 seconds                                   b3<\/code><\/pre>\n<p>\u521b\u5efa\u65b0\u7f51\u7edc<\/p>\n<pre><code class=\"sql\">[root@localhost ~]# docker network create ljl -d bridge\n883eda50812bb214c04986ca110dbbcb7600eba8b033f2084cd4d750b0436e12\n[root@localhost ~]# docker network ls\nNETWORK ID     NAME      DRIVER    SCOPE\n0c5f4f114c27   bridge    bridge    local\n8c2d14f1fb82   host      host      local\n883eda50812b   ljl       bridge    local\n85ed12d38815   none      null      local<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u989d\u5916\u7684\u81ea\u5b9a\u4e49\u6865\uff0c\u533a\u522b\u4e8edocker0<\/p>\n<pre><code class=\"sql\">[root@localhost ~]# docker network create -d bridge --subnet &quot;192.168.2.0\/24&quot; --gateway &quot;192.168.2.1&quot; br0\naf9ba80deb619de3167939ec5b6d6136a45dce90907695a5bc5ed4608d188b99\n[root@localhost ~]# docker network ls\nNETWORK ID     NAME      DRIVER    SCOPE\naf9ba80deb61   br0       bridge    local\n0c5f4f114c27   bridge    bridge    local\n8c2d14f1fb82   host      host      local\n883eda50812b   ljl       bridge    local\n85ed12d38815   none      null      local<\/code><\/pre>\n<p>\u4f7f\u7528\u65b0\u521b\u5efa\u7684\u81ea\u5b9a\u4e49\u6865\u6765\u521b\u5efa\u5bb9\u5668\uff1a<\/p>\n<pre><code class=\"ruby\">[root@localhost ~]# docker run -it --name b1 --network br0 busybox\n\/ # ifconfig\neth0      Link encap:Ethernet  HWaddr 02:42:C0:A8:02:02\n          inet addr:192.168.2.2  Bcast:192.168.2.255  Mask:255.255.255.0\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:11 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:962 (962.0 B)  TX bytes:0 (0.0 B)<\/code><\/pre>\n<p>\u518d\u521b\u5efa\u4e00\u4e2a\u5bb9\u5668\uff0c\u4f7f\u7528\u9ed8\u8ba4\u7684bridge\u6865\uff1a<\/p>\n<pre><code class=\"ruby\">[root@localhost ~]# docker run --name b2 -it busybox\n\/ # ls\nbin   dev   etc   home  proc  root  sys   tmp   usr   var\n\/ # ifconfig\neth0      Link encap:Ethernet  HWaddr 02:42:C0:A8:01:03\n          inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:6 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:516 (516.0 B)  TX bytes:0 (0.0 B)<\/code><\/pre>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=o9xb2Geja9I1jZzkVsi5fQ%3D%3D.HevhusHwBTABNT1WOaZh1UcWgWrIinFh2M76NftLKro%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=jhfQUUbOmjRGkPKiw0pUzA%3D%3D.%2BV2CE21eBYJ76%2FiLpD6gLmGEDjVPkiENZMXWeeGDdvcTW9z3wrkcSsk%2FMFp4YWgukC837f9GkTuGbXcBrtQFPw%3D%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=bkysrp8eWNHRkp4qJ%2B8zyA%3D%3D.noyIXEudm09Aene7jDDOsqzM8raXqrfxN%2B8%2BCwkZm7N5joB45V457fdCnuWY5U6y\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=pkWHysJG1bYYnMzVQJ3jgQ%3D%3D.dFOCJPzjCTvV4C5xsqyMBlqzqtbdDkU8U2pwhn401tcXtd6DyTZaDfkaNRxTgtnM4FDW3OzpZsc3O0HN6FcMXA%3D%3D\" rel=\"nofollow\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a href=\"https:\/\/link.segmentfault.com\/?enc=%2Ft0mJTXCGW7jpCHRr810Wg%3D%3D.LoEgJ8Bjj816QGdOdKnAu%2FuUUaPgf7V3yiJ8amt6B86FOpQb65JX1zCyMbnL4VKTVSFQmLuDK63JO8LFvmgoC%2F3jJlNG1vy6whwzclOrf%2BM%3D\" rel=\"nofollow\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043228357\">https:\/\/segmentfault.com\/a\/1190000043228357<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4f5c\u8005\uff1a\u77e5\u77e5\u884c\u884c \/\u672c\u6587\u94fe\u63a5\uff1ahttps:\/\/www.cnblogs.com\/loron&#8230; do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10007040","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10007040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10007040"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10007040\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10007040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10007040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10007040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}