title: Connect tomcat and mysql in docker-compose
body: <p>I'm working on project for studies. Now I am at the stage of moving project to docker containers. I have placet tomcat with my website and mysql with database do docker-compose.yml. Unfortunately classes from tomcat can't connect with database</p>
  
  <hr>
  
  <h2>Java code</h2>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  public class Waliduj {
  
  String url="jdbc:mysql://localhost:3306/uczelnia?useTimezone=true&amp;serverTimezone=UTC";
  String username="root";
  String password="121212";
  
  public boolean Waliduje(String uname,String pass) {
  
  
      try {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con = DriverManager.getConnection(url,username,password);
          PreparedStatement st=con.prepareStatement("SELECT email,haslo FROM logowanie WHERE email=? AND haslo=?");
          st.setString(1,uname);
          st.setString(2,pass);
          ResultSet rs = st.executeQuery();
          if(rs.next()) {
              return true;
          }
      } catch (SQLException e) {
          e.printStackTrace();
      } catch (ClassNotFoundException se) {
          // TODO Auto-generated catch block
          se.printStackTrace();
      }
  
      return false;
  }
  }
  </code></pre>
  
  <hr>
  
  <h2>Dockerfile code</h2>
  
  <pre><code>FROM tomcat:8.0.41-jre8
  COPY ./webapp /usr/local/tomcat/webapps/webapp
  CMD ["catalina.sh", "run"]
  </code></pre>
  
  <hr>
  
  <h2>docker-compose.yml code</h2>
  
  <pre><code>version: '3'
  services: 
      web:
          build: .
          ports:
           - "8080:8080"
          links:
           - "db:uczelnia"
      db:
          image: mysql
          environment:
           MYSQL_ROOT_PASSWORD: 121212
           MYSQL_DATABASE: uczelnia
  
          ports:
           - "3306:3306" 
      PhpMyAdmin:
          image: nazarpc/phpmyadmin
          ports:
           - "8090:80"
          links:
           - db:mysql
  </code></pre>
  
  <hr>
  
  <h2>Error code</h2>
  
  <pre><code>web_1         | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
  web_1         |
  web_1         | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  web_1         |         at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
  web_1         |         at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
  web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
  web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:444)
  web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
  web_1         |         at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
  web_1         |         at java.sql.DriverManager.getConnection(DriverManager.java:664)
  web_1         |         at java.sql.DriverManager.getConnection(DriverManager.java:247)
  web_1         |         at com.login.walidacja.Waliduj.Waliduje(Waliduj.java:20)
  web_1         |         at com.login.Login.doPost(Login.java:27)
  web_1         |         at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
  web_1         |         at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  web_1         |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
  web_1         |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
  web_1         |         at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  web_1         |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
  web_1         |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
  web_1         |         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
  web_1         |         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
  web_1         |         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
  web_1         |         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
  web_1         |         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  web_1         |         at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
  web_1         |         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
  web_1         |         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
  web_1         |         at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
  web_1         |         at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
  web_1         |         at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
  web_1         |         at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
  web_1         |         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  web_1         |         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  web_1         |         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  web_1         |         at java.lang.Thread.run(Thread.java:745)
  web_1         | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
  web_1         |
  web_1         | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  web_1         |         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  web_1         |         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  web_1         |         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  web_1         |         at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
  web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)
  web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)
  web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)
  web_1         |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:92)
  web_1         |         at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
  web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)
  web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
  web_1         |         ... 30 more
  web_1         | Caused by: java.net.ConnectException: Connection refused (Connection refused)
  web_1         |         at java.net.PlainSocketImpl.socketConnect(Native Method)
  web_1         |         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
  web_1         |         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
  web_1         |         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
  web_1         |         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  web_1         |         at java.net.Socket.connect(Socket.java:589)
  web_1         |         at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:173)
  web_1         |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:66)
  web_1         |         ... 33 more
  </code></pre>
  
  <hr>
  
  <h2>Additional informations:</h2>
  
  <ul>
  <li>I'm working on windows 10 so docker is on windows too.</li>
  <li>I apologize for spelling errors but I'm from poland so english is not
  my</li>
  </ul>