19

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
17
  • 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? Commented Nov 15, 2019 at 16:25
  • you might have a look at this question stackoverflow.com/questions/44780571/… Commented Nov 15, 2019 at 16:33
  • 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. Commented Nov 15, 2019 at 17:08
  • 1
    @Barath in application.properties or in createConnection() ? Commented Nov 15, 2019 at 17:10
  • 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 Commented Nov 15, 2019 at 17:36

7 Answers 7

28

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
Sign up to request clarification or add additional context in comments.

7 Comments

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?
Not needed as it can be passed as an env variable to override the property
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.
@RaoWaqasAkram if thats the case use network_mode: "host". reference here docker compose networking
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.
|
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

1 Comment

You save my day ! Thks
1

For me, it was the version of mysql

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

1 Comment

I follow many roads. I don´t know but since I changed the version this works.
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.

Comments

-1

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.

1 Comment

please have a look to updated question. I have updated the question and add the docker-compose.yml
-1

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}

Comments

-1

With error of author question

String mysqlUrl = "jdbc:mysql://localhost/database?autoReconnect=true&useSSL=false";

change localhost to name of container:  docker-mysql

String mysqlUrl = "jdbc:mysql://docker-mysql/database?autoReconnect=true&useSSL=false";

I don't have error above but I have error Communications link failure because I configure SPRING_DATASOURCE_URL failure

-e SPRING_DATASOURCE_USERNAME:root' \
-e SPRING_DATASOURCE_PASSWORD:Raisingthebar123!!/ \
-e SPRING_DATASOURCE_URL:'jdbc:mysql://docker-mysql:3306/database? \autoReconnect=true&useSSL=false'

Cause by: SPRING_DATASOURCE_USERNAME, SPRING_DATASOURCE_USERNAME is right but SPRING_DATASOURCE_URL fail because it have special & character

So I make a change NOT use : ALTERNATE use =

-e SPRING_DATASOURCE_USERNAME='root' `

-e SPRING_DATASOURCE_PASSWORD='Raisingthebar123!!/' `

-e SPRING_DATASOURCE_URL='jdbc:mysql://docker-mysql:3306/database?autoReconnect=true&useSSL=false'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.