Search code examples
mysqldockertomcat8docker-swarm

having issues while connecting tomcat to mysql in a docker container?


I have 2 docker services created via docker-swarm.

One is Tomcat and the other is mysql, i am trying to connect from tomcat to mysql both are in different containers.

here is my hibernate.cfg.xml in tomcat container.

<property name="connection.url">
            jdbc:mysql://localhost:3306/travelbiz
        </property>
        <property name="connection.username">trvlbz_admin</property>
        <property name="connection.password">admin123</property>

and here is the service running

root@ubuntu:/home/smadmin# docker ps
CONTAINER ID        IMAGE                                                                                                 COMMAND                  CREATED             STATUS              PORTS               NAMES
2ecb386d9bbc        cloudesire/tomcat@sha256:19e24a2290e4c3253a44d65243a54c520e9f8e0fe0edf3a5221ccc67cf9da0f8             "/run.sh"                7 hours ago         Up 7 hours          8080/tcp            apache.2.1ftla1ohbnjgax7ibk8fbh5hz
4e940f3be18d        vishnuranganathan/vishmysql@sha256:4460004d01c9a23d4c80ea2a9d7a69fc1495cbe01b48555cf28bd73a535176d1   "/usr/bin/mysqld_safe"   7 hours ago         Up 7 hours          3306/tcp            mysql.1.hjoba4bsrl8lujfumc6qru3uq

i am getting mysql connection refused exception when trying to access via tomcat.can someone help me on this ?


Solution

  • You do not want to use the ip address of the mysql container as that is bound to change the next time a new mysql container is started or if you happen to scale it (may not be easy with mysql though). What you should ideally use is the name of the container. When you brought up the container you would have used docker run, make sure to add --name mysql and then use mysql instead of the ip address.