title: SpringBoot on Docker unable to connect to MySQL
body: <p>I have written docker compose for my SpringBoot app with MySQL. When I run docker-compose up eventually I get an error:</p>
  
  <pre><code> 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.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
        at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:325) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
        ... 69 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:172) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
        at com.mysql.cj.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:444) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
        at java.sql.DriverManager.getConnection(DriverManager.java:664) ~[na:1.8.0_111]
        at java.sql.DriverManager.getConnection(DriverManager.java:208) ~[na:1.8.0_111]
        at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
        at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
        at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:196) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
        at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:159) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
        ... 70 common frames omitted
    Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
  </code></pre>
  
  <p>At first I thought the the cause was the db but I managed to run local instance of the app and connect to the docker mysql.
  Next I assumed that the spring app tries to connect to mysql before it is up and running but I restarted the app (mysql was already running) and got the same error.
  Currently I think that this might be caused by the windows10 firewall but I am running low on ideas.</p>
  
  <p>Dockerfile:</p>
  
  <pre><code>FROM java:8
  LABEL maintainer="maciej"
  WORKDIR /app
  COPY target/kamienica.jar /app/kamienica.jar
  ENTRYPOINT ["java", "-jar","kamienica.jar", "--spring.profiles.active=docker"]
  </code></pre>
  
  <p>docker-compose.yml</p>
  
  <pre><code>version: '3'
  
  services:
    mysql-docker-container:
      image: mysql:latest
      environment:
        - MYSQL_ROOT_PASSWORD=maciej
        - MYSQL_DATABASE=kamienica
        - MYSQL_USER=maciej
        - MYSQL_PASSWORD=maciej
      volumes:
        - /data/mysql
      ports:
      - 3333:3306
      container_name: kamienica-db
    kamienica-app:
      image: kamienica-image
      build:
        context: ./
        dockerfile: Dockerfile
      depends_on:
        - mysql-docker-container
      ports:
        - 8080:8080
      volumes:
        - /data/spring-boot-app
      container_name: kamienica-app
  </code></pre>
  
  <p>and the application-docker.properties:</p>
  
  <pre><code>jdbc.url = jdbc:mysql://kamienica-db:3333/kamienica?useSSL=false&amp;characterEncoding=UTF-8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC
  jdbc.username = maciej
  jdbc.password = maciej
  hibernate.show_sql = true
  hibernate.format_sql = true
  hibernate.hbm2ddl.auto=create-drop
  hibernate.dialect=org.hibernate.dialect.MySQLDialect
  hibernate.current_session_context_class=thread
  hibernate.connection.characterEncoding=UTF-8
  hibernate.connection.charSet=UTF-8
  </code></pre>