title: docker-compose + spring-boot + mysql = CommunicationsException: Communications link failure
body: <p>The spring boot app fails with <strong><code>liquibase.exception.DatabaseException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure</code></strong> caused by: <strong><code>java.net.UnknownHostException: mysql-server: Name does not resolve
  </code></strong></p>
  
  <p>Tried to wait for <code>mysql</code> with waiting for condition:<code>service_healthy</code> and restart app <strong>on-failure</strong>. No effect.</p>
  
  <p>docker-compose:</p>
  
  <pre><code>version: '2.1'
  services:
  
    db:
      image: mysql:5.7
      restart: always
      container_name: db
      environment:
        MYSQL_DATABASE: motorcycle_info
        MYSQL_USER: demo
        MYSQL_PASSWORD: demo
        MYSQL_ROOT_PASSWORD: password
      ports:
        - '3306:3306'
      healthcheck:
        test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
        timeout: 20s
        retries: 10
  
    backend-spring-boot-app:
      container_name: backend-spring-boot-app
      build:
        context: ./backend
        dockerfile: Dockerfile
      image: backend-spring-boot-app:latest
      restart: on-failure
      links:
        - "db:db"
      depends_on:
        db:
          condition: service_healthy
      ports:
        - 8090:8090
      environment:
        - DATABASE_HOST=db
        - DATABASE_USER=demo
        - DATABASE_PASSWORD=demo
        - DATABASE_NAME=motorcycle_info
        - DATABASE_PORT=3306
  </code></pre>
  
  <p>Dockerfile for backend-spring-boot-app:</p>
  
  <pre><code>FROM openjdk:8
  COPY target/backend-0.0.1-SNAPSHOT.jar backend-0.0.1-SNAPSHOT.jar
  ENTRYPOINT ["java", "-jar", "backend-0.0.1-SNAPSHOT.jar"]
  </code></pre>
  
  <p>app props:</p>
  
  <pre><code># Datasource
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  spring.datasource.url=jdbc:mysql://db:3306/motorcycle_info?createDatabaseIfNotExist=true&amp;autoReconnect=true&amp;useSSL=false # for db container
  spring.datasource.username=root
  spring.datasource.password=root
  
  # Show or not log for each sql query
  spring.jpa.show-sql = true
  
  # The SQL dialect makes Hibernate generate better SQL for the chosen database
  spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
  
  # Hibernate naming strategy
  spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  
  # Liquibase
  spring.liquibase.change-log=classpath:/db/liquibase-changelog.xml
  
  # Application context
  server.servlet.context-path=/bearings-info/api
  server.port=8090
  
  #Logging
  logging.file=log/application.log
  logging.level.root=DEBUG
  </code></pre>
  
  <p>exception log:</p>
  
  <pre><code> org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.DatabaseException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
  
   The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
      at com.pazukdev.backend.BackendApplication.main(BackendApplication.java:10) [classes!/:0.0.1-SNAPSHOT]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
      at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [backend-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
      at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [backend-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
      at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [backend-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
      at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [backend-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
   Caused by: liquibase.exception.DatabaseException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
  
   The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:307) ~[liquibase-core-3.6.3.jar!/:na]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1821) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
      ... 26 common frames omitted
   Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
  
   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.15.jar!/:8.0.15]
      at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
      at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
      at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
      at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
      at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
      at com.zaxxer.hikari.pool.HikariPool.&lt;init&gt;(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
      at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
      at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:302) ~[liquibase-core-3.6.3.jar!/:na]
      ... 28 common frames omitted
   Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
  
   The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_212]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
      at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.NativeSession.connect(NativeSession.java:152) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      ... 39 common frames omitted
   Caused by: java.net.UnknownHostException: mysql-server: Name does not resolve
      at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_212]
      at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_212]
      at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_212]
      at java.net.InetAddress.getAllByName0(InetAddress.java:1277) ~[na:1.8.0_212]
      at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
      at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
      at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
      ... 42 common frames omitted
  
  db | 2019-07-31T07:25:18.147239Z 3 [Note] Access denied for user 'root'@'localhost' (using password: NO)
  </code></pre>
  
  <p>Was googling a lot, but still didn't found solution.</p>
  
  <p>Whats wrong with my docker-compose?</p>
  
  <p>Pay attention on last line in log: Access denied for user 'root'@'localhost'. This is result of service healthcheck with ping. So, that means that db is already running.</p>
  
  <p><strong>Aditionally:</strong> why access is denied? This healthcheck is the same as in example I had found ( <a href="https://stackoverflow.com/questions/42567475/docker-compose-check-if-mysql-connection-is-ready">Docker-compose check if mysql connection is ready</a> ) and there was no any additional configs neither for db nor for ping command. </p>