1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| [vagrant@docker-node1 ~]$ docker network create -d bridge my-bridge 7cbd844d6e4cf262eba12277a3e8b4c475a1fb6aa73b9ea312d113d6c519284a [vagrant@docker-node1 ~]$ docker network ls NETWORK ID NAME DRIVER SCOPE 36c9fe545daf bridge bridge local 39385556d8cd host host local 7cbd844d6e4c my-bridge bridge local 7828c2433efd none null local [vagrant@docker-node1 ~]$ brctl show bridge name bridge id STP enabled interfaces br-7cbd844d6e4c 8000.02425dd71cb3 no docker0 8000.0242428b4b10 no veth2657408 veth7687c87 [vagrant@docker-node1 ~]$ docker run -d --name test3 --network my-bridge busybox /bin/sh -c "while true; do sleep 3600; done" [vagrant@docker-node1 ~]$ brctl show bridge name bridge id STP enabled interfaces br-7cbd844d6e4c 8000.02425dd71cb3 no vetha530f6b docker0 8000.0242428b4b10 no veth2657408 veth7687c87 [vagrant@docker-node1 ~]$ docker network ls NETWORK ID NAME DRIVER SCOPE 36c9fe545daf bridge bridge local 39385556d8cd host host local 7cbd844d6e4c my-bridge bridge local 7828c2433efd none null local [vagrant@docker-node1 ~]$ docker network inspect 7cbd844d6e4c [ { "Name": "my-bridge", "Id": "7cbd844d6e4cf262eba12277a3e8b4c475a1fb6aa73b9ea312d113d6c519284a", "Created": "2018-12-16T13:17:25.765613424Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "5adef860247e056b8f8d2fab89892e10573daff5ea598482f53cefe3152700e0": { "Name": "test3", "EndpointID": "19f5d575ad3ff8602c7342f036f024e88c4b97c33a68efa206bd810128b82ddc", "MacAddress": "02:42:ac:12:00:02", "IPv4Address": "172.18.0.2/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ] 连接到test2上 [vagrant@docker-node1 ~]$ docker network connect my-bridge test2 [vagrant@docker-node1 ~]$ docker network inspect 7cbd844d6e4c [ { "Name": "my-bridge", "Id": "7cbd844d6e4cf262eba12277a3e8b4c475a1fb6aa73b9ea312d113d6c519284a", "Created": "2018-12-16T13:17:25.765613424Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "4b7db28e2a457ea6102fd8f06b512041e502dcf54c6d7c362fc5e39f7a0cdf23": { "Name": "test2", "EndpointID": "cf4a07fff8a8684cd179ae747630ceeef32ebd38b2fb5eb9db4d2d35759970fb", "MacAddress": "02:42:ac:12:00:03", "IPv4Address": "172.18.0.3/16", "IPv6Address": "" }, "5adef860247e056b8f8d2fab89892e10573daff5ea598482f53cefe3152700e0": { "Name": "test3", "EndpointID": "19f5d575ad3ff8602c7342f036f024e88c4b97c33a68efa206bd810128b82ddc", "MacAddress": "02:42:ac:12:00:02", "IPv4Address": "172.18.0.2/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ] 查看默认的bridge,都有test2,说明test2 Container及连到了默认的bridge又连到了my-bridge。 [vagrant@docker-node1 ~]$ docker network ls NETWORK ID NAME DRIVER SCOPE 36c9fe545daf bridge bridge local 39385556d8cd host host local 7cbd844d6e4c my-bridge bridge local 7828c2433efd none null local [vagrant@docker-node1 ~]$ docker network inspect 36c9fe545daf [ { "Name": "bridge", "Id": "36c9fe545daf2ab0917fce9b1a8edee5ebdd4cb375b1083439212506f3fe179c", "Created": "2018-12-15T19:24:36.985544361Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.17.0.0/16", "Gateway": "172.17.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "4b7db28e2a457ea6102fd8f06b512041e502dcf54c6d7c362fc5e39f7a0cdf23": { "Name": "test2", "EndpointID": "4ba2bbe1ee63a797b528e25533fd09dda8921cf5f688d4a5a24db4efed7410e2", "MacAddress": "02:42:ac:11:00:03", "IPv4Address": "172.17.0.3/16", "IPv6Address": "" }, "f6c6d9b55defc79cf5a39feff1dde1da336f3aa29e68a7577428aefab0196f6b": { "Name": "test1", "EndpointID": "12a9fa5786c42c486e46bb058e44eb811f8d293ca4a89e1203df134c393d0254", "MacAddress": "02:42:ac:11:00:02", "IPv4Address": "172.17.0.2/16", "IPv6Address": "" } }, "Options": { "com.docker.network.bridge.default_bridge": "true", "com.docker.network.bridge.enable_icc": "true", "com.docker.network.bridge.enable_ip_masquerade": "true", "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", "com.docker.network.bridge.name": "docker0", "com.docker.network.driver.mtu": "1500" }, "Labels": {} } ] [vagrant@docker-node1 ~]$ docker exec test2 ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 9: eth0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever 14: eth1@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff inet 172.18.0.3/16 brd 172.18.255.255 scope global eth1 valid_lft forever preferred_lft forever
[vagrant@docker-node1 ~]$ docker exec -it test3 /bin/sh / # ping 172.18.0.3 PING 172.18.0.3 (172.18.0.3): 56 data bytes 64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.727 ms / # ping test2 PING test2 (172.18.0.3): 56 data bytes 64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.058 ms Container连接到用户自己创建的bridge上,默认是link好的。所以使用Container name也能通。如果连接到docker0则不是。
|