4

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

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.

2
  • What is mysql_hostname or mysql connection string into java configuration file? Commented Jul 30, 2016 at 21:12
  • There was "url="jdbc:mysql://localhost:3306/db_name". I changed to "jdbc:mysql://db_name:3306/db_name" like @Ohmen advise and it's work now properly. Commented Jul 31, 2016 at 20:38

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.