skip to Main Content

I’m trying to run my spring boot application on docker with mysql.
Everything is working fine, MySQL DB starts successfully and after that spring application is deployed in container and I end up getting this error :

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

This is my docker compose yaml file content:

version: "3.8"

services:
  mysql-db:
    image: mysql
    container_name: mysqldb
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: employeedb
      MYSQL_HOST: mysql-db
      MYSQL_PORT: 3306
    networks:
      - mt-network

networks:
  mt-network:
    driver: bridge

And my application-properties.yml file content:

spring:
 datasource:
  password: root
  url: jdbc:mysql://mysql-db:3306/employeedb
  username: root
  driver-class-name: com.mysql.cj.jdbc.Driver
 jpa:
  hibernate:
   ddl-auto: update

I tried all ways to fix this but I’m still facing this issue. Please don’t put this question as Duplicate Question.

Thank you in advance.

2

Answers


  1. Chosen as BEST ANSWER

    There was something wrong with mysql container I guess it was not starting properly.

    So I added HealthCheck in docker compose and now after i run docker-compose up the mysql container starts and after that it stops for few second. After few seconds my spring application starts working without giving me error : Communication Link Failure.

    EDIT:

    Usually whats happening is that until MySQL container is not in healthy state it will re-run it as many times which is given in docker-compose yaml file. And when the container is healthy it executes Spring Application container.

    Below is my docker-compose.yml file,

        version: "3.8"
    
    services:
      spring-scon:
        container_name: scon
        build:
          context: ./
          dockerfile: Dockerfile
        ports:
          - 9090:8080
        depends_on: 
          mysqldb:
            condition: service_healthy
        networks:
          - spring-network
        restart: always
    
      mysqldb:
        image: mysql
        container_name: mysql
    
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: employeedb
          MYSQL_HOST: mysqldb
          MYSQL_PORT: 3306
    
        ports:
          - "127.0.1.1:3306:3306"
    
    
        healthcheck:
          test: ["CMD-SHELL", 'mysqladmin ping']
          interval: 10s
          timeout: 2s
          retries: 10
    
    
        volumes:
          - dbdata:/usr/app
    
        networks:
         - spring-network
    
    volumes:
      dbdata:
        external: true
    
    networks:
      spring-network:
        driver: bridge
    

    Here, I don't know about IP "127.0.1.1" but I read somewhere we can configure port number with IP like I did in my docker-compose.yml. If someone knows what that IP means please explain it.

    I use that IP to connect Dockerized MySQL Server with my local MySQL Workbench.

    Thanks Once Again.


  2. First, remove the whole network configuration, it’s unnecessary:

    version: "3.8"
    
    services:
      mysql-db:
        image: mysql
        container_name: mysqldb
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: employeedb
          MYSQL_HOST: mysql-db
          MYSQL_PORT: 3306
    

    You should use the container name mysqldb as hostname if your Spring Boot application is started with Docker Compose as well:

    spring:
     datasource:
      url: jdbc:mysql://mysqldb:3306/employeedb
    

    The documentation states:

    By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

    If you start your Spring Boot app from your IDE, you need to use localhost as hostname because the app won’t be in the same network as the Docker container and won’t therefore not be able to resolve the hostname mysqldb:

    spring:
     datasource:
      url: jdbc:mysql://localhost:3306/employeedb
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search