7
votes

I'm working with spring boot, hibernate & MySql. While running the application it is running well as per expectation . But while making the docker-compose file and running the app docker image with mysql docker image it gives this error.

Error com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure java.net.ConnectException: Connection refused.

private Connection createConnection() throws SQLException 
{
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        String mysqlUrl = "jdbc:mysql://localhost/database?autoReconnect=true&useSSL=false";
        Connection connection = DriverManager.getConnection(mysqlUrl, "root", "root");
        return connection;
}

Application.properties

spring.datasource.url=jdbc:mysql://localhost/database?autoReconnect=true&useSSL=false spring.datasource.username=root

spring.datasource.password=root

Please guide me how to tackle this.

**docker-compose.yml**

version: '3'

services:
  docker-mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=database
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
    ports:
      - 3307:3306

  app:
    image: app:latest
    ports:
       - 8091:8091
    depends_on:
       - docker-mysql
3
I guess that since the service is running inside a docker image localhost is actually that docker image. You probably have another docker images where the database is or not?Sergiu
you might have a look at this question stackoverflow.com/questions/44780571/…JAMSHAID
Here are a few reasons explained. you should check these out. javarevisited.blogspot.com/2013/02/…JAMSHAID
change this jdbc:mysql://localhost/database?autoReconnect=true&useSSL=false to jdbc:mysql://docker-mysql/database?autoReconnect=true&useSSL=false where docker-mysql is the name of the db service and docker embedded DNS will do the job of resolving service name to docker ip.Barath
@RaoWaqasAkram it may work in your machine where app & db runs on the same host ie localhost. But when you deploy as docker containers it defaults to bridge network. Technically you can achieve it using host network. There are tons of online materials may help you with better explanation. please read here spring boot + mysqlBarath

3 Answers

12
votes

Issue is due to reference of localhost in the jdbc url.

Below configuration should work.

**docker-compose.yml**

version: '3'

services:
  docker-mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=database
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
    ports:
      - 3307:3306

  app:
    image: app:latest
    ports:
       - 8091:8091
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://docker-mysql:3306/database?autoReconnect=true&useSSL=false
    depends_on:
       - docker-mysql
2
votes

This Docker forum discussion helped me when I was running into this error. For me it was a problem with cache and I didn't get the error after running docker-compose down --rmi all

0
votes

did you tell your docker that it depends on SQL? I mean is there something like this:

depends_on: 
 - mysql_server

in your docker-compose.yml ?

looks like a configuration issue.