婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Docker容器互訪的三種方法

Docker容器互訪的三種方法

熱門標簽:山東電信外呼系統(tǒng)靠譜嗎 長沙回撥外呼系統(tǒng) 江蘇自動外呼系統(tǒng)一般多少錢 ai電話機器人營銷 云南云電銷機器人招商 400 電話 申請費用 信貸電銷機器人系統(tǒng) 鸚鵡螺號航海地圖標注時間 比較穩(wěn)定的外呼系統(tǒng)

  我們都知道docker容器之間是互相隔離的,不能互相訪問,但如果有些依賴關(guān)系的服務(wù)要怎么辦呢。下面介紹三種方法解決容器互訪問題。

方式一、虛擬ip訪問

安裝docker時,docker會默認創(chuàng)建一個內(nèi)部的橋接網(wǎng)絡(luò)docker0,每創(chuàng)建一個容器分配一個虛擬網(wǎng)卡,容器之間可以根據(jù)ip互相訪問。

[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

容器內(nèi)部ping測試結(jié)果如下:

[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,在實際使用中并不實用。

方式二、link

運行容器的時候加上參數(shù)link

運行第一個容器

docker run -it --name centos-1 docker.io/centos:latest

運行第二個容器

[root@CentOS ~]# docker run -it --name centos-2 --link centos-1:centos-1 docker.io/centos:latest

--link:參數(shù)中第一個centos-1是容器名,第二個centos-1是定義的容器別名(使用別名訪問容器),為了方便使用,一般別名默認容器名。

測試結(jié)果如下:

[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

此方法對容器創(chuàng)建的順序有要求,如果集群內(nèi)部多個容器要互訪,使用就不太方便。

方式三、創(chuàng)建bridge網(wǎng)絡(luò)

1.安裝好docker后,運行如下命令創(chuàng)建bridge網(wǎng)絡(luò):docker network create testnet

查詢到新創(chuàng)建的bridge testnet。

2.運行容器連接到testnet網(wǎng)絡(luò)。

使用方法:docker run -it --name <容器名> ---network <bridge> --network-alias <網(wǎng)絡(luò)別名> <鏡像名>

[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另外一個容器,測試結(jié)果如下:

[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.若訪問容器中服務(wù),可以使用這用方式訪問 <網(wǎng)絡(luò)別名>:<服務(wù)端口號>

  推薦使用這種方法,自定義網(wǎng)絡(luò),因為使用的是網(wǎng)絡(luò)別名,可以不用顧慮ip是否變動,只要連接到docker內(nèi)部bright網(wǎng)絡(luò)即可互訪。bridge也可以建立多個,隔離在不同的網(wǎng)段。

以上就是Docker容器互訪的三種方法的詳細內(nèi)容,更多關(guān)于Docker容器互訪的資料請關(guān)注腳本之家其它相關(guān)文章!

標簽:齊齊哈爾 拉薩 澳門 嘉興 衡陽 烏海 運城 亳州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Docker容器互訪的三種方法》,本文關(guān)鍵詞  Docker,容器,互訪,的,三種,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Docker容器互訪的三種方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于Docker容器互訪的三種方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 平原县| 彰化市| 七台河市| 汉寿县| 永年县| 濉溪县| 吉林省| 玉环县| 武定县| 弥渡县| 苏尼特左旗| 海丰县| 宁海县| 邻水| 肥乡县| 什邡市| 贡嘎县| 遂川县| 锡林浩特市| 宁津县| 田东县| 丰台区| 宣武区| 鄂尔多斯市| 崇左市| 长春市| 积石山| 怀远县| 永城市| 富蕴县| 娄底市| 松阳县| 定陶县| 五常市| 上饶县| 石门县| 余干县| 惠州市| 中方县| 柳河县| 府谷县|