The container interconnect in Docker is a more complex topic, and the details are described in the following sections. To continue with the first 2 chapters, we created a MySQL container and a tomcat container that can use "docker ps" to see their status.
[Email protected] ~/base $ docker pscontainer ID IMAGE COMMAND CREATED STATUS PORTS NAMES5a02ce457c87 Tutum/tomcat:8.0 "/run.sh" -Minutes ago Up -Minutes0.0.0.0: the-8080/tcp insane_hawkingfb4b9ab6a2ba dl.dockerpool.com: the/mysql:5.7 "/entrypoint.sh mysq hours ago Up hours 0.0.0.0:3306->3306/tcp db001
We see here that we directly mapped the MySQL 3306 port directly to the host host, Tomcat's 8080 port mapped to the host host's 80 ports above, they can now actually directly use the host host's address interconnection. In the development environment, this is not a problem, if in the production environment, there may be a certain security risks, the use of container interconnection can avoid this problem, let's do the following:
[Email protected] ~/base $ docker Stop db001#使用容器的名字来停止容器Db001[email protected] ~/base $ docker RM db001#使用容器的名字来删除容器Db001[email protected] ~/base $ docker run--name db001- eMysql_root_password=admin- DMysql:5.7Fe9e65aaf58CD34c12f3c1ab4a3318ae3920300b0f3d0241359c904d7182376f[email protected] ~/base $ docker stop5A05A0#使用容器的id来停止容器, the effect is the same as using the name, and generally only need to enter the container ID of the first 3 characters can be[Email protected] ~/base $ docker RM5A05A0[email protected] ~/base $ docker Run- D-P the:8080--name tomcat001--link db001:tomysql tutum/tomcat:8.0#解释下--link Front db001 is the container name of the database server we want to connect to, and Tomysql is the name of the link we're going to create.E3c136d76b447e885006a43c63f5200c2012a7ce02aaa43860c7a00c130a563e[email protected] ~/base $ docker PS#这里我们可以看到只有tomcat的端口映射到了宿主主机上 While the MySQL port is only available for the Tomcat container to connectCONTAINER ID IMAGE COMMAND CREATED STATUS PORT S namese3c136d76b44 Tutum/tomcat:8.0 "/run.sh" 3Seconds ago Up2Seconds0.0.0.0: the-8080/tcp tomcat001fe9e65aaf58c dl.dockerpool.com: the/mysql:5.7 "/entrypoint.sh mysq 5 minutes ago up 5 minutes 3306/tcp Db001,tomcat001/tomysql
At this point, the connection between MySQL and Tomcat is complete.
For more information, please follow http://www.dockerpool.com
Docker: Connecting to the MySQL container from the Tomcat container