1.8 Docker 网络管理-四种网络模式
2018-05-20
1.8 Docker 网络管理-四种网络模式
host 模式,使用 docker run 时使用 —net=host 指定,docker 使用的网络实际上和宿主机一样,在容器内看到的网卡 ip 是宿主机上的 ip。
container 模式,使用 —net=container:container_id/container_name 多个容器使用共同的网络,看到的 ip 是一样的。
root@ubuntu:~# docker run -itd 45d bash
5666da74e57ec78d723973cce2fb2ed200524e7536b0e6c0f87acfcf5d111606
root@ubuntu:~# docker exec -it 5666 bash
root@5666da74e57e:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:c0:a8:2a:06
inet addr:192.168.42.6 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::42:c0ff:fea8:2a06/64 Scope:Link
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:648 (648.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@5666da74e57e:/# exit
exit
root@ubuntu:~# docker run -it --rm --net=container:5666 45d bash
root@5666da74e57e:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:c0:a8:2a:06
inet addr:192.168.42.6 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::42:c0ff:fea8:2a06/64 Scope:Link
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:648 (648.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@5666da74e57e:/#
none 模式,使用—net=none 指定这种模式,不会配置任何网络
root@ubuntu:~# docker run -it --rm --net=none 45d bash
root@648b3ffc6bc0:/# ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
bridge 模式,使用 —net=bridge 指定默认模式,不用指定默认就是这种模式。这种模式会为每个容器分配一个独立的 Network Namespace。类似于 vmware 的 nat 网络模式。同一个宿主机上的所有容器会在同一个网段下,相互之间是可以通信的。host 模式,使用 docker run 时使用 —net=host 指定,docker 使用的网络实际上和宿主机一样,在容器内看到的网卡 ip 是宿主机上的 ip。
container 模式,使用 —net=container:container_id/container_name 多个容器使用共同的网络,看到的 ip 是一样的。
root@ubuntu:~# docker run -itd 45d bash
5666da74e57ec78d723973cce2fb2ed200524e7536b0e6c0f87acfcf5d111606
root@ubuntu:~# docker exec -it 5666 bash
root@5666da74e57e:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:c0:a8:2a:06
inet addr:192.168.42.6 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::42:c0ff:fea8:2a06/64 Scope:Link
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:648 (648.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@5666da74e57e:/# exit
exit
root@ubuntu:~# docker run -it --rm --net=container:5666 45d bash
root@5666da74e57e:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:c0:a8:2a:06
inet addr:192.168.42.6 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::42:c0ff:fea8:2a06/64 Scope:Link
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:648 (648.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@5666da74e57e:/#
none 模式,使用—net=none 指定这种模式,不会配置任何网络
root@ubuntu:~# docker run -it --rm --net=none 45d bash
root@648b3ffc6bc0:/# ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
bridge 模式,使用 —net=bridge 指定默认模式,不用指定默认就是这种模式。这种模式会为每个容器分配一个独立的 Network Namespace。类似于 vmware 的 nat 网络模式。同一个宿主机上的所有容器会在同一个网段下,相互之间是可以通信的。