1.使用docker創(chuàng)建兩個tomcat容器并測試網(wǎng)絡(luò)連接
安裝鏡像和ping工具
# 1. 拉取鏡像
docker pull tomcat
# 2.使用鏡像創(chuàng)建兩個容器tomcat01,tomcat02
docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat02 tomcat
#3.分別進入兩個容器安裝ping相關(guān)工具
apt-get update
apt install net-tools # ifconfig
apt install iputils-ping # ping
查看網(wǎng)絡(luò)配置
tomcat01的網(wǎng)絡(luò):
tomcat01網(wǎng)絡(luò).png
tomcat02的網(wǎng)絡(luò):
tomcat02網(wǎng)絡(luò).png
查看是否可以ping通(成功)
image.png
以ping容器名的方式查看(失旊缜浮)
image.png
分析
eth-pair 就是一對的虛擬設(shè)備接口翘盖,他們都是成對出現(xiàn)的最仑,一端連著協(xié)議炊甲,一端彼此相連卿啡。而由于網(wǎng)絡(luò)的ip可能發(fā)生變化颈娜,為保證高可用浙宜,我們需要以服務(wù)名的形式進行配置粟瞬,因此需要自定義網(wǎng)絡(luò)。
20200814101617604.png
2.自定義網(wǎng)絡(luò)實現(xiàn)以服務(wù)方式連接
一些命令
docker network ls #查看所有docker網(wǎng)絡(luò)
docker inspect 2983wefoj298 #后面的是network id俗批,查看網(wǎng)絡(luò)詳情
#創(chuàng)建網(wǎng)絡(luò)步驟
#1.創(chuàng)建以橋接模式的網(wǎng)絡(luò)
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
#2.使用該網(wǎng)絡(luò)創(chuàng)建兩個容器
docker run -d -P --name tomcat-net-01 --net mynet tomcat
docker run -d -P --name tomcat-net-02 --net mynet tomcat
#默認不寫網(wǎng)絡(luò)是
docker run -d -P --name tomcat-net-01 --net bridge tomcat
#3.進入兩個容器安裝ping
apt-get update
apt install net-tools # ifconfig
apt install iputils-ping # ping
查看tomcat-net-01的網(wǎng)絡(luò)配置
查看tomcat-net-02的網(wǎng)絡(luò)配置
使用tomcat-net-02來pingtomcat-net-01
image.png
查看網(wǎng)絡(luò)配置
image.png
再來測試用tomcat01連接tomcat-net-01(失斍场)
image.png
3.網(wǎng)絡(luò)連通
# 打通網(wǎng)絡(luò)和一個容器,即將指定容器放到該網(wǎng)絡(luò)
docker network connect mynet tomcat01
則可以實現(xiàn)tomcat01和mynet中容器的通信
image.png
再來查看網(wǎng)絡(luò)配置詳情
image.png