咱們都知道docker容器之間是互相隔離的,不能互相訪問,但若是有些依賴關係的服務要怎麼辦呢。下面介紹三種方法解決容器互訪問題。
docker
安裝docker時,docker會默認建立一個內部的橋接網絡docker0,每建立一個容器分配一個虛擬網卡,容器之間能夠根據ip互相訪問。centos
[root@33fcf82ab4dd /]# [root@CentOS ~]# ifconfig ...... docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:35ff:feac:66d8 prefixlen 64 scopeid 0x20<link> ether 02:42:35:ac:66:d8 txqueuelen 0 (Ethernet) RX packets 4018 bytes 266467 (260.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4226 bytes 33935667 (32.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ......
運行一個centos鏡像, 查看ip地址獲得:172.17.0.7網絡
[root@CentOS ~]# docker run -it --name centos-1 docker.io/centos:latest [root@6d214ff8d70a /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.7 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:acff:fe11:7 prefixlen 64 scopeid 0x20<link> ether 02:42:ac:11:00:07 txqueuelen 0 (Ethernet) RX packets 16 bytes 1296 (1.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 648 (648.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
以一樣的命令再起一個容器,查看ip地址獲得:172.17.0.8測試
[root@CentOS ~]# docker run -it --name centos-2 docker.io/centos:latest [root@33fcf82ab4dd /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.8 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:acff:fe11:8 prefixlen 64 scopeid 0x20<link> ether 02:42:ac:11:00:08 txqueuelen 0 (Ethernet) RX packets 8 bytes 648 (648.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 648 (648.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
容器內部ping測試結果以下:spa
[root@33fcf82ab4dd /]# ping 172.17.0.7 PING 172.17.0.7 (172.17.0.7) 56(84) bytes of data. 64 bytes from 172.17.0.7: icmp_seq=1 ttl=64 time=0.205 ms 64 bytes from 172.17.0.7: icmp_seq=2 ttl=64 time=0.119 ms 64 bytes from 172.17.0.7: icmp_seq=3 ttl=64 time=0.118 ms 64 bytes from 172.17.0.7: icmp_seq=4 ttl=64 time=0.101 ms
這種方式必須知道每一個容器的ip,在實際使用中並不實用。code
運行容器的時候加上參數linkblog
運行第一個容器ip
docker run -it --name centos-1 docker.io/centos:latest
運行第二個容器it
[root@CentOS ~]# docker run -it --name centos-2 --link centos-1:centos-1 docker.io/centos:latest
--link:參數中第一個centos-1是容器名,第二個centos-1是定義的容器別名(使用別名訪問容器),爲了方便使用,通常別名默認容器名。io
測試結果以下:
[root@e0841aa13c5b /]# ping centos-1 PING centos-1 (172.17.0.7) 56(84) bytes of data. 64 bytes from centos-1 (172.17.0.7): icmp_seq=1 ttl=64 time=0.210 ms 64 bytes from centos-1 (172.17.0.7): icmp_seq=2 ttl=64 time=0.116 ms 64 bytes from centos-1 (172.17.0.7): icmp_seq=3 ttl=64 time=0.112 ms 64 bytes from centos-1 (172.17.0.7): icmp_seq=4 ttl=64 time=0.114 ms
此方法對容器建立的順序有要求,若是集羣內部多個容器要互訪,使用就不太方便。
1.安裝好docker後,運行以下命令建立bridge網絡:docker network create testnet
查詢到新建立的bridge testnet。
2.運行容器鏈接到testnet網絡。
使用方法:docker run -it --name <容器名> ---network <bridge> --network-alias <網絡別名> <鏡像名>
[root@CentOS ~]# docker run -it --name centos-1 --network testnet --network-alias centos-1 docker.io/centos:latest [root@CentOS ~]# docker run -it --name centos-2 --network testnet --network-alias centos-2 docker.io/centos:latest
3.從一個容器ping另一個容器,測試結果以下:
[root@fafe2622f2af /]# ping centos-1 PING centos-1 (172.20.0.2) 56(84) bytes of data. 64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=1 ttl=64 time=0.158 ms 64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=2 ttl=64 time=0.108 ms 64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=3 ttl=64 time=0.112 ms 64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=4 ttl=64 time=0.113 ms
4.若訪問容器中服務,可使用這用方式訪問 <網絡別名>:<服務端口號>
推薦使用這種方法,自定義網絡,由於使用的是網絡別名,能夠不用顧慮ip是否變更,只要鏈接到docker內部bright網絡便可互訪。bridge也能夠創建多個,隔離在不一樣的網段。