docker 网络模式

in docker with 0 comment

1.第一种网络模式host
host模式: 使用--net=host指定docker使用的网络实际上和宿主机一样,在容器内看到的网卡ip是宿主机上的ip.

bash-3.2# docker run -it --rm --name network_host --net=host new_centos:01 bash       
### --rm: 退出后删除该容器
### 宿主机执行ifconfig 与 容器执行ifconfig后进行ip对比,得到容器里的ip信息和宿主机的ip信息一样
bash-3.2 /# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.40  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::be5f:f4ff:fe5e:4aad  prefixlen 64  scopeid 0x20<link>
        ether bc:5f:f4:5e:4a:ad  txqueuelen 0  (Ethernet)
        RX packets 274360905  bytes 286715259393 (267.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 136254081  bytes 11227340887 (10.4 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
…………………………………………………………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………………………………………………………
vethe82752b: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::d496:d3ff:feb0:3c93  prefixlen 64  scopeid 0x20<link>
        ether d6:96:d3:b0:3c:93  txqueuelen 0  (Ethernet)
        RX packets 7  bytes 558 (558.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 117  bytes 9042 (8.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2.第二种网络模式container
container模式: 使用--net=container:container_id/container_name多个容器使用共同的网络,看到的ip是一样的.

bash-3.2# docker exec -it 1e4cf0c7b5dc bash     #进入任意一个容器
1e4cf0c7b5dc# yum -y install net-tools      #安装ifconfig命令
1e4cf0c7b5dc#  ifconfig |grep -A1 "eth0"
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.19  netmask 255.255.0.0  broadcast 0.0.0.0
bash-3.2# docker run -it --rm --name network_container --net=container:dced5597366d new_centos:01 bash
dced5597366d# ifconfig |grep -A1 "eth0"
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.19  netmask 255.255.0.0  broadcast 0.0.0.0
### 可以看到,两个容器id、ip都一样.

3.第三种网络模式none
none模式: 使用--net=none, 这种模式下,不会配置任何网络

bash-3.2# docker run -it --rm --name network_none --net=none new_centos:01 bash 
c3af5c1d7616# ifconfig |grep -E 'eth0|lo'
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        loop  txqueuelen 0  (Local Loopback)
c3af5c1d7616# ping baidu.com
ping: unknown host baidu.com
### 该模式创建容器后是没有网络的  

4.第四种网络模式bridge

bridge模式: 使用--net=bridge.创建完容器默认为这种网络模式.类似与vmware的nat网络模式.
Responses