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 application.properties 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
spring.datasource.username=test
spring.datasource.password=test1234

  1. When I start the application, I get error: com.mysql.cj.jdbc.CommunicationException: Communications Link Failure
    Java.net.UnknownHostException: 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’

services:
docker-mysql:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=MySQL_Num1
- MYSQL_DATABASE=demographics
- MYSQL_USER=test
- MYSQL_PASSWORD=test1234
spring-boot-jpa-docker-webapp:
image: my_app_image
depends_on:
- docker-mysql
ports:
- 8080:8080
environment:
- DATABASE_HOST=docker-mysql
- DATABASE_USER=test
- DATABASE_PASSWORD=test1234
- DATABASE_NAME=demographics
- DATABASE_PORT=3306

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.

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 -Dspring.profiles.active=dockerembbed,oauth-security -jar myapp.jar

1 Like

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

Hi , i also had the same problem but replaced “depends_on:” with “links:” and it worked

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