Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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
    image: app:latest
    ports:
       - 8091:8091
    depends_on:
       - docker-mysql
                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, 2019 at 16:25
                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, 2019 at 17:08
                @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
– Barath
                Nov 15, 2019 at 17:36
       - 8091:8091
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://docker-mysql:3306/database?autoReconnect=true&useSSL=false
    depends_on:
       - docker-mysql
                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, 2019 at 17:34
                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, 2019 at 17:39
                @RaoWaqasAkram if thats the case use network_mode: "host". reference here docker compose networking
– Barath
                Nov 15, 2019 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 4, 2021 at 5:08
                please have a look to updated question. I have updated the question and add the docker-compose.yml
– Lily
                Nov 15, 2019 at 17:03

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.

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}
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.