docker引擎十分強大,本身包含了對容器的網絡驅動的支援。預設docker會提供橋接(bridge)和覆寫網絡(overlay ).
- 橋接:實體網卡和虛拟網卡通過虛拟網絡内的虛拟交換進行橋接,對外通訊。

- 覆寫網絡:暫時隻從官方看到是使用vxlan技術的網絡,swarm使用這種加密網絡會更安全。
1.正常情況下如果你不實用swarm建立覆寫性網絡需要安裝kvstore服務支援選型,例如consul,etcd和zookeeper
2.一個叢集主機連接配接到kvstore
3.在每個swarm上配置叢集引擎的daemon
注意:使用覆寫性網絡時如果子網重複或覆寫可能會導緻容器無法使用網絡
每個安裝docker的使用者本地都會生成三個網絡如下:
[root@salt-node1
nginx-new]# docker network ls
NETWORK ID NAME DRIVER SCOPE
b60c9e065473 bridge bridge local
a603808ad4ba host host local
48d3687c03f0 none null local
橋接網卡你docker預設網卡,除非你指定使用别的網絡方式。
[root@salt-node1
nginx-new]# docker run -itd --name=networktest training/webapp
f959f1626b03d965692d0d45f5307c062facac69eff2a33779a50293c35f662e
檢視橋接網絡内的全部資訊
網段,網關,容器IP
[root@salt-node1
nginx-new]# docker network inspect
bridge
[
{
"Name": "bridge",
"Id":
"b60c9e065473e9d0f8b5eaffc520b681d812e3edd4105cdeba39b5e09bb81ba0",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver":
"default",
"Options": null,
"Config": [
{
"Subnet":
"172.17.0.0/16",
"Gateway":
"172.17.0.1"
}
]
},
"Internal": false,
"Containers":
{
"846953219c6d32025f2ec9b95ea57d50c2f6cc04fbf92047b8a0e5789d623026":
{
"Name":
"zen_varahamihira",
"EndpointID":
"d2f6b8fdfa73fc369c5c77465f79f9d7ada17d9d612b5397a3da227a5e133c1b",
"MacAddress":
"02:42:ac:11:00:02",
"IPv4Address":
"172.17.0.2/16",
"IPv6Address":
""
},
"f959f1626b03d965692d0d45f5307c062facac69eff2a33779a50293c35f662e":
{
"Name":
"networktest",
"EndpointID":
"3017afc38daac830d872606ffafe5254a408e30e2b10a5c65b0977ba60018c38",
"MacAddress":
"02:42:ac:11:00:03",
"IPv4Address":
"172.17.0.3/16",
"IPv6Address":
""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade":
"true",
"com.docker.network.bridge.host_binding_ipv4":
"0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
[root@salt-node1
nginx-new]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f959f1626b03 training/webapp "python app.py" 4 minutes ago Up 4 minutes 5000/tcp networktest
846953219c6d training/webapp "python app.py" 25 hours ago Up 25 hours 0.0.0.0:32768->5000/tcp zen_varahamihira
從橋接網絡移除指定容器
[root@salt-node1
nginx-new]# docker network disconnect bridge networktest
建立一個自己的橋接網絡
docker引擎天生再帶橋接網絡和覆寫性網絡,docker橋接網絡僅限于單機運作,如果出現多機叢集就有問題了。這時候覆寫型網絡更能滿足你的需求,它可以包括多個主機,是一種進階的主題。
docker
network create -d [network type] [network name]
[root@salt-node1
nginx-new]# docker network create -d bridge nginxs-bridge-network
b67220ae9284c802cd48dca1239026b7539c58b97ef19b19ae8b5d7c7ce13d62
[root@salt-node1
nginx-new]# docker network ls
NETWORK ID NAME DRIVER SCOPE
b60c9e065473 bridge bridge local
a603808ad4ba host host local
b67220ae9284 nginxs-bridge-network bridge local
48d3687c03f0 none null local
檢視新的網絡資訊
[root@salt-node1
nginx-new]# docker network inspect nginxs-bridge-network
[
{
"Name":
"nginxs-bridge-network",
"Id":
"b67220ae9284c802cd48dca1239026b7539c58b97ef19b19ae8b5d7c7ce13d62",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver":
"default",
"Options": {},
"Config": [
{
"Subnet":
"172.18.0.0/16",
"Gateway":
"172.18.0.1/16"
}
]
},
"Internal": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
添加一個容器到指定網絡
建立一個網絡使得你的web應用在不通的網絡下進行隔離這樣才是安全的。當你第一次運作一個容器的時候你可以把它添加到一個新的網絡。預設情況下兩個橋接網絡的容器是完全不能互相通訊的,要想兩個網絡下的指定容器可以互相通訊會做介紹。
參數:--net=[network
name]
[root@salt-node1
nginx-new]# docker run -d --net=nginxs-bridge-network --name db
training/postgres
Unable to find p_w_picpath
'training/postgres:latest' locally
latest: Pulling from
training/postgres
a3ed95caeb02: Pull
complete
6e71c809542e: Pull
complete
2978d9af87ba: Pull
complete
e1bca35b062f: Pull
complete
500b6decf741: Pull
complete
74b14ef2151f: Pull
complete
7afd5ed3826e: Pull
complete
3c69bb244f5e: Pull
complete
d86f9ec5aedf: Pull
complete
010fabf20157: Pull
complete
Digest:
sha256:a945dc6dcfbc8d009c3d972931608344b76c2870ce796da00a827bd50791907e
Status: Downloaded
newer p_w_picpath for training/postgres:latest
4b0bc86f18596e6c24a505a40c759e09c1fd7520a487bf2f278348c641c5240f
檢視指定容器的網絡配置
[root@salt-node1
nginx-new]# docker inspect --format='{{json .NetworkSettings.Networks}}' db
{"nginxs-bridge-network":{"IPAMConfig":null,"Links":null,"Aliases":["4b0bc86f1859"],"NetworkID":"b67220ae9284c802cd48dca1239026b7539c58b97ef19b19ae8b5d7c7ce13d62","EndpointID":"99b9f2f973335447640639e146614ab6f4857b0d1e30f5ed6f9b507f645e137a","Gateway":"172.18.0.1","IPAddress":"172.18.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:12:00:02"}}
[root@salt-node1 ~]#
docker inspect --format='{{range
.NetworkSettings.Networks}}``.`IPAddress``end`' db
172.18.0.2
指定容器可以連接配接到指定網絡
docker network connect
[network name] [CONTAINER NAME]
[root@salt-node1 ~]#
docker network connect nginxs-bridge-network db2
[root@salt-node1
nginx-new]# docker exec -it db2 bash
root@cf9b593a29bc:/#
ping 172.18.0.2
PING 172.18.0.2
(172.18.0.2) 56(84) bytes of data.
64 bytes from
172.18.0.2: icmp_seq=74 ttl=64 time=0.130 ms
64 bytes from
172.18.0.2: icmp_seq=75 ttl=64 time=0.116 ms
64 bytes from
172.18.0.2: icmp_seq=76 ttl=64 time=0.119 ms