1

I'm relatively new to Docker and I'm trying to deploy a Java Springboot application to a container and a MySQL server to another container then allow for a connection between both containers.

The Java application uses java.sql.DriverManager to communicate with an instance of a MySQL server hosted locally on my machine in a docker container, the application connects to the MySQL server correctly when executed outside of a docker container, however, when I try to execute a docker container of my Java application I receive this error Communications link failure java.net.ConnectException: Connection refused.

I tried changing the connections URI from jdbc:mysql://127.0.0.1:3306/online_store
to jdbc:mysql://mysql-container:3306/online_store (Note: mysql-container is the name of my docker container that hosts MySQL server) then rebuilt the docker image and ran the container, and the application was successfully able to connect to the MySQL server, was this supposed to happen? if so, I'd like to know why does running the same Java application on different environments (these environments being my host OS and a docker container) require modification of the MySQL URI although the application in both environments is communicating with the same MySQL server instance.

initializing the connection with the MySQL server:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DatabaseConnection {
    private static Connection connection;

    public static Connection getInstance(){
        if(connection == null){
            try{
                Class.forName("com.mysql.cj.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/online_store", "root", "1234"); // this only works correctly if the application is being executed outside of a Docker container
            }
            catch (Exception e){e.printStackTrace();}
        }
        return connection;
    }
}

Dockerfile:

FROM openjdk:11
ADD out/artifacts/online_store_api_jar/online-store-api.jar online-store-api.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "online-store-api.jar"]

Edit: Here is how I configure the Docker container building and communcation via Windows power shell.

>docker pull mysql:5.7
>docker run --name mysql-container -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 mysql:5.7
>docker build . -t online-store-image 
>docker run -p 8080:8080 --name online-store-container --link mysql-container:mysql -d online-store-image 
A. Khaled
  • 168
  • 1
  • 10
  • Can you add your mysql container details like how you configure the container – Eklavya Apr 16 '20 at 15:38
  • @AbinashGhosh done, updated my question with docker configuration. – A. Khaled Apr 16 '20 at 15:48
  • which OS are you using, are you use any VM for this ? – Eklavya Apr 16 '20 at 15:49
  • I'm using Windows 10, and no I'm not using any VM – A. Khaled Apr 16 '20 at 15:50
  • for windows it generally use a VM and there is ip for VM (If you use Docker Desktop or Docker toolbox) – Eklavya Apr 16 '20 at 15:51
  • Can you try with that ip ?and add user in docker-user group also – Eklavya Apr 16 '20 at 16:08
  • I don't really know where to get that ip from, and what is a docker user group? excuse my ignorance as stated in the question I have just been introduced to Docker and I am not really familiar with all of it's functionalities. – A. Khaled Apr 16 '20 at 16:12
  • Ok. are you using Docker Desktop or Docker toolbox ? – Eklavya Apr 16 '20 at 16:13
  • I'm using Docker Desktop – A. Khaled Apr 16 '20 at 16:15
  • 1
    Try this to get IP https://stackoverflow.com/questions/58073936/how-to-get-ip-address-of-docker-desktop-vm – Eklavya Apr 16 '20 at 16:17
  • And try with that ip rather 127.0.0.1 for mysql – Eklavya Apr 16 '20 at 16:19
  • I replaced 127.0.0.1 with 192.168.1.3 (which was added by Docker Desktop) and I was successfully able to connect to the MySQL server from the Java container, but why does this happen? in other words, why is 127.0.0.1 outside the scope of the Java docker container? – A. Khaled Apr 16 '20 at 16:31
  • Your java container is running in VM not in your machine, and VM is running inside your machine, so it does know localhost. Find more details here https://stackoverflow.com/questions/42866013/docker-toolbox-localhost-not-working – Eklavya Apr 16 '20 at 16:58
  • Are you clear now ? – Eklavya Apr 16 '20 at 17:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211830/discussion-between-a-khaled-and-abinash-ghosh). – A. Khaled Apr 16 '20 at 19:30

0 Answers0