Why the component of my backend can’t communications with database?

  Kiến thức lập trình

When I using the docker-compose.yml that build the backend component which that error

services:
  dbserver:
    container_name: dbserver
    image: mysql/mysql-server
    restart: always
    environment:
      MYSQL_DATABASE: 'integrated'
      MYSQL_ROOT_PASSWORD: 'mysql@sit'     
    ports:
      - '3306:3306'
    volumes:
      - mysqldb:/var/lib/mysql
      - mysqlconfig:/etc/my/
    networks:
      - test-networks

  frontend:
    build:
      context: ./integrated-1-frontend
      dockerfile: Dockerfile
    restart: always
    ports:
      - '3000:80'
    depends_on:
      - backend
    volumes:
      - ./integrated-1-backend/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - test-networks

  backend:
    build:
      context: ./integrated-1-backend
      dockerfile: Dockerfile
    restart: always
    ports:
      - '8080:8080'
    environment:
      MYSQL_PORT: 3306
      MYSQL_URL:  localhost
      MYSQL_USERNAME: root
      MYSQL_PASSWORD: mysql@sit
      SPRING_JPA.HIBERNATE: update
      port: 8080
    depends_on:
      - dbserver
    networks:
      - test-networks

      
volumes:
  mysqldb:
  mysqlconfig:

networks:
  test-networks:
    driver: bridge

This is a dockerfile of my backend

# Use Maven and OpenJDK 17 to build the application
FROM maven:3.8.5-openjdk-17 AS build
WORKDIR /integrated-1-backend/
COPY pom.xml .
COPY src ./src
RUN mvn clean package

# Use OpenJDK 17 to run the application
FROM openjdk:17-jdk-alpine
WORKDIR /integrated-1-backend/
COPY --from=build target/*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]

Here is a structure of myProject


sj-1-integrated-1
├
├─ docker-compose.yml
├─ integrated-1-backend
│  ├─ .mvn
│  │  └─ wrapper
│  │     ├─ maven-wrapper.jar
│  │     └─ maven-wrapper.properties
│  ├─ Dockerfile
│  ├─ mvnw
│  ├─ mvnw.cmd
│  ├─ pom.xml
│  ├─ src
│  │  ├─ main
│  │  │  ├─ java
│  │  │  │  └─ int221
│  │  │  │     └─ integrated1backend
│  │  │  │  
│  │  │  └─ resources
│  │  │     └─ application.properties

and the setting in application.propeties look be like this .

spring.application.name=integrated-1-backend
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=${MYSQL_USERNAME:root}
spring.datasource.password=${MYSQL_PASSWORD:mysql@sit}
spring.datasource.url= jdbc:mysql://${MYSQL_URL:ip23sj1.sit.kmutt.ac.th}:${MYSQL_PORT:3306}/integrated
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
server.error.include-stacktrace=never
server.port=${port:8080}
#spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.jackson.time-zone=UTC
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

I’ve tried many times but I’m stuck with the same problem. If some part of my config looks weird. It’s because I tried many things. Now I can’t sleep well.

the error is

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

I want it to work. And thank you all of you.

New contributor

Fernando Sucreno is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT