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
Update your
docker-compose.yml
to something like this:Important changes:
healthcheck
to thedatabase
service. The service will only be marked as "healthy" oncemysqladmin
is able to get a successful ping, which indicates that the database is ready.depends_on
for thebackend
service to depend on the condition that thedatabase
service has been marked as "healthy".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:
src/test/java/sit/int221/ItbKkApplicationTests.java
and could then rundocker-compose build && docker-compose up
.Here’s the additional context that indicates that it’s a testing error: