mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-20 23:05:49 +00:00
Add dockerfiles, add compose, add build.gradle, modify postgres.properties, add .env
This commit is contained in:
parent
4850aacb51
commit
0619b72113
8 changed files with 160 additions and 25 deletions
6
.env
Normal file
6
.env
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
DB_USER=customUser
|
||||||
|
DB_PASSWORD=customPassword
|
||||||
|
DB_DATABASE=customDb
|
||||||
|
DB_CONTAINER_NAME=database
|
||||||
|
DB_URL=jdbc:postgresql://${DB_CONTAINER_NAME}/${DB_DATABASE}
|
||||||
|
|
46
Dockerfile
Normal file
46
Dockerfile
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
|
||||||
|
FROM eclipse-temurin:17-jdk-jammy as deps
|
||||||
|
WORKDIR /build
|
||||||
|
COPY --chmod=0755 mvnw mvnw
|
||||||
|
COPY .mvn/ .mvn/
|
||||||
|
RUN --mount=type=bind,source=pom.xml,target=pom.xml \
|
||||||
|
--mount=type=cache,target=/root/.m2 ./mvnw dependency:go-offline -DskipTests
|
||||||
|
|
||||||
|
FROM deps as package
|
||||||
|
WORKDIR /build
|
||||||
|
COPY ./src src/
|
||||||
|
RUN --mount=type=bind,source=pom.xml,target=pom.xml \
|
||||||
|
--mount=type=cache,target=/root/.m2 \
|
||||||
|
./mvnw package -DskipTests && \
|
||||||
|
mv target/$(./mvnw help:evaluate -Dexpression=project.artifactId -q -DforceStdout)-$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout).jar target/app.jar
|
||||||
|
|
||||||
|
FROM package as extract
|
||||||
|
WORKDIR /build
|
||||||
|
RUN java -Djarmode=layertools -jar target/app.jar extract --destination target/extracted
|
||||||
|
|
||||||
|
FROM extract as development
|
||||||
|
WORKDIR /build
|
||||||
|
RUN cp -r /build/target/extracted/dependencies/. ./
|
||||||
|
RUN cp -r /build/target/extracted/spring-boot-loader/. ./
|
||||||
|
RUN cp -r /build/target/extracted/snapshot-dependencies/. ./
|
||||||
|
RUN cp -r /build/target/extracted/application/. ./
|
||||||
|
ENV JAVA_TOOL_OPTIONS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000
|
||||||
|
CMD [ "java", "-Dspring.profiles.active=postgres", "org.springframework.boot.loader.launch.JarLauncher" ]
|
||||||
|
|
||||||
|
FROM eclipse-temurin:17-jre-jammy AS final
|
||||||
|
ARG UID=10001
|
||||||
|
RUN adduser \
|
||||||
|
--disabled-password \
|
||||||
|
--gecos "" \
|
||||||
|
--home "/nonexistent" \
|
||||||
|
--shell "/sbin/nologin" \
|
||||||
|
--no-create-home \
|
||||||
|
--uid "${UID}" \
|
||||||
|
appuser
|
||||||
|
USER appuser
|
||||||
|
COPY --from=extract build/target/extracted/dependencies/ ./
|
||||||
|
COPY --from=extract build/target/extracted/spring-boot-loader/ ./
|
||||||
|
COPY --from=extract build/target/extracted/snapshot-dependencies/ ./
|
||||||
|
COPY --from=extract build/target/extracted/application/ ./
|
||||||
|
EXPOSE 8080
|
||||||
|
ENTRYPOINT [ "java", "-Dspring.profiles.active=postgres", "org.springframework.boot.loader.launch.JarLauncher" ]
|
28
Dockerfile-multi_stage
Normal file
28
Dockerfile-multi_stage
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
FROM eclipse-temurin:17-jdk-jammy as build
|
||||||
|
WORKDIR /build
|
||||||
|
COPY --chmod=0755 mvnw mvnw
|
||||||
|
COPY .mvn/ .mvn/
|
||||||
|
COPY pom.xml .
|
||||||
|
RUN --mount=type=cache,target=/root/.m2 ./mvnw dependency:go-offline -DskipTests
|
||||||
|
|
||||||
|
COPY ./src src/
|
||||||
|
RUN --mount=type=cache,target=/root/.m2 \
|
||||||
|
./mvnw package -DskipTests && \
|
||||||
|
mv target/$(./mvnw help:evaluate -Dexpression=project.artifactId -q -DforceStdout)-$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout).jar target/app.jar
|
||||||
|
|
||||||
|
|
||||||
|
FROM eclipse-temurin:17-jre-jammy AS final
|
||||||
|
|
||||||
|
WORKDIR /src
|
||||||
|
|
||||||
|
COPY --from=build /build/target/app.jar .
|
||||||
|
|
||||||
|
RUN groupadd -r -g 2001 groupuser && useradd -r -m -d /home/myuser -u 2001 -g 2001 -s /sbin/nologin myuser
|
||||||
|
|
||||||
|
RUN chown myuser:groupuser .
|
||||||
|
|
||||||
|
USER myuser
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
ENTRYPOINT [ "java", "-jar", "/src/app.jar" ]
|
13
Dockerfile-single_stage
Normal file
13
Dockerfile-single_stage
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
FROM maven:3.9-eclipse-temurin-17
|
||||||
|
|
||||||
|
WORKDIR /src
|
||||||
|
|
||||||
|
COPY ./target/spring-petclinic-4.2.jar /src/app.jar
|
||||||
|
|
||||||
|
RUN groupadd -r -g 2001 groupuser && useradd -r -m -d /home/myuser -g 2001 myuser
|
||||||
|
|
||||||
|
RUN chown myuser:groupuser .
|
||||||
|
|
||||||
|
USER myuser
|
||||||
|
|
||||||
|
ENTRYPOINT [ "java", "-jar", "/src/app.jar" ]
|
31
build.gradle
31
build.gradle
|
@ -7,12 +7,15 @@ plugins {
|
||||||
id 'io.spring.javaformat' version '0.0.41'
|
id 'io.spring.javaformat' version '0.0.41'
|
||||||
id "io.spring.nohttp" version "0.0.11"
|
id "io.spring.nohttp" version "0.0.11"
|
||||||
id 'pl.allegro.tech.build.axion-release' version '1.18.0'
|
id 'pl.allegro.tech.build.axion-release' version '1.18.0'
|
||||||
|
id 'maven-publish'
|
||||||
|
id 'java-library'
|
||||||
}
|
}
|
||||||
|
|
||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
apply plugin: 'checkstyle'
|
apply plugin: 'checkstyle'
|
||||||
apply plugin: 'io.spring.javaformat'
|
apply plugin: 'io.spring.javaformat'
|
||||||
|
|
||||||
|
|
||||||
gradle.startParameter.excludedTaskNames += [ "checkFormatAot", "checkFormatAotTest" ]
|
gradle.startParameter.excludedTaskNames += [ "checkFormatAot", "checkFormatAotTest" ]
|
||||||
|
|
||||||
group = 'org.springframework.samples'
|
group = 'org.springframework.samples'
|
||||||
|
@ -27,6 +30,34 @@ repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
publishing {
|
||||||
|
publications {
|
||||||
|
mavenJava(MavenPublication) {
|
||||||
|
from components.java
|
||||||
|
groupId = 'com.example'
|
||||||
|
artifactId = 'myCustomLib'
|
||||||
|
version = '1.0.0'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
maven {
|
||||||
|
name = 'nexus'
|
||||||
|
url = uri('http://localhost:8081/repository/maven-local/')
|
||||||
|
allowInsecureProtocol = true
|
||||||
|
credentials {
|
||||||
|
username = 'admin'
|
||||||
|
password = 'admin'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
task localpublish {
|
||||||
|
dependsOn publish
|
||||||
|
description "publish to a local nexus repository instance"
|
||||||
|
}
|
||||||
|
|
||||||
ext.webjarsFontawesomeVersion = "4.7.0"
|
ext.webjarsFontawesomeVersion = "4.7.0"
|
||||||
ext.webjarsBootstrapVersion = "5.3.3"
|
ext.webjarsBootstrapVersion = "5.3.3"
|
||||||
|
|
||||||
|
|
34
compose.yaml
Normal file
34
compose.yaml
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
services:
|
||||||
|
database:
|
||||||
|
image: postgres
|
||||||
|
environment:
|
||||||
|
- POSTGRES_USER=${DB_USER:-petclinic}
|
||||||
|
- POSTGRES_DB=${DB_DATABASE:-petclinic}
|
||||||
|
- POSTGRES_PASSWORD=${DB_PASSWORD:-petclinic}
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready", "-d", "${DB_DATABASE:-petclinic}"]
|
||||||
|
interval: 10s
|
||||||
|
retries: 5
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql
|
||||||
|
|
||||||
|
app:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile-multi_stage
|
||||||
|
environment:
|
||||||
|
- POSTGRES_URL=${DB_URL:-petclinic}
|
||||||
|
- POSTGRES_USER=${DB_USER:-petclinic}
|
||||||
|
- POSTGRES_PASS=${DB_PASSWORD:-petclinic}
|
||||||
|
- DATABASE=postgres
|
||||||
|
- SPRING_PROFILES_ACTIVE=postgres
|
||||||
|
depends_on:
|
||||||
|
database:
|
||||||
|
condition: service_healthy
|
||||||
|
volumes:
|
||||||
|
- deps:/home/myuser/.m2
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
deps:
|
|
@ -1,25 +0,0 @@
|
||||||
services:
|
|
||||||
mysql:
|
|
||||||
image: mysql:8.4
|
|
||||||
ports:
|
|
||||||
- "3306:3306"
|
|
||||||
environment:
|
|
||||||
- MYSQL_ROOT_PASSWORD=
|
|
||||||
- MYSQL_ALLOW_EMPTY_PASSWORD=true
|
|
||||||
- MYSQL_USER=petclinic
|
|
||||||
- MYSQL_PASSWORD=petclinic
|
|
||||||
- MYSQL_DATABASE=petclinic
|
|
||||||
volumes:
|
|
||||||
- "./conf.d:/etc/mysql/conf.d:ro"
|
|
||||||
profiles:
|
|
||||||
- mysql
|
|
||||||
postgres:
|
|
||||||
image: postgres:16.3
|
|
||||||
ports:
|
|
||||||
- "5432:5432"
|
|
||||||
environment:
|
|
||||||
- POSTGRES_PASSWORD=petclinic
|
|
||||||
- POSTGRES_USER=petclinic
|
|
||||||
- POSTGRES_DB=petclinic
|
|
||||||
profiles:
|
|
||||||
- postgres
|
|
|
@ -4,3 +4,5 @@ spring.datasource.username=${POSTGRES_USER:petclinic}
|
||||||
spring.datasource.password=${POSTGRES_PASS:petclinic}
|
spring.datasource.password=${POSTGRES_PASS:petclinic}
|
||||||
# SQL is written to be idempotent so this is safe
|
# SQL is written to be idempotent so this is safe
|
||||||
spring.sql.init.mode=always
|
spring.sql.init.mode=always
|
||||||
|
logging.level=DEBUG
|
||||||
|
logging.level.org.springframework.web=DEBUG
|
||||||
|
|
Loading…
Reference in a new issue