CentOS7环境下Docker的网络模式

1 Docker的四种网络模式

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

[root@localhost ~]# docker network ls

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

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 ~]#

自定义网桥:

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

3 Host网络模式

eg:

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

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

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 但是文件系统并不共享,只共享网络

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值