14

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
user007
  • 2,156
  • 2
  • 20
  • 35
Lily
  • 605
  • 3
  • 15
  • 31
  • 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 Nov 15 '19 at 16:25
  • you might have a look at this question https://stackoverflow.com/questions/44780571/how-to-connect-with-mysql-db-running-as-container-in-docker – JAMSHAID Nov 15 '19 at 16:33
  • Here are a few reasons explained. you should check these out. https://javarevisited.blogspot.com/2013/02/java-net-ConnectException-Connection-refused.html – JAMSHAID Nov 15 '19 at 17:00
  • @JAMSHAID the first link share by you did't works for me sir. – Lily Nov 15 '19 at 17:02
  • @RaoWaqasAkram right. – JAMSHAID Nov 15 '19 at 17:06
  • did you tried changing the path in your connection? – JAMSHAID Nov 15 '19 at 17:07
  • Yeah . I have tried to do so.But failed – Lily Nov 15 '19 at 17:07
  • 1
    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 Nov 15 '19 at 17:08
  • 1
    @Barath in application.properties or in createConnection() ? – Lily Nov 15 '19 at 17:10
  • However you want. you can also pass it as an env variable ```SPRING_DATASOURCE_URL=jdbc:mysql://docker-mysql/database?autoReconnect=true&useSSL=false``` – Barath Nov 15 '19 at 17:10
  • @Barath will that work? is docker image running on docker-mysql? – JAMSHAID Nov 15 '19 at 17:11
  • Absolutely!! it should work – Barath Nov 15 '19 at 17:11
  • @RaoWaqasAkram try on both the places – JAMSHAID Nov 15 '19 at 17:11
  • right. I never dealt with that in docker but Swing and online applications. So was curious about the docker path – JAMSHAID Nov 15 '19 at 17:12
  • @Barath it works for me . Thank you for your suggestion. But i am confused about one thing that isn't possible that my spring app & image both works with the same code? – Lily Nov 15 '19 at 17:30
  • @RaoWaqasAkram there might be a possibility for that but it might take more time while running the application. put the connection variable in condition if it is null after attempting to connect with localhost, it should try out the docker connection. if it is not null, it'll simply ignore the docker part. The condition will increase the running time here. – JAMSHAID Nov 15 '19 at 17:36
  • 2
    @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 + mysql](https://www.javainuse.com/devOps/docker/docker-mysql) – Barath Nov 15 '19 at 17:36

6 Answers6

21

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
Barath
  • 5,093
  • 1
  • 17
  • 42
  • it works for me. But my question is that do we need to add the same URL in application.prop while building the docker image? – Lily Nov 15 '19 at 17:34
  • Not needed as it can be passed as an env variable to override the property – Barath Nov 15 '19 at 17:35
  • i want to run the application and docker image from the same code. How I am supposed to do so.As if we change the URL we are unable to run app. – Lily Nov 15 '19 at 17:39
  • @RaoWaqasAkram if thats the case use ```network_mode: "host"```. reference here [docker compose networking](https://docs.docker.com/compose/compose-file/#network_mode) – Barath Nov 15 '19 at 17:45
  • I'm also facing the same issue. It is working fine which build the image form ( Window Docker Desktop ). It's giving error while building the image on Linux Docker with same code base and Docker file. I'm worried. – Siva Karuppiah Aug 04 '21 at 05:08
  • @SivaKaruppiah It would be great if you could share some logs. – Barath Aug 04 '21 at 19:47
  • The issue has been identified. The issue was the connection string usessl=false parameter. working as expected once added this. – Siva Karuppiah Aug 06 '21 at 07:45
7

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

Barry Sweeney
  • 81
  • 2
  • 5
2

For me, it was the version of mysql

image: mysql:8 -> image: mysql:5.7

Mahdy H.
  • 181
  • 9
0

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.

inam
  • 1
  • please have a look to updated question. I have updated the question and add the docker-compose.yml – Lily Nov 15 '19 at 17:03
0

In my case I have provided proper privilege for my user to get connected from docker host and the error war gone.

I have provided % as host for my user which will provide access to connect from any host. The default root user will only connects from localhost.

0

Put something like this in the application.properties

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/test
spring.datasource.username=${MYSQL_USER:root}
spring.datasource.password=${MYSQL_PASSWORD:password}
Claudiu Haidu
  • 817
  • 4
  • 12
  • 24