docker四种网络模式

docker四种网络模式

docker网络模式

  • 网络模式

--network=bridge

默认网络,Docker启动创建一个docker0网桥,默认创建的容器也是添加到这个网桥中。

  • host

--network=host

容器不会获得一个独立network namesace,而是与宿主机共用一个。这就意味着容器不会有自己的网卡信息,而是使用宿主机的。容器除了网络,其他都是隔离的。

  • none

--network=none

获取独立的network namesace,但不为容器进行任何网络配置,需要我们手动配置。

container

--net=container

与指定的容器使用同一个network namespace,具有同样的网络配置信息,两个容器除了网络,其他都是还是隔离的。

自定义网络

与默认网络的bridge原理一样,但自定义网络具备内部DNS发现,可用通过容器名或者主机容器之间网络通信。

二、docker网络图

docker四种网络模式

三、网络实战

  •    host 实战

[root@docker ~]# docker run -itd --name web06 --network host busybox sh
5729c3fd5adf723b3a12d4cc79a9a5f5f767e506476644ecb24741a7b1d44bb9
[root@docker ~]# docker exec -it web06 sh
/ # ifconfig 
docker0   Link encap:Ethernet  HWaddr 02:42:CC:E5:27:97  
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:ccff:fee5:2797/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8103 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8725 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:343529 (335.4 KiB)  TX bytes:35221929 (33.5 MiB)

ens33     Link encap:Ethernet  HWaddr 00:0C:29:60:B7:A3  
          inet addr:192.168.254.30  Bcast:192.168.254.255  Mask:255.255.255.0
          inet6 addr: fe80::d371:191b:e3b1:d693/64 Scope:Link
          inet6 addr: fd15:4ba5:5a2b:1008:2c0:2dee:549f:18b9/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:254105 errors:0 dropped:0 overruns:0 frame:0
          TX packets:92977 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:263872914 (251.6 MiB)  TX bytes:8320088 (7.9 MiB)

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:20 errors:0 dropped:0 overruns:0 frame:0
          TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:1313 (1.2 KiB)  TX bytes:1313 (1.2 KiB)

veth0f72b2f Link encap:Ethernet  HWaddr A6:7A:9D:B0:72:58  
          inet6 addr: fe80::a47a:9dff:feb0:7258/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  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)

vethb72ec01 Link encap:Ethernet  HWaddr AA:EE:B5:C8:B8:C6  
          inet6 addr: fe80::a8ee:b5ff:fec8:b8c6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  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)

/ # 
  • none无网络模式实战

[root@docker ~]# docker exec -it web07 sh

/ # ifconfig 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # 

 

  • 自定义网络模式模式实战

[root@docker ~]# docker network create  lnmp 
504fea04c7c4fcb47a7605c660930df02f84ffed698e8cede7e42b61ecbd6c06
[root@docker ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
3046c002d243        bridge              bridge              local
097ce6ec1721        host                host                local
504fea04c7c4        lnmp                bridge              local
a8fdcc0ef242        none                null                local

 

说明:

自定义网络类似同一个局域网,共享6种名称空间

UTS    主机和域名

IPC     信号量、消息队列和共享内容

PID     进程编号

network   网络设备、网络栈、端口等

mount    挂载点(文件系统)

user     用户和用户组

实验1:

/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:14:00:02  
          inet addr:172.20.0.2  Bcast:172.20.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:16 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:1296 (1.2 KiB)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # hostname 
2a4ed0a8b139
/ # ping 172.20.0.3
PING 172.20.0.3 (172.20.0.3): 56 data bytes
64 bytes from 172.20.0.3: seq=0 ttl=64 time=0.069 ms
64 bytes from 172.20.0.3: seq=1 ttl=64 time=0.079 ms
64 bytes from 172.20.0.3: seq=2 ttl=64 time=0.061 ms

 

 实验2

 [root@docker ~]# docker exec -it web09 sh
/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:14:00:03  
          inet addr:172.20.0.3  Bcast:172.20.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9 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:718 (718.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # ping 172.20.0.2
PING 172.20.0.2 (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.303 ms
64 bytes from 172.20.0.2: seq=1 ttl=64 time=0.107 ms
64 bytes from 172.20.0.2: seq=2 ttl=64 time=0.055 ms

 

container

实战:

实例1、

[root@docker ~]# docker run -itd --name web10 --network container:web08 busybox sh
5f7c868285768f41287d0a6a8150009dae66202c5b730e6adafde70597376dd5
[root@docker ~]# docker exec -it web10 sh
/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:14:00:02  
          inet addr:172.20.0.2  Bcast:172.20.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:309 errors:0 dropped:0 overruns:0 frame:0
          TX packets:293 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:28946 (28.2 KiB)  TX bytes:27650 (27.0 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

 

实例2、

[root@docker ~]# docker exec -it web08 sh
/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:14:00:02  
          inet addr:172.20.0.2  Bcast:172.20.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:16 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:1296 (1.2 KiB)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

 

 

 

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
百度已收录
Linux

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: