Connection Spring Boot Cloud with MySql Workbench in Docker Compose resulting in error

  docker, docker-compose, java, mysql, spring-boot

Hello and thanks for your time and help! i started using Docker in the past week and i trying to make a project with the help of Docker, using a Spring Boot App, a MySql in Docker using docker compose. I tested if my Spring boot app connect to mySql Workbench in the classic way, outside of Docker, and it’s work, but somehow, when i use it in Docker, docker-compose context, i got the next errors, idk why:

echipa_1         | 2021-02-27 16:39:02.127  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.27.Final

echipa_1         | 2021-02-27 16:39:19.236  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
echipa_1         | 2021-02-27 16:39:32.364  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
echipa_1         | 2021-02-27 16:39:33.533 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
echipa_1         |
echipa_1         | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
echipa_1         |
echipa_1         | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
echipa_1         |      at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar!/:na]
echipa_1         |      at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar!/:na]
echipa_1         |      at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar!/:na]
echipa_1         |      at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.5.jar!/:na]
echipa_1         |      at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.5.jar!/:na]
echipa_1         |      at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.5.jar!/:na]
echipa_1         |      at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.5.jar!/:na]
echipa_1         |      at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:176) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:127) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
echipa_1         |      at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.12.RELEASE.jar!/:5.2.12.RELEASE]
echipa_1         |      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.8.RELEASE.jar!/:2.3.8.RELEASE]
echipa_1         |      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.8.RELEASE.jar!/:2.3.8.RELEASE]
echipa_1         |      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.8.RELEASE.jar!/:2.3.8.RELEASE]
echipa_1         |      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) ~[spring-boot-2.3.8.RELEASE.jar!/:2.3.8.RELEASE]
echipa_1         |      at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.8.RELEASE.jar!/:2.3.8.RELEASE]
echipa_1         |      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.8.RELEASE.jar!/:2.3.8.RELEASE]
echipa_1         |      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.8.RELEASE.jar!/:2.3.8.RELEASE]
echipa_1         |      at com.petrea.BoysDocker.BoysRevenindApplication.main(BoysRevenindApplication.java:10) ~[classes!/:0.0.1-SNAPSHOT]
echipa_1         |      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
echipa_1         |      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
echipa_1         |      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
echipa_1         |      at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
echipa_1         |      at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[boys-revenind.jar:0.0.1-SNAPSHOT]
echipa_1         |      at org.springframework.boot.loader.Launcher.launch(Launcher.java:107) ~[boys-revenind.jar:0.0.1-SNAPSHOT]
echipa_1         |      at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[boys-revenind.jar:0.0.1-SNAPSHOT]
echipa_1         |      at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[boys-revenind.jar:0.0.1-SNAPSHOT]
echipa_1         | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
echipa_1         |
echipa_1         | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
echipa_1         |      at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
echipa_1         |      at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
echipa_1         |      at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
echipa_1         |      at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
echipa_1         |      at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      ... 58 common frames omitted
echipa_1         | Caused by: java.net.ConnectException: Connection refused (Connection refused)
echipa_1         |      at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
echipa_1         |      at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
echipa_1         |      at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na]
echipa_1         |      at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
echipa_1         |      at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
echipa_1         |      at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
echipa_1         |      at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
echipa_1         |      ... 61 common frames omitted
echipa_1         |
echipa_1         | 2021-02-27 16:39:33.605  WARN 1 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata
echipa_1         |

What i did so far:

-check if configuration it’s working in Spring Boot + MySql Workbench, without using Docker – works

-add dependency: hibernate-entitymanager, core, jaxb-api — works, no error appers when i run without Docker

-reinstall Sql Server – did that, no change

Soo, i think i miss something, maybe in the compose, maybe in the configuration?

Dockerfile for my Spring Boot App:

FROM openjdk:11
LABEL maintainer = "Petrea"
COPY target/boys-revenind.jar boys-revenind.jar 
ENTRYPOINT ["java", "-jar", "/boys-revenind.jar"]

My configuration of Spring Boot app for Sql:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: ''
    url: jdbc:mysql://localhost:3306/petrea
    username: root
  jpa:
    database-platform: org.hibernate.dialect.MySQL5Dialect
    generate-ddl: true
    show-sql: true

-"petrea" being my DB, don’t have a password for my root user, i dont know if that localhost is setted correct for the Docker…

My DockerCompose file:

version: "2.1"

services: 
  database:
    container_name: database
    image: mysql:8.0
    hostname: database
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: ""
      MYSQL_USERNAME: "root"
      MYSQL_DATABASE: "petrea"
    volumes:
      - my-datavolume:/var/lib/mysql
    networks:
      backend:
        aliases:
          - "database"
    healthcheck:  
        test: ["CMD", "mysqladmin" ,"ping", "-h", "database"] 
        interval: 25s
        timeout: 20s
        retries: 5

  echipa:
    build: C:UsersmariusVideosboys-revenind #Image: targetboys-revenind.jar
    environment:
      PROFILE: "dev"
      SPRING_DATASOURCE_URL: "jdbc:mysql://database:3306/petrea"
      SPRING_DATASOURCE_USERNAME: "root"
      SPRING_DATASOURCE_PASSWORD: ""
    depends_on:
      database:
        condition: service_healthy
    ports:
      - "1111:1111"
    restart: unless-stopped
    networks:
      - backend

     
volumes:
  my-datavolume:
        
networks:
  backend:
    driver: bridge

I using MySql Workbench with Xaamp, Server is running oke, but i don’t know what to do…
Feel free to ask about configuration or maybe somethings that’s it’s essential and maybe i did it wrong.
Thanks!

Source: Docker Questions

LEAVE A COMMENT