Docker Spring JDBC App fails to connect to Mysql database

I am migrating my project on docker but facing issues.

Requirement - Need my application to connect to two existing mysql databases to fetch data.

Problem - Unable to connect two either of the database with the given configuration.

What it Seems to me- Spring JDBC template needs to have some additional library to connect the database to localhost.

Error - Communication Link Failure

Application Properties:

spring.application.name = DBService
server.port = 8081

#write-database: savvis2_write
spring.datasource.jdbcUrl = jdbc:mysql://mysql-master:3306/savvis2_write?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username = root
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.password = root

#read-database: savvis2_read
spring.datasource.secondary.jdbcUrl = jdbc:mysql://mysql-slave:3306/savvis2_read?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.secondary.username = root
spring.datasource.secondary.password = root
spring.datasource.secondary.driverClassName = com.mysql.cj.jdbc.Driver

DataSourceConfig File :

package com.svcomfort.configurations;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

@Bean(name = "savvis2_write")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource2() {
	return DataSourceBuilder.create().build();
}

@Bean(name = "writeTemplate")
@Primary
public NamedParameterJdbcTemplate jdbcTemplate2(@Qualifier("savvis2_write") DataSource ds) {
	return new NamedParameterJdbcTemplate(ds);
}



@Bean(name = "writeTransactionManager")
@Autowired
DataSourceTransactionManager tm2(@Qualifier("savvis2_write") DataSource datasource) {
	return new DataSourceTransactionManager(datasource);
}


@Bean(name = "savvis2_read")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource dataSource1() {
	return DataSourceBuilder.create().build();
}

@Bean(name = "readTemplate")
public NamedParameterJdbcTemplate jdbcTemplate1(@Qualifier("savvis2_read") DataSource ds) {
	return new NamedParameterJdbcTemplate(ds);
} 

@Bean(name = "readTransactionManager")
@Autowired
DataSourceTransactionManager tm1(@Qualifier("savvis2_read") DataSource datasource) {
	return new DataSourceTransactionManager(datasource);
} 

}

DockerFile:
FROM openjdk:11-jre-slim
EXPOSE 8081
ADD target/DBService.jar DBService.jar
ENTRYPOINT [“java”, “-jar”, “/DBService.jar”]

Docker-Compose File:
services:
mysql-master:
image: “mysql:8”
container_name: mysql-master
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: savvis2_write
MYSQL_PASSWORD: root
MYSQL_USER: root
ports:

  • 3316:3306
    networks:
  • mysql-net

mysql-slave:
image: “mysql:8”
container_name: mysql-slave
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: savvis2_read
MYSQL_PASSWORD: root
MYSQL_USER: root
ports:

  • 3317:3306
    networks:
  • mysql-net

multi_schema_service:
container_name: multidbserver
build:
context: .
dockerfile: Dockerfile
ports:

  • 8081:8081
    depends_on:
  • mysql-master
    networks:
  • mysql-net
    depends_on:
  • mysql-slave
    volumes:
  • .m2:/root/.m2

networks:
mysql-net:
driver: bridge


Please, format your post according to the following guide: How to format your forum posts
In short: please, use </> button to share codes, terminal outputs, error messages or anything that can contain special characters which would be interpreted by the MarkDown filter. Use the preview feature to make sure your text is formatted as you would expect it and check your post after you have sent it so you can still fix it.

Example code block:

```
echo "I am a code."
echo "An athletic one, and I wanna run."
```

After fixing your post, please send a new comment so people are notified about the fixed content.