skip to Main Content

When i use docker compose up for building all service and run when building stage backend(spring-boot) cannot connect MySQL database it cannot build and run image for backend.

I try to set all service is in same network but it not working

version: '3'
services:
  database:
    build:
      context: ./database
      dockerfile: database.Dockerfile
    environment:
      - MYSQL_ROOT_PASSWORD=mysql@sit
    volumes:
      - ./database/mysql-lib:/var/lib/mysql
      # - ./database/my.cnf:/etc/my.cnf
      # - ./database/setup/:/docker-entrypoint-initdb.d/
    restart: on-failure
    networks:
    - test-networks
  backend:
    depends_on:
      - database
      
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      - MYSQL_USER=root
      - MYSQL_PASSWORD=mysql@sit
      - MYSQL_URL=jdbc:mysql://database:3306/task_base?serverTimezone=UTC
    networks:
      - test-networks

networks:
  test-networks:
    driver: bridge

Here is my Dockerfile (backend)

FROM maven:3.8.3-openjdk-17
COPY . /backendAPI
WORKDIR /backendAPI
RUN mvn clean package
ENTRYPOINT ["java","-jar","/backendAPI/target/*.jar"]

dockerfile for database

FROM mysql/mysql-server:latest
COPY ./my.cnf /etc/
VOLUME ./mysql-lib /var/lib/mysql
COPY ./setup/db-script-v2.sql /docker-entrypoint-initdb.d/
ENV  MYSQL_ROOT_PASSWORD=mysql@sit

My project structure

ITB-KK-Backend
├─ .mvn
│  └─ wrapper
│     ├─ maven-wrapper.jar
│     └─ maven-wrapper.properties
├─ database
│  ├─ database.Dockerfile
│  ├─ my.cnf
│  └─ setup
│     └─ db-script-v2.sql
├─ docker-compose.yaml
├─ Dockerfile
├─ mvnw
├─ mvnw.cmd
├─ pom.xml
└─ src
   ├─ main

My application.properties (spring-boot config file)

spring.application.name=ITB-KK
spring.datasource.username={MYSQL_USER:root}
spring.datasource.password={MYSQL_PASSWORD:123456}
spring.datasource.url=jdbc:mysql://${MYSQL_URL:localhost}:3306/task_base?serverTimezone=UTC
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.main.banner-mode=off
server.servlet.context-path=/itb-kk
#spring.jackson.time-zone=UTC
#spring.jpa.properties.hibernate.jdbc.time_zone=UTC
server.error.include-stacktrace=never

but when i run compose up it error

309.6 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
309.6
309.6 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
309.6   at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-j-8.3.0.jar:8.3.0]
309.6   at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-j-8.3.0.jar:8.3.0]
309.6   at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:815) ~[mysql-connector-j-8.3.0.jar:8.3.0]
309.6   at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:438) ~[mysql-connector-j-8.3.0.jar:8.3.0]
309.6   at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-j-8.3.0.jar:8.3.0]
309.6   at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:189) ~[mysql-connector-j-8.3.0.jar:8.3.0]
309.6   at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-5.0.1.jar:na]
309.6   at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359) ~[HikariCP-5.0.1.jar:na]
309.6   at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-5.0.1.jar:na]
309.6   at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470) ~[HikariCP-5.0.1.jar:na]
309.6   at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-5.0.1.jar:na]
309.6   at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100) ~[HikariCP-5.0.1.jar:na]
309.6   at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-5.0.1.jar:na]
309.6   at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]  
309.6   at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:428) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
309.6   at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:61) ~[hibernate-core-6.4.4.Final.jar:6

I want can build docker image for backend successfully i dont know why it cannot

2

Answers


  1. Update your docker-compose.yml to something like this:

    version: '3'
    services:
      database:
        build:
          context: ./database
          dockerfile: database.Dockerfile
        environment:
          - MYSQL_ROOT_PASSWORD=mysql@sit
        volumes:
          - ./database/mysql-lib:/var/lib/mysql
        restart: on-failure
        networks:
        - test-networks
        healthcheck:
          test: ["CMD", "mysqladmin", "ping"]
          interval: 5s
          timeout: 5s
          retries: 5
    
      backend:
        depends_on:
          database:
            condition: service_healthy
        build:
          context: .
          dockerfile: Dockerfile
        ports:
          - "8080:8080"
        environment:
          - MYSQL_USER=root
          - MYSQL_PASSWORD=mysql@sit
          - MYSQL_URL=jdbc:mysql://database:3306/task_base?serverTimezone=UTC
        networks:
          - test-networks
    
    networks:
      test-networks:
        driver: bridge
    

    Important changes:

    • Add healthcheck to the database service. The service will only be marked as "healthy" once mysqladmin is able to get a successful ping, which indicates that the database is ready.
    • Update the depends_on for the backend service to depend on the condition that the database service has been marked as "healthy".
    Login or Signup to reply.
  2. After looking at your repository, https://github.com/22p21s0045/Fix-spring-deploy.git, it became apparent that the error is indeed happening at build time. The question is a little misleading because it mentions that the error occurs "when i run compose up", which suggests that it is a runtime error.

    With additional context it appears that the error is being caused by the tests in src/test/java/sit/int221/ItbKkApplicationTests.java. Those tests appear to be trying to connect to a database and the way that this is currently set up there will be no database for them to connect to.

    You have a few options including:

    1. Delete or disable the tests (temporarily) just to get it to build. I removed src/test/java/sit/int221/ItbKkApplicationTests.java and could then run docker-compose build && docker-compose up.
    2. Ensure that there is a database available at build time for the tests to use.

    Here’s the additional context that indicates that it’s a testing error:

    13.13 [INFO] -------------------------------------------------------                                                                                                                                                                    
    13.13 [INFO]  T E S T S                                                                                                                                                                                                                 
    13.13 [INFO] -------------------------------------------------------                                                                                                                                                                    
    13.41 [INFO] Running sit.int221.ItbKkApplicationTests                                                                                                                                                                                   
    13.55 08:13:43.329 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [sit.int221.ItbKkApplicationTests]: ItbKkApplicationTests do
    es not declare any static, non-private, non-final, nested classes annotated with @Configuration.                                                                                                                                        
    13.55 08:13:43.378 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration sit.int221.ItbKkApplication for test class sit.int221.ItbKkApplicationTests                    
    13.65 08:13:43.448 [main] INFO org.springframework.boot.devtools.restart.RestartApplicationListener -- Restart disabled due to context in which it is running                                                                           
    13.75 2024-04-26T08:13:43.584Z  INFO 120 --- [ITB-KK] [           main] sit.int221.ItbKkApplicationTests         : Starting ItbKkApplicationTests using Java 17.0.1 with PID 120 (started by root in /backendAPI)                       
    13.75 2024-04-26T08:13:43.585Z  INFO 120 --- [ITB-KK] [           main] sit.int221.ItbKkApplicationTests         : No active profile set, falling back to 1 default profile: "default"                                                  
    14.15 2024-04-26T08:13:43.971Z  INFO 120 --- [ITB-KK] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.                                                          
    14.25 2024-04-26T08:13:44.001Z  INFO 120 --- [ITB-KK] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 26 ms. Found 1 JPA repository interface.                                 
    14.45 2024-04-26T08:13:44.271Z  INFO 120 --- [ITB-KK] [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]                                                            
    14.45 2024-04-26T08:13:44.294Z  INFO 120 --- [ITB-KK] [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.4.4.Final                                                                    
    14.55 2024-04-26T08:13:44.313Z  INFO 120 --- [ITB-KK] [           main] o.h.c.internal.RegionFactoryInitiator    : HHH000026: Second-level cache disabled                                                                               
    14.65 2024-04-26T08:13:44.416Z  INFO 120 --- [ITB-KK] [           main] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer                                                              
    14.65 2024-04-26T08:13:44.430Z  INFO 120 --- [ITB-KK] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...                                                                                           
    15.65 2024-04-26T08:13:45.490Z ERROR 120 --- [ITB-KK] [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.                                                                 
    15.65                                                                                                                                                                                                                                   
    15.65 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure                                                                                                                                                 
    15.65                                                                                                                                                                                                                                   
    15.65 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search