Can't connect Java program to MySQL using Docker

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:

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.<init>(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.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)

MySQL Dockerfile

  • Docker | How can I remove all containers but a certain few
  • Unable to connect to remote MySQL from a docker on Ubuntu Desktop
  • Using fs.stat to get stats from a file inside a docker container directory
  • Unable to start Docker in Windows 10 - Hyper-V error is thrown
  • Copy files between docker machines
  • REST request across networks
  • FROM mysql:latest
    ENV MYSQL_DATABASE=db_name #name of db that is required by Java program
    

    Run by:

    docker run --name db_name -e MYSQL_ROOT_PASSWORD=root -d db_name
    

    Java Dockerfile

    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
    

    Run by:

    docker run --name app_name --link db_name:db_name -p 8080:8080 -d app_name
    

    Whole configuration was running properly when i was running it locally in Eclipse.

  • Could i run docker in a docker Container Ubuntu 14.04
  • Bluemix Kubernetes docker deployment query
  • Ansible error due to GMP package version on Centos6
  • Docker ENV command no longer valid when using docker run
  • docker runs image on wrong port
  • How is Vagrant an advantage over boot2docker? [closed]
  • One Solution collect form web for “Can't connect Java program to MySQL using Docker”

    Because you do not provide the full stack tace, which would show what connection string tomcat is using, I have to guess that you do not provide the proper connection string to your tomcat conainer. You have to supply an connectionstring like:

    jdbc:mysql://database_container_name:3306/database_name
    

    into your tomcat config.


    BTW:

    You should rearrange you lines in the Tomcat Dockerfile to

    FROM tomcat:7.0.70-jre8
    ADD jdbc /usr/local/tomcat/lib #MySQL jdbc drivers
    ADD context /usr/local/tomcat/conf #context.xml
    ADD deploy /usr/local/tomcat/webapps #extracted .war
    

    Because docker can cache build layers. With the old Order your war is the first layer of the image and changes every time you make a change to your application causing the folliwing layers to be rebuild every time, even if they did not change. The new order uses the docker chache better. With this order the never changing MySQL driver is always cached and the config also does not alter as fast as the war.

    In this example the effect may be minimal, but if you build bigger images with more layers and lengthy build steps (like apt-get install smth) the chache can significantly speed up your build.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.