Docker Community Forums

Share and learn in the Docker community.

Unable to Connect Mysql container from a spring-boot application

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:

  1. I created a bridge network in docker. named it my-net.
  2. Created mysql container using following command.

docker container run --name mysql-demo --network my-net -e MYSQL_ROOT_PASSWORD=MySQL_Num1 -e MYSQL_DATABASE=demographics -e MYSQL_USER=test -e MYSQL_PASSWORD=test1234 -d mysql:latest

  1. Made sure the container is up and running. Also, the database ‘demographics’ is created in container’s mysql.

  2. In SpringBoot’s used the connection parameters as below, instead of localhost I used the mysql container name:

spring.datasource.url= jdbc:mysql://mysql-demo:3306/demographics?useSSL=false&allowPublicKeyRetrieval=true

  1. When I start the application, I get error: com.mysql.cj.jdbc.CommunicationException: Communications Link Failure mysql-demo

Please help!!!

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.

version: ‘3’

image: mysql:latest
- MYSQL_DATABASE=demographics
image: my_app_image
- docker-mysql
- 8080:8080
- DATABASE_HOST=docker-mysql
- DATABASE_NAME=demographics

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.

I used ‘host’ network instead of ‘bridge’ and the spring boot container connected to the mysql container.

1 Like

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:

java,oauth-security -jar myapp.jar

1 Like

Thank you @danilobrodrigues, that solved the problem for me!