diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..ffb892a --- /dev/null +++ b/Containerfile @@ -0,0 +1,11 @@ +# Build demoapp-backend using dockerfile-maven-plugin: https://github.com/spotify/dockerfile-maven + +ARG RUNTIME_IMAGE="registry.access.redhat.com/ubi8/openjdk-17-runtime" +FROM ${RUNTIME_IMAGE} +WORKDIR /app + +# Add the service itself +ARG JAR_FILE +COPY target/${JAR_FILE} demoapp-backend.jar + +CMD ["/usr/bin/java", "-jar", "/app/demoapp-backend.jar"] diff --git a/Containerfile.multistage b/Containerfile.multistage index df3dbce..efbbdc5 100644 --- a/Containerfile.multistage +++ b/Containerfile.multistage @@ -1,18 +1,19 @@ -# Build demoapp-backend using Multi-Stage Container Build +# Build demoapp-backend using Multi-Stage Container Build: https://docs.docker.com/build/building/multi-stage/ ARG BUILD_IMAGE="registry.access.redhat.com/ubi8/openjdk-17" ARG RUNTIME_IMAGE="registry.access.redhat.com/ubi8/openjdk-17-runtime" -ARG MAVEN_ARGS="-Dmaven.test.skip=true" # Build FROM ${BUILD_IMAGE} as build ARG MAVEN_ARGS +WORKDIR /build COPY src ./src COPY pom.xml ./pom.xml -RUN mvn "${MAVEN_ARGS[@]}" clean package +RUN mvn clean package -Dmaven.test.skip=true -Ddockerfile.skip # APP FROM ${RUNTIME_IMAGE} -COPY --from=build /home/jboss/target/demoapp-backend*.jar demoapp-backend.jar -CMD ["/usr/bin/java", "-jar", "demoapp-backend.jar"] +WORKDIR /app +COPY --from=build /build/target/demoapp-backend*.jar demoapp-backend.jar +CMD ["/usr/bin/java", "-jar", "/app/demoapp-backend.jar"] diff --git a/README.md b/README.md index 8c79ad6..c4d6a67 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ This project uses [Visual Studio Code Dev Containers](https://code.visualstudio. Visual Studio Code Dev Containers extension looks up [devcontainer.json](.devcontainer/devcontainer.json) file which defines the Container environment specification. -### Build Application JAR +### Build Application Container Image with Maven To ensure successful build of this project, `project.properties['java.version']` value from [pom.xml](pom.xml) must match with `features['ghcr.io/devcontainers/features/java:1'].version` from [devcontainer.json](.devcontainer/devcontainer.json) ```xml @@ -48,12 +48,14 @@ mvn clean install # Build and skip tests mvn clean install -Dmaven.test.skip=true -# Note: Visual Studio Dev Container creates and runs `mysql` service. To add other dependency services, update `.devcontainer/compose.yaml` +# Notes: +# - Visual Studio Dev Container creates and runs `mysql` service. To add other dependency services, update `.devcontainer/compose.yaml` +# - Maven was configured to create 2 image tags: [1] based on project.version [2] `latest` tag ``` -### Build Application Container with Multi-stage builds +### Build Application Container Image with Multi-stage builds [Multi-stage](https://docs.docker.com/build/building/multi-stage/) builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain. ```sh -docker build -f Containerfile.multistage -t demoapp-backend . +docker build -f Containerfile.multistage -t demoapp-backend:latest . ``` ## Run Application from Visual Studio Code Dev Container diff --git a/compose.yaml b/compose.yaml index 0d16362..cea757a 100644 --- a/compose.yaml +++ b/compose.yaml @@ -32,8 +32,10 @@ services: mysql: condition: service_healthy # healthy status is indicated by `healthcheck` keyword build: - context: . - dockerfile: Containerfile + image: demoapp-backend:latest # use built image + # Build image from Containerfile + # context: . + # dockerfile: Containerfile.multistage environment: # Externalized Spring Configuration: https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html SPRING_DATASOURCE_USERNAME: root diff --git a/pom.xml b/pom.xml index c1706a3..81c76c6 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,9 @@ Backend application 17 + + 1.4.13 + @@ -51,7 +54,42 @@ org.springframework.boot spring-boot-maven-plugin + + + com.spotify + dockerfile-maven-plugin + ${dockerfile-maven-version} + + + + default + + + build + + + + + latest-tag + + tag + + + ${docker.registry.url}${project.artifactId} + latest + + + + + Containerfile + ${docker.registry.url}${project.artifactId} + ${project.version} + . + + ${project.build.finalName}.jar + + + -