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)
FROM mysql:latest ENV MYSQL_DATABASE=db_name #name of db that is required by Java program
docker run --name db_name -e MYSQL_ROOT_PASSWORD=root -d db_name
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
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.
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:
into your tomcat config.
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.