0

I am very new to docker, I have a jar file of simple java app which connects to the database on localhost:3306, I don't want to create any new docker mysql server, I want my app to be connected with my existing database. I want to run this jar from docker. My Dockerfile is

 FROM java:8  
     COPY . /var/www/java  
     WORKDIR /var/www/java  
     CMD ["java", "-jar","exp.jar"]

but it is showing error

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
   Communications link failure Last packet sent to the server was 0 ms
   ago. 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:406) at
   com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
   at
   com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
   at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718) at
   com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) 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:406) at
   com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302) at
   com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
   at java.sql.DriverManager.getConnection(DriverManager.java:664) at
   java.sql.DriverManager.getConnection(DriverManager.java:247) at
   com.linkeztech.code.App.main(App.java:37) Caused by:
   java.net.ConnectException: Connection refused (Connection refused)

What do I do for it? I'm stuck please help me. Thanks.

1
  • what is you host machine os? mac, linux, windows? Commented Sep 26, 2017 at 12:20

1 Answer 1

1

Linux Host

Start the container using the network mode host. docker run --network host .... This will make the container share the network stack with the host. Thus a connection to localhost:3306 from the container will connect to the database.

Docker for MAC

From inside the application container connect to docker.for.mac.localhost:3306 rather than localhost:3306. docker.for.mac.localhost is a special DNS name that will resolve to the IP of the host machine.

Docker for Windows

Run ipconfig on the windows machine and find the IP address for Ethernet adapter vEthernet (DockerNAT) network interface. Use that IP to connect to the database.

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.