Not able to dockerize a spring boot app with mysql

I am new to docker and created one spring boot app with mysql datasource but when i pull mysql image and then ran that image, mysql container is created. tested from bash also everything worked
but then i put container name in spring.datasource.url and try to build jar with mvn clean install
build is failing with error
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
and
Caused by: java.net.UnknownHostException: MysqlContainer

How to solve this?

Please show your compose file or docker run commands. It is hard to imagine what is actually happening just based on your description. When you share code or logs, anywhting with special characters, please format your post as described in our formatting guide: How to format your forum posts

Example code block:

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

I dont have docker-compose file
i pull mysql image and run it with command
docker run --name Mysqlcontainer -e MYSQL_ROOT_PASSWORD=root -p 8080:8080 mysql:latest
and then container named Mysqlcontainer is created and then I paste container name in spring.jdbc.url
my application.properties file is

spring.application.name=test
#${port:8080}
#spring.datasource.url=jdbc:mysql://localhost:3306/test_docker?serverTimezone=UTC
spring.datasource.url=jdbc:mysql://MysqlContainer:3306/test_docker?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

and then tried to create jar with command mvn clean install and got above mentioned error

Make it clean, create a Docker network, attach both containers to it. Then calling a container via its service name should work.

1 Like

To extend on @bluepuma77 's response: the default bridge network does not provide dns-based service discovery. Only user defined networks do!

You should look into docker compose, as it will help in creating repeatable deployments, and allow storing the configuration in a git repository. If you don’t know how to get started, try https://www.composerize.com to convert your docker run command to a compose file.

1 Like

Hi @meyay
i created docker-compose file but still same issue mysql is container is running successfully but it is giving above error while starting spring boot app
this is my docker-compose file

version: '3.8'

services:
  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_DATABASE: testdb_docker
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    depends_on:
      - db
    environment:
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root
      MYSQL_HOST: Mysqlcontainer

This is application.properties

spring.application.name=test
#${port:8080}
#spring.datasource.url=jdbc:mysql://localhost:3306/studentdb_docker?serverTimezone=UTC
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/testdb_docker
spring.datasource.username=${SPRING_DATASOURCE_USERNAME:root}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD:root}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

These are logs

2024-12-23 20:10:35 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2024-12-23 20:10:35 2024-12-23T14:40:35.709Z  WARN 1 --- [test] [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata
2024-12-23 20:10:35 
2024-12-23 20:10:35 org.hibernate.exception.JDBCConnectionException: unable to obtain isolated JDBC connection [Communications link failure
2024-12-23 20:10:35 
2024-12-23 20:10:35 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] [n/a]
2024-12-23 20:10:35     at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:100) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:58) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:108) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:94) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:116) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJdbcMetadata(JdbcEnvironmentInitiator.java:320) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:129) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:81) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:130) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:238) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:215) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.boot.model.relational.Database.<init>(Database.java:45) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getDatabase(InFlightMetadataCollectorImpl.java:226) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:194) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:171) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1431) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1502) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:390) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:419) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:400) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:523) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:336) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:289) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:970) ~[spring-context-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:627) ~[spring-context-6.2.1.jar!/:6.2.1]
2024-12-23 20:10:35     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-23 20:10:35     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-23 20:10:35     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-23 20:10:35     at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-23 20:10:35     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1361) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-23 20:10:35     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1350) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-23 20:10:35     at com.docker.test.TestApplication.main(TestApplication.java:10) ~[!/:0.0.1-SNAPSHOT]
2024-12-23 20:10:35     at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
2024-12-23 20:10:35     at java.base/java.lang.reflect.Method.invoke(Method.java:577) ~[na:na]
2024-12-23 20:10:35     at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:102) ~[app.jar:0.0.1-SNAPSHOT]
2024-12-23 20:10:35     at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:64) ~[app.jar:0.0.1-SNAPSHOT]
2024-12-23 20:10:35     at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:40) ~[app.jar:0.0.1-SNAPSHOT]
2024-12-23 20:10:35 Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2024-12-23 20:10:35 
2024-12-23 20:10:35 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2024-12-23 20:10:35     at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:165) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:55) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:837) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:420) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:238) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:180) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:137) ~[HikariCP-5.1.0.jar!/:na]
2024-12-23 20:10:35     at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:360) ~[HikariCP-5.1.0.jar!/:na]
2024-12-23 20:10:35     at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-5.1.0.jar!/:na]
2024-12-23 20:10:35     at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:461) ~[HikariCP-5.1.0.jar!/:na]
2024-12-23 20:10:35     at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:550) ~[HikariCP-5.1.0.jar!/:na]
2024-12-23 20:10:35     at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:98) ~[HikariCP-5.1.0.jar!/:na]
2024-12-23 20:10:35     at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:111) ~[HikariCP-5.1.0.jar!/:na]
2024-12-23 20:10:35     at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:126) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:467) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:61) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-23 20:10:35     ... 40 common frames omitted
2024-12-23 20:10:35 Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
2024-12-23 20:10:35 
2024-12-23 20:10:35 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2024-12-23 20:10:35     at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67) ~[na:na]
2024-12-23 20:10:35     at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
2024-12-23 20:10:35     at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483) ~[na:na]
2024-12-23 20:10:35     at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:52) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:95) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:140) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:156) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:79) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.NativeSession.connect(NativeSession.java:142) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:961) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     ... 53 common frames omitted
2024-12-23 20:10:35 Caused by: java.net.ConnectException: Connection refused
2024-12-23 20:10:35     at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
2024-12-23 20:10:35     at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
2024-12-23 20:10:35     at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:539) ~[na:na]
2024-12-23 20:10:35     at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:594) ~[na:na]
2024-12-23 20:10:35     at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
2024-12-23 20:10:35     at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
2024-12-23 20:10:35     at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:144) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:53) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-23 20:10:35     ... 56 common frames omitted
2024-12-23 20:10:35```

Since you database service is called db, this needs to become MYSQL_HOST: db

same issue, i have updated docker-compose file but still

version: '3.8'

services:
  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_DATABASE: testdb_docker
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    depends_on:
      - db
    environment:
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root
      MYSQL_HOST: db

This is the way :slight_smile:

Within a compose file, the services can connect to each other via the service names, all ports are reachable within the Docker network.

You could go one step further and add an explicit Docker network within compose and attach both containers.

Make sure all containers are running. Debug output the connection string to ensure it’s correct. Share the actual error.

It is really the same issue or just similar because you still get an error message?

Also note that just because the mysql container is running (depends_on), it doesn’t mean the server is ready to accept requests.

2024-12-24 20:25:42 Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-12-24 20:25:42 2024-12-24T14:55:42.757Z ERROR 1 --- [test] [           main] o.s.boot.SpringApplication               : Application run failed
2024-12-24 20:25:42 
2024-12-24 20:25:42 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure
2024-12-24 20:25:42 
2024-12-24 20:25:42 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] [n/a]
2024-12-24 20:25:42     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1808) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:523) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:336) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:289) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:970) ~[spring-context-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:627) ~[spring-context-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-24 20:25:42     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-24 20:25:42     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-24 20:25:42     at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-24 20:25:42     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1361) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-24 20:25:42     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1350) ~[spring-boot-3.4.1.jar!/:3.4.1]
2024-12-24 20:25:42     at com.docker.test.TestApplication.main(TestApplication.java:10) ~[!/:0.0.1-SNAPSHOT]
2024-12-24 20:25:42     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
2024-12-24 20:25:42     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
2024-12-24 20:25:42     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
2024-12-24 20:25:42     at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
2024-12-24 20:25:42     at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:102) ~[app.jar:0.0.1-SNAPSHOT]
2024-12-24 20:25:42     at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:64) ~[app.jar:0.0.1-SNAPSHOT]
2024-12-24 20:25:42     at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:40) ~[app.jar:0.0.1-SNAPSHOT]
2024-12-24 20:25:42 Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure
2024-12-24 20:25:42 
2024-12-24 20:25:42 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] [n/a]
2024-12-24 20:25:42     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:431) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:400) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     ... 22 common frames omitted
2024-12-24 20:25:42 Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure
2024-12-24 20:25:42 
2024-12-24 20:25:42 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] [n/a]
2024-12-24 20:25:42     at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:100) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:58) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:108) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:94) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:74) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:39) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcConnection(ImprovedExtractionContextImpl.java:63) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcDatabaseMetaData(ImprovedExtractionContextImpl.java:70) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.processTableResultSet(InformationExtractorJdbcDatabaseMetaDataImpl.java:65) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.extract.internal.AbstractInformationExtractorImpl.getTables(AbstractInformationExtractorImpl.java:570) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.getTablesInformation(DatabaseInformationImpl.java:122) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:72) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:233) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:112) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:280) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.lambda$process$5(SchemaManagementToolCoordinator.java:144) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at java.base/java.util.HashMap.forEach(HashMap.java:1425) ~[na:na]
2024-12-24 20:25:42     at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:141) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.boot.internal.SessionFactoryObserverForSchemaExport.sessionFactoryCreated(SessionFactoryObserverForSchemaExport.java:37) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:324) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:463) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1506) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:390) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:419) ~[spring-orm-6.2.1.jar!/:6.2.1]
2024-12-24 20:25:42     ... 26 common frames omitted
2024-12-24 20:25:42 Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2024-12-24 20:25:42 
2024-12-24 20:25:42 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2024-12-24 20:25:42     at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:165) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:55) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:837) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:420) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:238) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:180) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:137) ~[HikariCP-5.1.0.jar!/:na]
2024-12-24 20:25:42     at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:360) ~[HikariCP-5.1.0.jar!/:na]
2024-12-24 20:25:42     at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-5.1.0.jar!/:na]
2024-12-24 20:25:42     at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:461) ~[HikariCP-5.1.0.jar!/:na]
2024-12-24 20:25:42     at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:550) ~[HikariCP-5.1.0.jar!/:na]
2024-12-24 20:25:42     at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:98) ~[HikariCP-5.1.0.jar!/:na]
2024-12-24 20:25:42     at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:111) ~[HikariCP-5.1.0.jar!/:na]
2024-12-24 20:25:42     at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:126) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:467) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:46) ~[hibernate-core-6.6.4.Final.jar!/:6.6.4.Final]
2024-12-24 20:25:42     ... 47 common frames omitted
2024-12-24 20:25:42 Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
2024-12-24 20:25:42 
2024-12-24 20:25:42 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2024-12-24 20:25:42     at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
2024-12-24 20:25:42     at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78) ~[na:na]
2024-12-24 20:25:42     at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
2024-12-24 20:25:42     at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
2024-12-24 20:25:42     at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
2024-12-24 20:25:42     at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:52) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:95) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:140) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:156) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:79) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.NativeSession.connect(NativeSession.java:142) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:961) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     ... 60 common frames omitted
2024-12-24 20:25:42 Caused by: java.net.ConnectException: Connection refused
2024-12-24 20:25:42     at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
2024-12-24 20:25:42     at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:669) ~[na:na]
2024-12-24 20:25:42     at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
2024-12-24 20:25:42     at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
2024-12-24 20:25:42     at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:331) ~[na:na]
2024-12-24 20:25:42     at java.base/java.net.Socket.connect(Socket.java:630) ~[na:na]
2024-12-24 20:25:42     at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:144) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:53) ~[mysql-connector-j-9.1.0.jar!/:9.1.0]
2024-12-24 20:25:42     ... 63 common frames omitted

Hi @rimelek error logs ^^

You should learn to read your errors:

Either DB is not up a running or your connection string is wrong, I asked you to output the string in your app, that’s the first step in plain old debugging.

That is a good idea. If anything overwrites variables in an entrypoint or in the app, you can send it to the output before connecting and see if it is still right. I also suggested

This can be checked if you first start the database and a couple of seconds later you start the app. Also check the mysql logs as well. You can also share that too if you don’t understand it.

Thanks every one
I found the issue and solved it
actually my application is depends on MySQL db service but while running docker compose file before starting MySQL db container my application is tries to start, basically depends-on tag is not working so that issue occurs
I solved by adding condition in depends_on

    depends_on:
      db:
        condition: service_healthy

and service added this logic:

    healthcheck:
      test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
      interval: 10s
      timeout: 5s
      retries: 3

And now able to start service successfully