0

I created a docker compose configuration file containing a simple MySQL container and a simple Springboot application container I created. Each one works fine standalone when executed outside a docker-compose. Here is my docker-compose yaml file:

version: '3'
services:
  mysql:
    image: mysql
    restart: always
    ports:
      - 3307:3306
    environment:
      - MYSQL_DATABASE=test
      - MYSQL_USER=jonathan
      - MYSQL_PASSWORD=secret
      - MYSQL_ROOT_PASSWORD=secret
    volumes:
      - mysql-data:/data/mysql
  my-spring-boot:
    image: my-spring-boot-app:1.0
    ports:
      - 8081:8080
    depends_on:
      - mysql

volumes:
  mysql-data:
    driver: local

Within the springboot application, the spring.datasource.url property has the value of "jdbc:mysql://mysql:3306/test".

When starting this network, the springboot application outputs:

2021-12-03 22:06:20.591 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] with root cause

java.net.ConnectException: Connection refused (Connection refused)

What am I doing wrong?

Jonathan T
  • 115
  • 7

1 Answers1

0

There is no one answer with the details that you provided.

First of all you need to troubleshoot the issue :

  • Check the health of containers

    docker-compose ps -a
    
  • Check the logs of DB

    docker-compose logs -f mysql
    
  • check resource usage

    docker stats
    

Since it's containers, the first intuitive action is to recreate your containers:

docker-compose down --remove-orphans

then

docker-compose up -d

If the issue persists, you should look into answers of this question & try it one by one.

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254