前面我们的实战都是采用单容器操作,并没有涉及到多个容器之间的通讯。可是我们使用容器并不一定把所有的软件都安装到同一个容器中,那样的话,不仅容器的体积增大,而且内部软件复杂,不是同一个类型的软件放在同一个容器中不好管理。今天我们来学习一下如果采用多个容器,多个容器之间应该如何通讯的呢?又是怎么做到的呢?(以tomcat和centos为例)
1.下载tomcat容器,并启动

[root@docker01 /]# docker search tomcat
NAME                          DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
tomcat                        Apache Tomcat is an open source implementati…   2471                [OK]                
tomee                         Apache TomEE is an all-Apache Java EE certif…   66                  [OK]                
dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base…   53                                      [OK]
bitnami/tomcat                Bitnami Tomcat Docker Image                     29                                      [OK]
kubeguide/tomcat-app          Tomcat image for Chapter 1                      26                                      
consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              16                                      [OK]
cloudesire/tomcat             Tomcat server, 6/7/8                            15                                      [OK]
tutum/tomcat                  Base docker image to run a Tomcat applicatio…   11                                      
aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              11                                      [OK]
arm32v7/tomcat                Apache Tomcat is an open source implementati…   6                                       
rightctrl/tomcat              CentOS , Oracle Java, tomcat application ssl…   5                                       [OK]
maluuba/tomcat7-java8         Tomcat7 with java8.                             3                                       
arm64v8/tomcat                Apache Tomcat is an open source implementati…   2                                       
amd64/tomcat                  Apache Tomcat is an open source implementati…   2                                       
ppc64le/tomcat                Apache Tomcat is an open source implementati…   1                                       
camptocamp/tomcat-logback     Docker image for tomcat with logback integra…   1                                       [OK]
99taxis/tomcat7               Tomcat7                                         1                                       [OK]
i386/tomcat                   Apache Tomcat is an open source implementati…   1                                       
s390x/tomcat                  Apache Tomcat is an open source implementati…   0                                       
secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                                       [OK]
picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAGER…   0                                       [OK]
oobsri/tomcat8                Testing CI Jobs with different names.           0                                       
cfje/tomcat-resource          Tomcat Concourse Resource                       0                                       
jelastic/tomcat               An image of the Tomcat Java application serv…   0                                       
appsvc/tomcat                                                                 0                                       
[root@docker01 /]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
a4d8138d0f6b: Pull complete 
dbdc36973392: Pull complete 
f59d6d019dd5: Pull complete 
aaef3e026258: Pull complete 
5e86b04a4500: Pull complete 
1a6643a2873a: Pull complete 
2ad1e30fc17c: Pull complete 
16f4e6ee0ca6: Pull complete 
928f4d662d23: Pull complete 
b8d24294d525: Pull complete 
Digest: sha256:2785fac92d1bcd69d98f2461c6799390555a41fd50d3f847b544368d594c637b
Status: Downloaded newer image for tomcat:latest
[root@docker01 /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos               latest             0cb8570501c2        5 hours ago         425MB
tomcat              latest              238e6d7313e3        8 days ago          506MB
[root@docker01 /]# docker run -d --name tomcat tomcat:latest
8aa574b1d5488eef7ebb00778b08df900255405420c9acd5bfad1ba6e2a3eae9
[root@docker01 /]#

#如果容器之间没有建立连接,想要在其他容器上访问tomcat容器,可以通过ip+port进行访问(tomcat默认端口为8080),为什么不直接采用ip地址访问呢?因为启动每次启动都会被分配一个新的ip,不可能每次都去调整ip,因此只需要访问自己指定的容器名称即可,这样方便了许多:

[root@6c78bc7000c5 html]# curl -I 172.17.0.2:8080
HTTP/1.1 200 
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 26 Jul 2019 08:11:13 GMT

[root@6c78bc7000c5 html]# 

2.启动centos并连接到tomcat容器(--link tomcat表示,连接到已经启动的其他容器)

[root@docker01 /]# docker run -it --link tomcat centos:latest /bin/bash
[root@6c78bc7000c5 html]

3.采用别名进行容器之间的通讯:

[root@6c78bc7000c5 html]# curl -I tomcat:8080
HTTP/1.1 200 
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 26 Jul 2019 08:05:42 GMT

[root@6c78bc7000c5 html]# 

4.为什么--link tomcat,就可以让centos访问tomcat(172.17.0.2)呢?因为加了这条参数后,启动centos启动时centos会自动将tomcat容器ip以及自己的ip解析到自己的/etc/hosts中,因此只需通过容器名称即可访问。

[root@6c78bc7000c5 html]# cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.2	tomcat 8aa574b1d548
172.17.0.3	6c78bc7000c5
[root@6c78bc7000c5 html]# curl -I tomcat:8080
HTTP/1.1 200 
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 26 Jul 2019 08:05:42 GMT

[root@6c78bc7000c5 html]# curl -I 172.17.0.2:8080
HTTP/1.1 200 
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 26 Jul 2019 08:11:13 GMT

[root@6c78bc7000c5 html]# 

但是tomcat并不能访问centos容器,因为--link 是单方向连接。
 

 

最后修改于 2019-07-26 15:42:33
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇