Skip to content

Commit

Permalink
update build application guide
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-gilber committed Oct 27, 2023
1 parent 858abe9 commit b044de3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
11 changes: 11 additions & 0 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -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"]
11 changes: 6 additions & 5 deletions Containerfile.multistage
Original file line number Diff line number Diff line change
@@ -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"]
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<!-- pom.xml -->
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 39 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<description>Backend application </description>
<properties>
<java.version>17</java.version>
<!-- Dockerfile Build Plugin: https://github.com/spotify/dockerfile-maven -->
<dockerfile-maven-version>1.4.13</dockerfile-maven-version>
<docker.registry.url></docker.registry.url>
</properties>
<dependencies>
<!-- Added by Paul Gilber (Start) -->
Expand Down Expand Up @@ -51,7 +54,42 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Dockerfile Build Plugin (tag: ${project.version}): https://github.com/spotify/dockerfile-maven -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<!-- Build Container Image -->
<execution>
<id>default</id>
<!-- https://github.com/spotify/dockerfile-maven/blob/master/docs/usage.md#maven-goals -->
<goals>
<goal>build</goal>
</goals>
</execution>
<!-- Create Container Image `latest` tag -->
<execution>
<id>latest-tag</id>
<goals>
<goal>tag</goal>
</goals>
<configuration>
<repository>${docker.registry.url}${project.artifactId}</repository>
<tag>latest</tag>
</configuration>
</execution>
</executions>
<configuration>
<dockerfile>Containerfile</dockerfile>
<repository>${docker.registry.url}${project.artifactId}</repository>
<tag>${project.version}</tag>
<contextDirectory>.</contextDirectory>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>

</project>

0 comments on commit b044de3

Please sign in to comment.