1、虚拟桥接式网络:
隔离桥:只能在同一桥上的各主机之间通信,且地址需要在同一网段
仅主机桥:这个桥上的虚拟机可以直接与桥地址所代表的宿主机地址之间通信
路由桥:如果我们打开宿主机的核心转发功能并且让仅主机桥上的每一个虚拟机网关指向仅主机桥的地址,就构建成路由桥;报文能够被宿主机发出去但是报文回不来,因为通常情况下外部的主机并不知道隐藏在宿主机背后的虚拟机的网络到底如何可达
NAT桥:你可以把她想象成是某个虚拟机就是通过交换机连接起来的位于我们的网关主机背后的一个单独的主机,这个虚拟机指向了我们网关主机的内网接口,而网关主机又开启了SNAT功能,因此它与我们网关主机其它接口所能够到达的网络中的主机可直接进行通信,因为外部的主机收到以后看到的源地址是SNAT主机的对外通信的网络接口,因此可以让外部主机收到报文以后正常回应,回应通过SNAT主机的连接追踪功能还要把报文返回给内网中的虚拟机请求者。
注意:我们虚拟机可以访问或者不能访问哪个网络取决于桥的构建方式
2、docker网络实现方式:
四种:
桥网络:docker0 NAT桥,这种桥网络可以自行定义
共享桥:联盟式网络 一个容器可以共享另外一个容器得网络空间
Host网络:容器直接使用宿主机网络,共享宿主机得网络名称空间也就意味着看到得直接是宿主机得网卡也是宿主机得网络协议栈
None网络:没有网络
3、四种网络运行:
a、None网络(这种模式没有网卡,只能自己与自己通信)
[root@bogon ~]# docker run --name tinyweb2 -it --rm --network none 572232220/myimg:v0.1 /bin/sh
/ # ifconfig -a //查看是否有虚拟网卡
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:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
b、桥网络(默认是bridge桥,docker0桥有什么功能这个容器就有什么功能)
先启一个tinyweb2容器:
[root@bogon ~]# docker run --name tinyweb2 -it --rm --network bridge 572232220/myimg:v0.1 /bin/sh
/ # ifconfig //查看IP地址和宿主机是同一个网段的
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 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:656 (656.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:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
c、共享桥:联盟式网络
共享容器间的网络:
再起一个容器tinyweb3去共享tinyweb2的网络:
[root@bogon ~]# docker run --name tinyweb3 -it --rm --network container:tinyweb2 572232220/myimg:v0.1 /bin/sh
/ # idconfig
/bin/sh: idconfig: not found
/ # ifconfig //查看IP地址与tinyweb2地址是一样的则代表网络共享成功
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 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:656 (656.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:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
在容器tinyweb2中启动httpd查看监听的端口
/ # httpd -h /data/web/html
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN
/ # hostname
fefa72d32e6f
/ #
在容器tinyweb3中去访问127.0.0.1
/ # curl 127.0.0.1
/bin/sh: curl: not found
/ # wget -O - -q 127.0.0.1 //访问
<h1>Test Page @Busybox Web Server</h1>
注意:容器之间共享网络只是网络空间共用,其它的名称空间是被隔离的
例如:在容器tinyweb2中cp一个文件到/tmp下面
/ # cp /etc/hosts /tmp
/ # ll /tmp
/bin/sh: ll: not found
/ # ls /tmp
hosts
/ #
在容器tinyweb3中查看是看不到的,因为文件系统是隔离的
/ # ls /tmp
/ #
d、Host网络
[root@bogon ~]# docker run --name tinyweb2 -it --rm --network host 572232220/myimg:v0.1 /bin/sh
/ # ifconfig
docker0 Link encap:Ethernet HWaddr 02:42:86:AB:93:77
inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0
inet6 addr: fe80::42:86ff:feab:9377/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:446 (446.0 B)
ens33 Link encap:Ethernet HWaddr 00:0C:29:AC:94:49
inet addr:192.168.19.129 Bcast:192.168.19.255 Mask:255.255.255.0
inet6 addr: fe80::c40f:e734:5d21:73f5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:169341 errors:0 dropped:0 overruns:0 frame:0
TX packets:31469 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:238037803 (227.0 MiB) TX bytes:4102528 (3.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:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1044 (1.0 KiB) TX bytes:1044 (1.0 KiB)
veth5678dc3 Link encap:Ethernet HWaddr 26:38:D9:0F:0C:CB
inet6 addr: fe80::2438:d9ff:fe0f:ccb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:13 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:1102 (1.0 KiB)
/ # hostname
bogon
我在容器tinyweb2内监听了某一个端口以后
/ # httpd -h /data/web/html
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
/ #
我们可以用最外部的宿主机去访问容器的宿主地址的web
4、和网络相关的其它选项
a、主机名:--hostname
[root@bogon ~]# docker run --name bbox2 -it --rm --hostname yumumu busybox //在外部直接注入一个主机名给容器使用
/ # hostname
yumumu
b、从外部注入生成hosts文件 --add-host
[root@bogon ~]# docker run --name bbox2 -it --rm --hostname yumumu --add-host www.baidu.com:8.8.8.8 busybox
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
8.8.8.8 www.baidu.com
172.17.0.3 yumumu
注意--add-host文件可以使用多次,可以多加几次
[root@bogon ~]# docker run --name bbox2 -it --rm --hostname yumumu --add-host www.baidu.com:8.8.8.8 --add-host yulin.com:2.2.2.2 --add-host yulin2.com:3.3.3.3 busybox
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
8.8.8.8 www.baidu.com
2.2.2.2 yulin.com
3.3.3.3 yulin2.com
172.17.0.3 yumumu
c、直接注入我们自定义的DNS服务器地址和搜索域
--dns 指定dns
--dns-search 指定搜索域
[root@bogon ~]# docker run --name bbox2 -it --rm --hostname yumumu --add-host www.baidu.com:8.8.8.8 --dns 1.1.1.1 --dns 114.114.114.114 --dns-search ilinux.io busybox
/ # cat /etc/resolv.conf
search ilinux.io
nameserver 1.1.1.1
nameserver 114.114.114.114
d、-p选项 端口映射
要想让私有主机上的网络服务在外呼被访问除了直接共享宿主机的网络之外还可以做映射,我们可以指定宿主机上的某个IP地址上的某个端口与容器上的某个端口建立映射关系,访问宿主的某个端口映射到容器内的指定端口,这个操作我们通常称为服务暴露
-p选项的使用格式:
-p <containerPort> 将指定容器端口映射至主机所有地址的一个动态端口
-p <hostPort>:<containerPort> 将容器端口映射至<hostPort>主机端口
-p <ip>::<containerPort> 将指定容器端口<containerPort>映射至主机指定<ip>的动态端口
-p <ip>:<hostPort>:<containerPort> 将指定容器端口<containerPort>映射至主机指定<ip>的端口<hostPort>
“动态端口”指随机端口,具体的映射结果可以使用docker port命令来查看
a、将指定容器端口映射至主机所有地址的一个动态端口
[root@bogon ~]# docker run -itd -p 80 --name tinyweb3 --name tinyweb3 572232220/myimg:v0.1
45944fab1c8f79a51840a8a03c0ed1b57fd9f66e7b3974b39b9b3293e54aeb0c
[root@bogon ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45944fab1c8f 572232220/myimg:v0.1 "/bin/sh" 5 seconds ago Up 4 seconds 0.0.0.0:32771->80/tcp tinyweb3
root@bogon ~]# iptables -t nat -vnL //查看端口映射信息
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:32771 to:172.17.0.2:80
用电脑去访问宿主机的32771端口就可到达容器
b、将容器端口映射至<hostPort>主机端口
[root@bogon ~]# docker run -itd -p 80:80 --name tinyweb3 --name tinyweb3 572232220/myimg:v0.1
fa4b69d994f787adece9816c00fa5b25b2ea325bbdcb26926525c03c0f14b3da
[root@bogon ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
71 3692 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 to:172.17.0.2:80
外界访问宿主机的80端口就可到达容器
c、将指定容器端口<containerPort>映射至主机指定<ip>的端口<hostPort>
[root@bogon ~]# docker run -itd -p 192.168.19.129:80:80 --name tinyweb3 --name tinyweb3 572232220/myimg:v0.1
efa86699f75a5ee303ea8f698eda77e568a5510d1baadd8d24bbb454cdc74c81
[root@bogon ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
77 4004 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 192.168.19.129 tcp dpt:80 to:172.17.0.2:80
d、将指定容器端口<containerPort>映射至主机指定<ip>的动态端口
[root@bogon ~]# docker run -itd -p 192.168.19.129::80 --name tinyweb3 --name tinyweb3 572232220/myimg:v0.1
113e199a9b31bd06225a8d00a44f428f8fa127e3c884fa6a044fdc0179516494
[root@bogon ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
77 4004 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 192.168.19.129 tcp dpt:32768 to:172.17.0.2:80
e、假如一个容器内的程序监听了多个端口呢,比如web服务同时监听80和443端口
-p选项可用多次
[root@bogon ~]# docker run -itd -p 80 -p 443 --name tinyweb3 --name tinyweb3 572232220/myimg:v0.1
5193eaccb4da5fb9493597c9b9dc511e6048cc900b29a94bbf8fb589ec8c9750
[root@bogon ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
77 4004 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:443
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:32772 to:172.17.0.2:443
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:32773 to:172.17.0.2:80
查看容器映射关系的命令
[root@bogon ~]# docker container port tinyweb3
443/tcp -> 0.0.0.0:32772
80/tcp -> 0.0.0.0:32773
创建网络的命令
#docker network create
[root@bogon ~]# docker network create --subnet 10.10.0.0/24 mybr0 //创建网络mybr0
69fff7eb1891554b8f89a95f0555fe54c8648138143d6a31edfa8420f0502034
[root@bogon ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
c0abba736037 bridge bridge local
bd05669898de host host local
69fff7eb1891 mybr0 bridge local
184f753b11f7 none null local
用所创建的网络去运行容器
[root@bogon ~]# docker run --name tinyweb3 --rm --network mybr0 -p 80 -it 572232220/myimg:v0.1 /bin/sh
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:0A:0A:00:02
inet addr:10.10.0.2 Bcast:10.10.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 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:656 (656.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:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)