0

I have a Springboot application which connects to MySQL database. When I try to run the application it gives following exceptions :

- 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.
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
- java.net.ConnectException: Connection refused (Connection refused)

My Dockerfile is this :

FROM openjdk:8
ADD target/studyapp.jar studyapp.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "studyapp.jar"]

My application.properties :

spring.datasource.url=jdbc:mysql://localhost:3306/study # ${MYSQL_HOST:localhost}
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=false
spring.jpa.database=mysql
spring.hibernate.dialect=org.hibernate.dialect.MySQLDialect
security.basic.enable: false
security.ignored=/**

This is the command that I used for running mysql container :

sudo docker run --net studyapp-network --name localhost -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=study -e MYSQL_USER=cnyt -e MYSQL_PASSWORD=root -d mysql:5.7.32

And this is for running the app container :

sudo docker run --net studyapp-network -p 8080:8080 cuneytyvz/studyapp

I'm using 'localhost' as name for my mysql container. I tried to change this, same error occured.

I also can run this app using docker-compose with no problem. But I want to see it working without docker-compose also. What might be the problem?

This is the working docker-compose file :

version: '3.3'

services:
  #service 1: definition of mysql database
  mysql-db:
    image: mysql:latest
    container_name: mysql-db
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_USER=cnyt
    ports:
      - "3306:3306"
    restart: always
    volumes:
      - my-datavolume:/var/lib/mysql


  #service 2: definition of phpMyAdmin
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    container_name: my-php-myadmin
    ports:
      - "8082:80"
    restart: always

    depends_on:
      - mysql-db
    environment:
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root



  #service 3: definition of your spring-boot app
  studyapp:                        #it is just a name, which will be used only in this file.
    image: studyapp               #name of the image after dockerfile executes
    container_name: studyapp  #name of the container created from docker image
    build:
      context: .                          #docker file path (. means root directory)
      dockerfile: Dockerfile              #docker file name
    ports:
      - "8080:8080"                       #docker containter port with your os port
    restart: always

    depends_on:                           #define dependencies of this app
      - mysql-db                                #dependency name (which is defined with this name 'db' in this file earlier)
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db:3306/study?createDatabaseIfNotExist=true
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root

volumes:
  my-datavolume:
cuneyttyler
  • 1,255
  • 2
  • 17
  • 27
  • 1
    In your final `docker run` command, the `--net` option is after the image name, so it becomes the command to run. You need to move it before the image name if you want it interpreted as a `docker` option. – David Maze Mar 27 '21 at 15:17
  • @DavidMaze gave you a point and also check these https://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – Sachith Muhandiram Mar 27 '21 at 15:19
  • @DavidMaze I tried moving --net before --name and nothing has changed. – cuneyttyler Mar 27 '21 at 15:43
  • 1
    @SachithMuhandiram I checked the link but I only have this problem when I try to run the app via Docker. If I run my app through IDE or docker-compose it connects to db and runs without any problem. – cuneyttyler Mar 27 '21 at 15:43
  • @cuneytyvz can you post working `docker-compose` file? – Sachith Muhandiram Mar 27 '21 at 15:57
  • I'm not completely sure but you should change `spring.datasource.url=jdbc:mysql://localhost:3306/study` with `spring.datasource.url=jdbc:mysql://localhost.studyapp-network:3306/study`. – Eugenio Carocci Mar 27 '21 at 22:57
  • @SachithMuhandiram I added the docker-compose file. – cuneyttyler Mar 28 '21 at 10:15
  • @EugenioCarocci I tried but it didn't work. – cuneyttyler Mar 28 '21 at 10:16

1 Answers1

1

It's because I was using 'localhost' as name for my mysql container. It probable confuses my application docker container. I changed it and it works correctly.

cuneyttyler
  • 1,255
  • 2
  • 17
  • 27