Skip to content

Commit 9119522

Browse files
MayuriXxCopilotCopilot
authored
feat(docker_launch_app): add dockerfile for start the app (#75)
* feat(docker_launch_app): add dockerfile for start the app * Update Dockerfile Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * docs: translate README Docker section from French to English Agent-Logs-Url: https://github.com/XPEHO/spring_boot_java_random_user/sessions/aeb0a3ca-f19e-4a14-8249-f59f74e929c9 Co-authored-by: MayuriXx <26456981+MayuriXx@users.noreply.github.com> * Update README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: MayuriXx <26456981+MayuriXx@users.noreply.github.com>
1 parent 4c0bb39 commit 9119522

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

Dockerfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# ---- Stage 1: Build ----
2+
# Uses the full JDK image to compile the application with Maven
3+
FROM eclipse-temurin:25-jdk AS build
4+
WORKDIR /app
5+
6+
# Copy Maven wrapper and pom.xml first to leverage Docker layer caching
7+
# Dependencies are downloaded only when pom.xml changes
8+
COPY mvnw pom.xml ./
9+
COPY .mvn .mvn
10+
RUN chmod +x mvnw && ./mvnw dependency:go-offline -B
11+
12+
# Copy source code and build the JAR (tests are skipped as they run in CI)
13+
COPY src src
14+
RUN ./mvnw package -DskipTests -B
15+
16+
# ---- Stage 2: Run ----
17+
# Uses a lightweight JRE-only image for a smaller and more secure final image
18+
FROM eclipse-temurin:25-jre
19+
WORKDIR /app
20+
21+
# Create a non-root user and group for running the application securely
22+
RUN groupadd --system appgroup && useradd --system --gid appgroup appuser
23+
24+
# Copy the built JAR from the build stage using a stable pattern so version changes do not break the image build
25+
COPY --from=build /app/target/*.jar app.jar
26+
27+
# Ensure the non-root user owns the application files
28+
RUN chown -R appuser:appgroup /app
29+
30+
# Switch to the non-root user
31+
USER appuser
32+
33+
# Document the port the application listens on
34+
EXPOSE 8080
35+
36+
# Start the Spring Boot application
37+
ENTRYPOINT ["java", "-jar", "app.jar"]

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,69 @@ docker-compose up -d
4747

4848
---
4949

50+
## 🐳 Docker
51+
52+
### Architecture
53+
54+
The application can be fully containerized using a **multi-stage Dockerfile** and **Docker Compose**.
55+
56+
The `app` container connects to the `postgres` container over the `xpeho_network` bridge network using the PostgreSQL service name as the hostname.
57+
```
58+
┌─────────────────── xpeho_network (bridge) ───────────────────┐
59+
│ │
60+
│ ┌──────────┐ jdbc:postgresql:// ┌──────────┐ │
61+
│ │ app │ ──────── postgres:5432 ────────▶ │ postgres │ │
62+
│ │ :8080 │ (service name) │ :5432 │ │
63+
│ └──────────┘ └──────────┘ │
64+
│ │
65+
└───────────────────────────────────────────────────────────────┘
66+
```
67+
68+
| Service | Image / Build | Role | Exposed Port |
69+
|------------|------------------------------|----------------------------|---------------------------|
70+
| `postgres` | `postgres:17-alpine` | PostgreSQL database | `${POSTGRES_PORT}` → 5432 |
71+
| `app` | Built from `Dockerfile` | Spring Boot application | 8080 → 8080 |
72+
73+
### Dockerfile — Multi-stage Build
74+
75+
The Dockerfile uses two stages to produce a lightweight, secure final image:
76+
77+
| Stage | Image | Role |
78+
|-------|-------|------|
79+
| **Build** | `eclipse-temurin:25-jdk` | Compiles the JAR with Maven (full JDK) |
80+
| **Run** | `eclipse-temurin:25-jre` | Runs the application (lightweight JRE, non-root user) |
81+
82+
> **Why Eclipse Temurin?** Reference OpenJDK distribution: free, open-source, maintained by the Eclipse Foundation (Adoptium).
83+
84+
> **Security:** The final image runs as a non-root user (`appuser`), without source code or build tools.
85+
86+
### Compose Profiles
87+
88+
The `app` service is behind a **Compose profile** to avoid interfering with the dev/CI workflow:
89+
90+
```bash
91+
# Start PostgreSQL only (dev, tests, CI)
92+
docker compose up -d
93+
94+
# Start PostgreSQL + Application (full deployment)
95+
docker compose --profile app up -d --build
96+
```
97+
98+
### Useful Commands
99+
100+
```bash
101+
# View application logs
102+
docker compose logs -f app
103+
104+
# Stop and remove containers
105+
docker compose down
106+
107+
# Stop and remove containers + volumes (reset DB)
108+
docker compose down -v
109+
```
110+
111+
---
112+
50113
## ⚙️ Configuration
51114

52115
### Environment Variables (.env)
@@ -56,6 +119,11 @@ POSTGRES_USER=your_user
56119
POSTGRES_PASSWORD=your_password
57120
POSTGRES_DB=your_database
58121
POSTGRES_PORT=5432
122+
123+
# Liquibase (optional, defaults provided)
124+
LB_CHANGELOG=db/changelog/db.changelog-master.yaml
125+
LB_SCHEMA=public
126+
SPRING_LIQUIBASE_ENABLED=true
59127
```
60128

61129
### External API Configuration

docker-compose.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ services:
1818
networks:
1919
- xpeho_network
2020

21+
app:
22+
profiles:
23+
- app
24+
build:
25+
context: .
26+
dockerfile: Dockerfile
27+
container_name: xpeho_app
28+
environment:
29+
POSTGRES_USER: ${POSTGRES_USER}
30+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
31+
POSTGRES_DB: ${POSTGRES_DB}
32+
POSTGRES_PORT: 5432
33+
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/${POSTGRES_DB}
34+
SPRING_LIQUIBASE_ENABLED: ${SPRING_LIQUIBASE_ENABLED:-true}
35+
LB_CHANGELOG: ${LB_CHANGELOG:-db/changelog/db.changelog-master.yaml}
36+
LB_SCHEMA: ${LB_SCHEMA:-public}
37+
ports:
38+
- "8080:8080"
39+
depends_on:
40+
postgres:
41+
condition: service_healthy
42+
networks:
43+
- xpeho_network
44+
2145
volumes:
2246
postgres_data:
2347
driver: local

0 commit comments

Comments
 (0)