Skip to content

Commit 2c92f76

Browse files
Ashish MishraAshish Mishra
authored andcommitted
fixing the container spin up dependency issue wrt sql
1 parent 145225e commit 2c92f76

6 files changed

Lines changed: 33 additions & 10 deletions

File tree

Dockerfile

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1+
# Stage 1: Build the jar
12
FROM maven:3.8.5-openjdk-17 AS JARBUILDER
23

3-
WORKDIR build
4+
WORKDIR /build
45

56
COPY src src
67
COPY pom.xml pom.xml
78

89
RUN mvn clean package -DskipTests
910

10-
# above we are builing the project jar file and consuming it below to launch the application
11-
11+
# Stage 2: Run the app
1212
FROM bellsoft/liberica-openjre-alpine
1313

14-
WORKDIR springApplication/app
14+
WORKDIR /springApplication/app
15+
16+
# Copy the jar file from the builder stage
17+
COPY --from=JARBUILDER /build/target/*.jar app.jar
1518

16-
COPY --from=JARBUILDER build/target/*.jar app.jar
19+
# Copy wait-for.sh into the image
20+
COPY wait-for.sh .
1721

18-
CMD ["java", "-jar", "app.jar"]
22+
# Make the script executable
23+
RUN chmod +x wait-for.sh
1924

25+
# Use wait-for.sh as entrypoint
26+
ENTRYPOINT ["./wait-for.sh", "mysql-service", "java", "-jar", "app.jar"]

docker-compose.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717
# - "8082:3306" removing port mapping since by default the 3306 will expose the functionality
1818
volumes:
1919
- ./src/main/resources/dockerizedEntireApp/init-script:/docker-entrypoint-initdb.d
20-
- ./src/main/resources/dockerizedEntireApp/volumes/mysql_data:/var/lib/mysql
20+
# - ./src/main/resources/dockerizedEntireApp/volumes/mysql_data:/var/lib/mysql
2121
profiles:
2222
- entireApp
2323
networks:
@@ -34,7 +34,7 @@ services:
3434
- "8082:3306"
3535
volumes:
3636
- ./src/main/resources/dockerizedEntireApp/init-script:/docker-entrypoint-initdb.d
37-
- ./src/main/resources/dockerizedEntireApp/volumes/mysql_data:/var/lib/mysql
37+
# - ./src/main/resources/dockerizedEntireApp/volumes/mysql_data:/var/lib/mysql
3838
profiles:
3939
- onlyDb
4040
networks:
6 KB
Binary file not shown.
6 KB
Binary file not shown.

src/main/resources/application.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
spring.application.name=SpringWithDocker
22

3-
# Database connection (using dockerize postgress Db)
3+
# Database connection (using dockerize sql Db)
44
spring.datasource.url=jdbc:mysql://localhost:8082/userManagementDb?useSSL=false&allowPublicKeyRetrieval=true
55
spring.datasource.username=ashish
66
spring.datasource.password=ashish@123
@@ -11,7 +11,7 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1111
# JPA (Hibernate) Configuration
1212
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
1313
# Use `create` for fresh DB setup, `update` for incremental changes
14-
spring.jpa.hibernate.ddl-auto=update
14+
spring.jpa.hibernate.ddl-auto=none
1515
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
1616

1717

wait-for.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
# wait-for.sh
3+
4+
set -e
5+
6+
host="$1"
7+
shift
8+
cmd="$@"
9+
10+
until nc -z "$host" 3306; do
11+
>&2 echo "MySQL is unavailable - sleeping"
12+
sleep 2
13+
done
14+
15+
>&2 echo "MySQL is up - executing command"
16+
exec $cmd

0 commit comments

Comments
 (0)