Docker: Connecting to the MySQL container from the Tomcat container

Source: Internet
Author: User
Tags docker ps docker run

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 names              5a02ce457c87 tutum/tomcat:8.0  "/run.sh" 16 minutes ago up 16 minutes 0. 0.0.0:80->8080/tcp Insane_ HAWKINGFB4B9AB6A2BA dl.dockerpool.com:5000/mysql:5.7  "/entrypoint.sh mysq hours ago up + hours 0.0.0.0:3306->3306/tcp db001    /span>          

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-E Mysql_root_password=admin-D MySQL: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-p80:8080--name tomcat001--link db001:tomysql tutum/tomcat:8.0 #解释下--link in front of db001 is the container name of the database server that we want to connect to, and the following tomysql Is the name of this link that we want to create e3c136d76b447e885006a43c63f5200c2012a7ce02aaa43860c7a00c130a563e[email protected] ~/base $ Docker Ps #这里我们可以看到只有tomcat的端口映射到了宿主主机上, while MySQL port only tomcat container can connect container ID IMAGE COMMAND CREATED STATUS PORTS namese3c136d76b44 tutum/tomcat:8.0  "/run.sh" 3 seconds ago up 2 seconds  0.0.0.0:80->8080/tcp tomcat001fe9e65aaf58c dl.dockerpool.com:5000/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.

Docker: Connecting to the MySQL container from the Tomcat container

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.