• Docker的网络模式


    1 Docker的四种网络模式

    当你安装docker时,它会自动创建三个网络,可使用如下命令查看:

    [root@localhost ~]# docker network ls
    
    • 1

    host:容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。使用 --net=host指定。
    Container:创建的容器不会创建自己的网卡,配置自己的IP,而是和一个指定的容器共享IP、端口范围。使用 --net=container:NAMEorID 指定。
    None:该模式关闭了容器的网络功能。使用 --net=none 指定。
    Bridge:此模式会为每一个容器分配、设置IP等。使用 --net=bridge 指定,默认设置。

    2 Bridge网络模式

    bridge模式是docker的默认网络模式,不写–net参数,就是bridge模式。使用docker run -p时,docker实际是在iptables做了DNAT规则,实现端口转发功能。可以使用iptables -t nat -vnL查看。

    [root@localhost ~]# docker run -d -P --name nginx1 nginx:1.14-alpine 
    [root@localhost ~]# docker ps 
    [root@localhost ~]# iptables -t nat -vnL
    
    • 1
    • 2
    • 3

    eg:

    [root@localhost ~]# docker run --name b1 --rm -it --network bridge busybox:latest
    / # ip a 
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 
    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 
    inet6 ::1/128 scope host 
    	valid_lft forever preferred_lft forever 
    4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    	link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff 
    	inet 172.17.0.2/16 scope global eth0 
    		valid_lft forever preferred_lft forever 
    	inet6 fe80::42:acff:fe11:2/64 scope link 
    		valid_lft forever preferred_lft forever 
    / # route -n 
    Kernel IP routing table 
    Destination Gateway Genmask Flags Metric Ref Use Iface 
    0.0.0.0 172.17.0.1 0.0.0.0 UG 0 0 0 eth0 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 
    / # ping 192.168.168.139 
    PING 192.168.168.139 (192.168.168.139): 56 data bytes 64 bytes from 192.168.168.139: seq=0 ttl=64 time=0.249 ms 64 bytes from 192.168.168.139: seq=1 ttl=64 time=0.232 ms ^C
    --- 192.168.168.139 ping statistics --- 
    2 packets transmitted, 2 packets received, 0% packet loss 
    round-trip min/avg/max = 0.232/0.240/0.249 ms 
    / # exit 
    [root@localhost ~]#
    
    • 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

    自定义网桥:

    [root@localhost ~]# docker network create -d bridge my-bridge
    [root@localhost ~]# docker run --name b1 --rm -it --network my-bridge busybox:latest
    
    • 1
    • 2

    3 Host网络模式

    eg:

    [root@localhost ~]# docker run --name b1 -it --network host --rm busybox:latest 
    / # ip a
    。。。。
    / # exit 
    [root@localhost ~]#
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4 Container网络模式

    eg:
    1)在一个终端,使用bridge网络模式启动容器b1

    [root@localhost ~]# docker run --name b1 -it --rm busybox:latest 
    / # ip a
    / # echo hello b1 > /tmp/index.html 
    / # httpd -h /tmp 
    / # netstat -lntup
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2)在另一个终端使用Container 网络模式创建容器b2

    [root@localhost ~]# docker run --name b2 -it --network container:b1 --rm busybox:latest 
    / # ip a
    / # wget -O - -q 127.0.0.1 b1启动的httpd服务,在b2上直接访问 
    hello b1 
    / # ls /tmp 但是文件系统并不共享,只共享网络
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5 None网络模式

    [root@localhost ~]# docker run -it --network none --rm busybox:latest / # ip a
    / # route -n 
    Kernel IP routing table 
    Destination Gateway Genmask Flags Metric Ref Use Iface 
    / # exit
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    11. GIT 分支管理
    Java 使用 EMQX 实现物联网 MQTT 通信
    腾讯云大数据ES Serverless
    (一)gitblit安装教程
    Android studio控制台 输出乱码解决方法
    力扣560. 和为 K 的子数组
    OpenCV(三十三):计算轮廓面积与轮廓长度
    三次握手与四次挥的问题,怎么回答?
    不知道照片上怎么文字翻译成英文?来看看这篇文章
    三辊闸机的应用领域和特点
  • 原文地址:https://blog.csdn.net/anran_06/article/details/126852027