0

I have a Spring Boot + MySQL application which cannot start in docker environment (manually it runs with no errors, but I have to change the application.properties)

So first I launch the MySQL in docker with these command:

docker run -p 3307:3306 --name mysqldb -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=exp mysql

And here it is the docker containers list:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
0cc5c690fb7e   mysql     "docker-entrypoint.s…"   30 seconds ago   Up 30 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysqldb

This is screenshot of server connection settings for the database

When the database is running, I launch the Spring app with following commands in terminal:

docker run -p 9090:8080 --name exp --net spring-net -e MYSQL_HOST=mysqldb -e MYSQL_USER=root -e MYSQL_PASSWORD=root -e MYSQL_PORT=3306 exp 

As you can see, I used the docker network which is connected to mysqldb with following command docker network connect spring-net mysqldb

Here are the application.properies settings:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show-sql=true

spring.datasource.url=jdbc:mysql://mysqldb:3307/exp?verifyServerCertificate=true&useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialization-mode=always
spring.datasource.validationQuery=SELECT 1
spring.datasource.testWhileIdle=true
spring.server.port=8080
spring.server.compression.enabled=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true

When I try to start this, the application cannot find the mysqldb, here is the error from console:

Caused by: java.net.UnknownHostException: mysqldb
        at java.net.InetAddress.getAllByName0(InetAddress.java:1281) ~[na:1.8.0_191]
        at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_191]
        at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_191]
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
        ... 170 common frames omitted

However, when I change the spring.datasource.url inside application.properties to this: spring.datasource.url=jdbc:mysql://localhost:3307/exp?verifyServerCertificate=true&useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false and build it it runs following exception:

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
        at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]

And

Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_191]

And

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-5.2.10.RELEASE.jar!/:5.2.10.RELEASE]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.10.RELEASE.jar!/:5.2.10.RELEASE]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.10.RELEASE.jar!/:5.2.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-5.2.10.RELEASE.jar!/:5.2.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.2.10.RELEASE.jar!/:5.2.10.RELEASE]
        ... 135 common frames omitted
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
1
  • Have you tried using --net spring-net when starting mysqldb? Commented Aug 5, 2021 at 12:50

1 Answer 1

1

I think it is too late to connect the mysql container to the same network as your spring container, so I would suggest you first create the network and use that in both of your containers.

$ docker network create my-network

Then you have to use the mysqldb container name and the inside port in your application.properties (because you are trying to connect from a container) like this:

spring.datasource.url=jdbc:mysql://mysqldb:3306/exp?verifyServerCertificate=true&useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.