title: Can't connect Java program to MySQL using Docker
body: <p>I'm learning docker and trying to put my Java web application using Tomcat to container. I followed some basic tutorial but i found no solution to work properly to me. If i run my database and java containers i get the error:</p>
  
  <pre><code>SEVERE: Unable to create initial connections of pool.
  com.mysql.jdbc.exceptions.jdbc4.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:339)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:795)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:44)
  </code></pre>
  
  <p>MySQL Dockerfile</p>
  
  <pre><code>FROM mysql:latest
  ENV MYSQL_DATABASE=db_name #name of db that is required by Java program
  </code></pre>
  
  <p>Run by:</p>
  
  <pre><code>docker run --name db_name -e MYSQL_ROOT_PASSWORD=root -d db_name
  </code></pre>
  
  <p>Java Dockerfile</p>
  
  <pre><code>FROM tomcat:7.0.70-jre8
  ADD deploy /usr/local/tomcat/webapps #extracted .war
  ADD jdbc /usr/local/tomcat/lib #MySQL jdbc drivers
  ADD context /usr/local/tomcat/conf #context.xml
  </code></pre>
  
  <p>Run by:</p>
  
  <pre><code>docker run --name app_name --link db_name:db_name -p 8080:8080 -d app_name
  </code></pre>
  
  <p>Whole configuration was running properly when i was running it locally in Eclipse.</p>