This commit is contained in:
Lihan 2024-07-27 15:33:16 -04:00
parent d0bfd8b78e
commit c556ebc308
2 changed files with 22 additions and 14 deletions

View file

@ -1,14 +1,23 @@
# Use an official OpenJDK runtime as a parent image
FROM openjdk:11
# Set the working directory inside the container
# First stage: Build the application using Maven
FROM maven:3.8.1-openjdk-17 AS build
WORKDIR /app
# Copy the built JAR file from the build context
COPY target/*.jar app.jar
# Copy the Maven wrapper and the pom.xml file
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
# Copy the project source code
COPY src ./src
# Package the application
RUN ./mvnw package
# Second stage: Use an official OpenJDK runtime as a parent image
FROM openjdk
WORKDIR /app
# Copy the JAR file from the build stage
COPY --from=build /app/target/*.jar app.jar
# Run the jar file
CMD ["java", "-jar", "app.jar"]
# Expose the port the app runs on
EXPOSE 8080
CMD ["java", "-jar", "app"]

7
Jenkinsfile vendored
View file

@ -21,8 +21,6 @@ pipeline {
steps {
script {
echo "Building Docker Image..."
// Ensure Maven build is executed
sh './mvnw clean package -DskipTests'
def dockerImage = docker.build("spring-petclinic")
echo "Docker Image built: ${dockerImage.id}"
// Store the Docker image ID in the environment if needed across stages
@ -31,16 +29,17 @@ pipeline {
}
}
// Further stages would reference env.DOCKER_IMAGE_ID if needed
}
post {
always {
script {
// Use the saved Docker image ID from the environment if needed
if (env.DOCKER_IMAGE_ID) {
echo "Stopping and removing Docker Image with ID: ${env.DOCKER_IMAGE_ID}"
// Using shell command to remove Docker image
sh "docker rmi ${env.DOCKER_IMAGE_ID}"
docker.rmi(env.DOCKER_IMAGE_ID)
}
}
}