'communication link failure error when trying to run springboot container in docker

I want to run my springboot application inside a docker container with mysql but i got this error:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

I'm using docker-compose to configure and launch my 2 services (one for db and the other for the main application).

The mysql container works perfectly but the app container is stopping when trying to start it

Knowing that my application works fine in my machine (but not in docker)

Here is my Dockerfile file :

FROM openjdk:8
EXPOSE 8080
ADD target/mlr-it-consulting-repas-app.jar mlr-it-consulting-repas-app.jar
ENTRYPOINT ["java","-jar","/mlr-it-consulting-repas-app.jar"]

my application.properties file :

spring.datasource.url=jdbc:mysql://db1:3306/pjsavdb?createDatabaseIfNotExists=true&autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false&enabledTLSProtocols=TLSv1.2
spring.datasource.username=hamadi
spring.datasource.password=madi95
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update

and my docker-compose

version: "3.7"
services:
  db1:
    container_name: db1
    image: "mysql:8"
    restart: "no"
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: pjsavdb
      MYSQL_ALLOW_EMPTY_PASSWORD: true
    networks:
      - backend
  repas_app:
    build: .
    restart: "no"
    ports:
      - "8080:8080"
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://db1:3306/pjsavdb?createDatabaseIfNotExists=true&allowPublicKeyRetrieval=true&useSSL=false&enabledTLSProtocols=TLSv1.2
      SPRING_DATASOURCE_USERNAME: hamadi
      SPRING_DATASOURCE_PASSWORD: madi95
    networks:
      - backend
    depends_on:
      - db1
    command: sh -c './wait-for docker-mysql:3306 -- npm start'
networks:
  backend:
    external: true

I've been searching the net for days and testing multiple solutions but without result, maybe someone got an idea for my issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source