I’m trying to run my spring boot application on docker with mysql.
Everything is working fine, MySQL DB starts successfully and after that spring application is deployed in container and I end up getting this error :
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
This is my docker compose yaml file content:
version: "3.8"
services:
mysql-db:
image: mysql
container_name: mysqldb
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: employeedb
MYSQL_HOST: mysql-db
MYSQL_PORT: 3306
networks:
- mt-network
networks:
mt-network:
driver: bridge
And my application-properties.yml file content:
spring:
datasource:
password: root
url: jdbc:mysql://mysql-db:3306/employeedb
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
I tried all ways to fix this but I’m still facing this issue. Please don’t put this question as Duplicate Question.
Thank you in advance.
2
Answers
There was something wrong with mysql container I guess it was not starting properly.
So I added HealthCheck in docker compose and now after i run
docker-compose up
the mysql container starts and after that it stops for few second. After few seconds my spring application starts working without giving me error : Communication Link Failure.EDIT:
Usually whats happening is that until MySQL container is not in healthy state it will re-run it as many times which is given in docker-compose yaml file. And when the container is healthy it executes Spring Application container.
Below is my docker-compose.yml file,
Here, I don't know about IP "127.0.1.1" but I read somewhere we can configure port number with IP like I did in my docker-compose.yml. If someone knows what that IP means please explain it.
I use that IP to connect Dockerized MySQL Server with my local MySQL Workbench.
Thanks Once Again.
First, remove the whole network configuration, it’s unnecessary:
You should use the container name
mysqldb
as hostname if your Spring Boot application is started with Docker Compose as well:The documentation states:
If you start your Spring Boot app from your IDE, you need to use localhost as hostname because the app won’t be in the same network as the Docker container and won’t therefore not be able to resolve the hostname
mysqldb
: