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: