0

To create jar to build docker container for my spring boot applicaton, I run Maven Install from eclipse but build fails with below error:

2020-07-18 20:47:52.452  INFO 5920 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-07-18 20:47:52.566  INFO 5920 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-07-18 20:48:22.960  INFO 5920 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-07-18 20:48:23.299  INFO 5920 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate Core {5.4.9.Final}
2020-07-18 20:48:23.821  INFO 5920 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-07-18 20:48:54.341  WARN 5920 --- [  restartedMain] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata : HikariPool-1 - Connection is not available, request timed out after 30000ms.
2020-07-18 20:48:54.361  WARN 5920 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
2020-07-18 20:48:54.365  INFO 5920 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-07-18 20:49:03.446  INFO 5920 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2020-07-18 20:49:03.452  INFO 5920 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-07-18 20:49:03.522  INFO 5920 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-18 20:49:03.551 ERROR 5920 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

My application.properties file for spring boot application is:

spring.datasource.username=root
spring.datasource.password=ayush123
spring.datasource.url=jdbc:mysql://mysqldb:3306/employeedb
spring.datasource.platform=mysql
hibernate.hbm2ddl.auto=update
spring.jpa.generate-ddl=true
spring.datasource.hikari.initializationFailTimeout=-1

I have also tried with spring.datasource.url=jdbc:mysql://mysqldb/employeedb

I created docker network and built MYSQL container using below command:

docker pull mysql:8.0

docker network create edrms-net

docker container run --name mysqldb --network edrms-net -e MYSQL_ROOT_PASSWORD=ayush123 -e MYSQL_DATABASE=employeedb -d mysql:8.0

Upon docker container ls,I can see MYSQL contaner deployed and running.

enter image description here

I am completely lost and cannot recover. Please help!

Ayush28
  • 359
  • 3
  • 18
  • Is Eclipse somehow running the application as a container in `edrms-net` network? If not, you cannot connect directly to DNS container names from outside. For example: https://stackoverflow.com/questions/44780571/how-to-connect-with-mysql-db-running-as-container-in-docker – Andy Shinn Jul 18 '20 at 16:14
  • `edrms-net` is a bridge network. Can I publish this network in host mode to achieve what I want? Because when trying to create new host network, I cannot create, it gives `Error response from daemon: only one instance of "host" network is allowed`. Also, let me try the suggested post and revert. – Ayush28 Jul 18 '20 at 16:26
  • You don't need any network in this case. If you want to connect to the container from outside Docker you want to publish the MySQL port and then use the host IP. – Andy Shinn Jul 18 '20 at 16:28
  • I will eventually use netowrk, hence running the container from within a docker network. Also, I tried pulishing container port 3306 to VM port 3306 but that didn't solve the issue. – Ayush28 Jul 18 '20 at 17:16

1 Answers1

1

To connect to mySql server inside docker container you must publish port when running that container on which you want to connect to the server.

To achieve that add --publish 3306:3306 to your docker container run command.

Alexandr Ivanov
  • 389
  • 1
  • 5
  • 7
  • I still get the same error after publishing container port 3306 to VM port 3306. `docker container ls` shows port for mysqldb as 0.0.0.0:3306->3306` instead as shown in above image. Do I need to change my datasource URL from container name to VM IP @ `spring.datasource.url=jdbc:mysql://mysqldb:3306/employeedb` ? – Ayush28 Jul 18 '20 at 17:11
  • Yes, `mysqldb` name will only be available to containers. Change to your host IP until you are running as container. – Andy Shinn Jul 18 '20 at 17:34