Hello. I am pretty new to docker. I have created a small springboot application that uses mysql for data storage. Both my application and mysql are on the same host machine, and in this case the application runs fine. Now I am trying to create a mysql docker container, and want my application to access the running mysql container instead of using host’s mysql. But I did not get success in this. My host machine is Oracle Ubuntu 18.04 VirtualBox.
Following are the things I am trying to to connect my application to mysql container:
I created a bridge network in docker. named it my-net.
When I start the application, I get error: com.mysql.cj.jdbc.CommunicationException: Communications Link Failure
Java.net.UnknownHostException: mysql-demo
I recommend that you don’t create the containers on the command line, but with docker-compose (see docs for compose file, docs for compose cli). Then you can simply address the MySQL container with its service name and don’t have to worry about managing networks (docker compose will do this for you).
Hi Tekki,
Thank you for your reply. I tried to create the mysql and spring-boot app containers using Docker compose. The mysql container was up without any issue, but the spring boot application(container) gave the same error ‘Communication link failure’. Here is my yml file.
At first sight everything looks okay with this compose file (you don’t define a data volume, but that’s something else). So this is a complicated one.
Just a guess, the reason could be the following: With mysql:latest you install MySQL 8.0 that uses ‘caching sha2 password’ as default authentication method. To connect to it you need a connector that is build with the most recent MySQL and not the MariaDB C libraries and supports this method. Read here about ‘caching_sha2_password as the Preferred Authentication Plugin’. Maybe your connector is not made for MySQL 8.0.
So what you could try is to use mysql:5.7 instead.
Thanks again for your reply. I tried to create the mysql container using ‘mysql:5.7’, but still my application is not connecting. Can this be an issue with my Ubuntu VirtualBox?
Yes, that’s possible, nested virtualization is for adventurers.
On the other hand, your containers seem to be running. You could add an Adminer service to your compose file and then try to connect to your database. This will probably show you more useful messages.
Hello. I’d recently got stuck at this. I try everything regarding network settings with my containers, but eventually I noticed that the problem wasn’t with the containers or docker, but with my spring application. I wasn’t using the properly profile on the Java -jar command. Whithout it, the Default was used, and the app was trying to connect in the localhost.
So basically I got over this using the properly command in the app container, which was something like this:
where supposed to i use this
java -Dspring.profiles.active=dockerembbed,oauth-security -jar myapp.jar command
in application.properties file or some where else? please guide. thanks