實例解析Docker網絡管理機制(bridge network,overlay network),介紹Docker默認的網絡方式,並建立本身的網絡橋接方式,將開發的容器添加至本身新建的網絡,提升Docker網絡安全和通訊.python
給docker名稱的好處是:
- 容易記
- 能夠經過特殊命令,使得名稱能夠在容器和容器之間使用web
wxl@wxl-pc:~$ docker images
wxl@wxl-pc:~$ docker run -d -P --name wangxiaoleiweb training/webapp python app.py
最後一行能夠看到wangxiaoleiweb已經命名成功docker
可使用docker inspect經過剛剛設置的名稱來查看wangxiaoleiwebexpress
wxl@wxl-pc:~$ docker inspect wangxiaoleiweb
#中止運行中的鏡像
wxl@wxl-pc:~$ docker stop wangxiaoleiweb
#刪除自定義的名稱
docker rm wangxiaoleiweb
web
wxl@wxl-pc:~$ docker rm web
web
wxl@wxl-pc:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 50dae1ee8677 2 weeks ago 196.7 MB
hello-world latest c54a2cc56cbb 4 weeks ago 1.848 kB
training/webapp latest 6fae60ef3446 14 months ago 348.8 MB
training/sinatra latest 49d952a36c58 2 years ago 447 MB
#查看Docker默認的三種網絡
wxl@wxl-pc:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
7f6b9cbd3eec bridge bridge local
0cb106f52e66 host host local
cfb50541161b none null local
爲一個容器橋接網絡json
wxl@wxl-pc:~$ docker run -itd --name=networktest ubuntu:14.04
2a433c9467d3d35474daa544dcac665ecc4b71b202b82d0bd59049f7d52e397f
wxl@wxl-pc:~$ docker network inspect bridge
[
{
"Name": "bridge",
"Id": "7f6b9cbd3eece7301d196363b5250c44fd0b275f3c3d38a952af2ad8b2fd38c2",
"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": {
"2a433c9467d3d35474daa544dcac665ecc4b71b202b82d0bd59049f7d52e397f": {
"Name": "networktest",
"EndpointID": "d0b94ec934b390784faa5ad8946e9ba6031afe4467889e1f7fe0eda5d79deac1",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/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": {}
}
]
能夠看到經過檢查網絡能夠看到容器的IP地址,名稱等信息.ubuntu
wxl@wxl-pc:~$ docker network disconnect bridge networktest
再用docker network inspect bridge
會發現已經沒有networktest信息了centos
容器的網絡(network)是用來隔離容器和容器之間或者網絡與網絡之間的方法,因此,因此咱們經過創建網絡來隔離容器.其中,內置的bridge網絡是沒法刪除的.安全
Docker Engine 支持兩種網絡橋接網絡(bridge network)和覆蓋網絡(overlay network),橋接的網絡侷限於單個主機運行的Docker Engine,而覆蓋網絡能夠包含多個主機.ruby
wxl@wxl-pc:~$ docker network create -d bridge my-bridge-network
3bfde1ba62a75e38236517f4f0731f48c281cec107e976039773be134968c453
其中,-d
指使用Dockerbridge
(橋接)方式的網絡,若是沒有加默認的也是橋接網絡.
經過docker network ls
來查看已經建立好的網絡bash
#這裏建立了一個db的容器(若本地沒有該鏡像會自動拉取)
wxl@wxl-pc:~$ docker run -d --network=my-bridge-network --name db training/postgres
經過inspect命令查看已經添加至網絡的db容器
wxl@wxl-pc:~$ docker inspect --format='{{json .NetworkSettings.Networks}}' db
{"my-bridge-network":{"IPAMConfig":null,"Links":null,"Aliases":["237e0f7060b6"],"NetworkID":"3bfde1ba62a75e38236517f4f0731f48c281cec107e976039773be134968c453","EndpointID":"33516faca38c0f76bd2c6ebb6e8c2574c62e242d5a0c10b77c49e14b2549f3e5","Gateway":"172.18.0.1","IPAddress":"172.18.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:12:00:02"}}
wxl@wxl-pc:~$ docker run -d --name web training/webapp python app.py
8f12cfaaedda2b348bef437fa2eeed19ef69ad07cd98aa72e0c2113710413e6f
wxl@wxl-pc:~$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web 172.17.0.2
#進入db容器
wxl@wxl-pc:~$ docker exec -it db bash
#首次嘗試ping web容器
root@237e0f7060b6:/# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
^C
--- 172.17.0.2 ping statistics ---
24 packets transmitted, 0 received, 100% packet loss, time 23183ms
root@237e0f7060b6:/# exit
wxl@wxl-pc:~$ docker network connect my-bridge-network web
#再次進入db容器
wxl@wxl-pc:~$ docker exec -it db bash
#ping web容器,發現已經能夠ping通
root@237e0f7060b6:/# ping web
PING web (172.18.0.3) 56(84) bytes of data.
64 bytes from web.my-bridge-network (172.18.0.3): icmp_seq=1 ttl=64 time=0.097 ms
64 bytes from web.my-bridge-network (172.18.0.3): icmp_seq=2 ttl=64 time=0.110 ms
64 bytes from web.my-bridge-network (172.18.0.3): icmp_seq=3 ttl=64 time=0.109 ms
^C
--- web ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.097/0.105/0.110/0.010 ms
可見,不一樣的容器在不一樣的網絡是能夠起到隔離做用的,因此,自定義一個網絡能夠保證容器和容器之間通訊安全.