I am trying to dockerize my spring boot application & mysql database. When i try to docker-compose up
, the application giving me this error :
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
ebankapi-1 |
ebankapi-1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
And also when i try to seperately connect to my db container from mysql , it says :
Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
Here is my application.properties:
spring.application.name=e-bank
spring.datasource.url=jdbc:mysql://ebankdb:3307/e-bank
spring.datasource.username=projectserver
spring.datasource.password=projectserver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.open-in-view=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.sql.init.mode=always
logging.level.root=info
logging.level.org.springframework.web=DEBUG
And my Dockerfile :
FROM openjdk:17-slim
WORKDIR /app
COPY target/*.jar e-bank.jar
EXPOSE 8080
CMD [ "java", "-jar", "e-bank.jar" ]
And my docker-compose.yaml file :
version: "3.8" services: ebankapi: build: . restart: always ports: - '8080:8080' networks: - ebank-net environment: - spring.datasource.url=jdbc:mysql://ebankdb:3307/e-bank depends_on: - ebankdb volumes: - .m2:/root/.m2 ebankdb: image: "mysql:8.0" restart: always ports: - '3307:3306' networks: - ebank-net environment: MYSQL_DATABASE: e-bank MYSQL_USER: projectserver MYSQL_PASSWORD: projectserver MYSQL_ROOT_PASSWORD: root networks: ebank-net:
I tried to use ‘3307:3307’ but it did not work
I’ve been searching through questions to find a solution to my problem for one week. But it keep giving me this error. Please someone explain how to configure these files detailly like explaining to a stupid. I will make the working answer accepted immediately.
Thank you !