For some reason when the spring boot app is running in the container it can't connect to the MySQL container, I checked that the database container was up and running successfully by trying to connect to it from MySQL workbench and query data from it however that app itself fails to start up
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
docker-compose.yml
version: '3.3'
services:
db:
image: mydb
container_name: mysql-db2
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
demo:
image: springboot
container_name: my-spring-boot-app
depends_on:
- db
ports:
- 8080:8080
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db2:3306/mydb
application.properties
spring.jpa.hibernate.ddl.auto=update
spring.datasource.url=jdbc:mysql://mysql-db2:3306/mydb?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Dockerfile (MySQL)
FROM mysql
MAINTAINER me
ENV MYSQL_DATABASE=mydb \
MYSQL_ROOT_PASSWORD=root
ADD springboot.sql /docker-entrypoint-initdb.d
EXPOSE 3306
Dockerfile (Spring boot)
FROM openjdk:20-ea-17-jdk
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
pom.xml (mySQL dependency version)
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
Log File
2022-12-29 14:24:14
2022-12-29 14:24:14 . ____ _ __ _ _
2022-12-29 14:24:14 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2022-12-29 14:24:14 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2022-12-29 14:24:14 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2022-12-29 14:24:14 ' |____| .__|_| |_|_| |_\__, | / / / /
2022-12-29 14:24:14 =========|_|==============|___/=/_/_/_/
2022-12-29 14:24:14 :: Spring Boot :: (v3.0.0)
2022-12-29 14:24:14
2022-12-29 14:24:14 2022-12-29T13:24:14.135Z INFO 1 --- [ main] c.e.demo.application.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 20-ea with PID 1 (/app.jar started by root in /)
2022-12-29 14:24:14 2022-12-29T13:24:14.144Z INFO 1 --- [ main] c.e.demo.application.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2022-12-29 14:24:14 2022-12-29T13:24:14.734Z INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-12-29 14:24:14 2022-12-29T13:24:14.778Z INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 37 ms. Found 1 JPA repository interfaces.
2022-12-29 14:24:15 2022-12-29T13:24:15.502Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-12-29 14:24:15 2022-12-29T13:24:15.514Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-12-29 14:24:15 2022-12-29T13:24:15.514Z INFO 1 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.1]
2022-12-29 14:24:15 2022-12-29T13:24:15.570Z INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-12-29 14:24:15 2022-12-29T13:24:15.572Z INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1327 ms
2022-12-29 14:24:15 2022-12-29T13:24:15.730Z INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-12-29 14:24:15 2022-12-29T13:24:15.767Z INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.1.5.Final
2022-12-29 14:24:15 2022-12-29T13:24:15.887Z WARN 1 --- [ main] org.hibernate.orm.deprecation : HHH90000021: Encountered deprecated setting [javax.persistence.sharedCache.mode], use [jakarta.persistence.sharedCache.mode] instead
2022-12-29 14:24:15 2022-12-29T13:24:15.968Z INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-12-29 14:24:17 2022-12-29T13:24:17.085Z ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
2022-12-29 14:24:17
2022-12-29 14:24:17 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2022-12-29 14:24:17
2022-12-29 14:24:17 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2022-12-29 14:24:17 at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2022-12-29 14:24:17 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2022-12-29 14:24:17 at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2022-12-29 14:24:17 at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2022-12-29 14:24:17 at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2022-12-29 14:24:17 at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2022-12-29 14:24:17 at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-5.0.1.jar!/:na]
2022-12-29 14:24:17 at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359) ~[HikariCP-5.0.1.jar!/:na]
2022-12-29 14:24:17 at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-5.0.1.jar!/:na]
2022-12-29 14:24:17 at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470) ~[HikariCP-5.0.1.jar!/:na]
2022-12-29 14:24:17 at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-5.0.1.jar!/:na]
2022-12-29 14:24:17 at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100) ~[HikariCP-5.0.1.jar!/:na]
2022-12-29 14:24:17 at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-5.0.1.jar!/:na]
2022-12-29 14:24:17 at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:284) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:177) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:36) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:119) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:255) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:230) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.boot.model.relational.Database.<init>(Database.java:44) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getDatabase(InFlightMetadataCollectorImpl.java:218) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:191) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:138) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1350) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1421) ~[hibernate-core-6.1.5.Final.jar!/:6.1.5.Final]
2022-12-29 14:24:17 at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~[spring-orm-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[spring-orm-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~[spring-beans-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~[spring-beans-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~[spring-beans-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1130) ~[spring-context-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:905) ~[spring-context-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.2.jar!/:6.0.2]
2022-12-29 14:24:17 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.0.jar!/:3.0.0]
2022-12-29 14:24:17 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-3.0.0.jar!/:3.0.0]
2022-12-29 14:24:17 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[spring-boot-3.0.0.jar!/:3.0.0]
2022-12-29 14:24:17 at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-3.0.0.jar!/:3.0.0]
2022-12-29 14:24:17 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.0.jar!/:3.0.0]
2022-12-29 14:24:17 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.0.jar!/:3.0.0]
2022-12-29 14:24:17 at com.example.demo.application.DemoApplication.main(DemoApplication.java:15) ~[classes!/:0.0.1-SNAPSHOT]
2022-12-29 14:24:17 at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
2022-12-29 14:24:17 at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
2022-12-29 14:24:17 at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[app.jar:0.0.1-SNAPSHOT]
2022-12-29 14:24:17 at org.springframework.boot.loader.Launcher.launch(Launcher.java:95) ~[app.jar:0.0.1-SNAPSHOT]
2022-12-29 14:24:17 at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[app.jar:0.0.1-SNAPSHOT]
2022-12-29 14:24:17 at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) ~[app.jar:0.0.1-SNAPSHOT]
2022-12-29 14:24:17 Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
2022-12-29 14:24:17
CodePudding user response:
Solution is to add below property to application.properties
file.
NOTE: This solution needs restart of Spring Boot container alone separately after running docker-compose file. I think this is needed as MySQL takes some time to start-up.
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
application.properties
spring.jpa.hibernate.ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.url=jdbc:mysql://mysql-db2:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
docker-compose.yml
version: '3.3'
services:
db:
image: mydb
build: /Users/MyiCloud/IdeaProjects/so/mysql/
container_name: mysql-db2
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3306:3306"
demo:
image: springboot
build: /Users/MyiCloud/IdeaProjects/so/
container_name: my-spring-boot-app
depends_on:
- db
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db2:3306/mydb
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>so</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>