Skip to content

Commit 2d2bad1

Browse files
committed
Configured Spring Boot for open access, updated Docker and Mysql setup
1 parent b369417 commit 2d2bad1

26 files changed

+142
-8
lines changed

.dockerignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
target/
2+
.mvn/
3+
.idea/
4+
*.iml
5+
.classpath
6+
.project
7+
.settings/
8+
.git/
9+
node_modules/
10+
logs/
11+
*.log

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.compile.nullAnalysis.mode": "automatic"
3+
}

Docker-README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Docker: Build and run
2+
3+
This project is a Spring Boot application. The repository includes a multi-stage `Dockerfile` that builds the app with Maven and produces a runtime image. A `docker-compose.yml` is provided as an example to run the service together with a MySQL database.
4+
5+
Quick steps
6+
7+
Build the image with Docker (from repository root):
8+
9+
```bash
10+
docker build -t im-app:latest .
11+
```
12+
13+
Run the image:
14+
15+
```bash
16+
docker run --rm -p 8080:8080 \
17+
-e SPRING_DATASOURCE_URL='jdbc:mysql://<host>:3306/imdb' \
18+
-e SPRING_DATASOURCE_USERNAME=root \
19+
-e SPRING_DATASOURCE_PASSWORD=example \
20+
im-app:latest
21+
```
22+
23+
Use docker-compose for local development (builds image and starts MySQL):
24+
25+
```bash
26+
docker compose up --build
27+
```
28+
29+
Notes
30+
- The Dockerfile uses Maven to build and OpenJDK 21 runtime to run the jar.
31+
- The `docker-compose.yml` exposes MySQL on 3306 for convenience. In production you should not expose DB ports publicly and use secure credentials.
32+
- If you want Docker image layers to cache better during development, consider copying only `pom.xml` first and running `mvn dependency:go-offline` before copying source.

Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ---------- STAGE 1: Build ----------
2+
FROM maven:3.9.8-eclipse-temurin-21 AS builder
3+
WORKDIR /app
4+
5+
COPY pom.xml .
6+
RUN mvn dependency:go-offline
7+
8+
COPY src ./src
9+
RUN mvn clean package -DskipTests
10+
11+
# ---------- STAGE 2: Runtime ----------
12+
FROM eclipse-temurin:21-jdk
13+
WORKDIR /app
14+
15+
# Install netcat (for wait-for-it.sh)
16+
RUN apt-get update && apt-get install -y netcat-openbsd && rm -rf /var/lib/apt/lists/*
17+
18+
# Copy jar and wait script
19+
COPY --from=builder /app/target/*.jar app.jar
20+
COPY wait-for-it.sh .
21+
RUN chmod +x wait-for-it.sh
22+
23+
EXPOSE 8082
24+
25+
ENTRYPOINT ["java", "-jar", "app.jar"]

docker-compose.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
version: "3.9"
2+
3+
services:
4+
db:
5+
image: mysql:8.0
6+
container_name: mysql-db
7+
environment:
8+
MYSQL_ROOT_PASSWORD: Anjana@3176
9+
MYSQL_DATABASE: inventory_db
10+
# MYSQL_USER: root
11+
# MYSQL_PASSWORD: Anjana@3176
12+
#ports:
13+
# - "3306:3306"
14+
volumes:
15+
- mysql_data:/var/lib/mysql
16+
healthcheck:
17+
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-pAnjana@3176"]
18+
interval: 5s
19+
timeout: 10s
20+
retries: 10
21+
22+
app:
23+
build: .
24+
container_name: inventory-app
25+
depends_on:
26+
db:
27+
condition: service_healthy
28+
environment:
29+
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/inventory_db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
30+
SPRING_DATASOURCE_USERNAME: root
31+
SPRING_DATASOURCE_PASSWORD: Anjana@3176
32+
ports:
33+
- "8080:8082"
34+
command: ["./wait-for-it.sh", "db", "3306", "--", "java", "-jar", "app.jar"]
35+
36+
volumes:
37+
mysql_data:
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Database
2-
spring.datasource.url=jdbc:mysql://localhost:3306/inventory_db?allowPublicKeyRetrieval=true&useSSL=false
2+
spring.datasource.url=jdbc:mysql://db:3306/inventory_db?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
33
spring.datasource.username=root
44
spring.datasource.password=Anjana@3176
55

@@ -11,10 +11,3 @@ spring.jpa.properties.hibernate.format_sql=true
1111

1212
# Server
1313
server.port=8082
14-
15-
16-
# JWT
17-
#abhay
18-
# jwt.secret=your-secret-key
19-
# jwt.expiration=86400000 # 24 hours in milliseconds
20-
#lol
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Database
2+
spring.datasource.url=jdbc:mysql://db:3306/inventory_db?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
3+
spring.datasource.username=root
4+
spring.datasource.password=Anjana@3176
5+
6+
# JPA/Hibernate
7+
spring.jpa.hibernate.ddl-auto=update
8+
spring.jpa.show-sql=true
9+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
10+
spring.jpa.properties.hibernate.format_sql=true
11+
12+
# Server
13+
server.port=8082
727 Bytes
Binary file not shown.
5.97 KB
Binary file not shown.
3.08 KB
Binary file not shown.

0 commit comments

Comments
 (0)