IK.AM

@making's tech note


Build a Spring Boot app with a Dockerfile to deploy to Tanzu Application Platform

🗃 {Dev/CaaS/Kubernetes/TAP}
🏷 Kubernetes 🏷 Docker 🏷 Tanzu 🏷 TAP 🏷 Spring Boot 
🗓 Updated at 2023-08-01T10:26:25Z  🗓 Created at 2023-08-01T10:26:25Z   🇯🇵 Original entry

⚠️ The content of this article is not supported by VMware. Any issues arising from the content of this article are your responsibility and please do not contact VMware Support.

Tanzu Application Platform uses Cloud Native Buildpacks by default when creating application container images. You can also use Docker to build Docker images.

Recommendations on how to write a Dockerfile for Spring Boot apps can be found in the following document.

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#container-images.dockerfiles

The Dockerfile described in the document assumes that the jar has already been built, but since there is only the source code on the Tanzu Application Platform, I need to build a jar file from source code when docker build. Also, runAsUser: 1000 is set at runtime, so switch to the user with uid 1000.

Based on the above, if you write a Dockerfile, it will be as follows.

FROM eclipse-temurin:17-jdk as builder
WORKDIR application
ADD ./.mvn .mvn/
ADD ./mvnw mvnw
ADD ./pom.xml pom.xml
ADD ./src src/
RUN ./mvnw -V clean package -DskipTests --no-transfer-progress && \
    cp target/*.jar application.jar && \
    java -Djarmode=layertools -jar application.jar extract

FROM eclipse-temurin:17-jre
ARG USERNAME=spring
ARG GROUPNAME=spring
ARG UID=1000
ARG GID=1000
WORKDIR application
RUN groupadd -g $GID $GROUPNAME && \
    useradd -m -s /bin/bash -u $UID -g $GID $USERNAME
USER $USERNAME
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

When deploying, if you specify --param dockerfile=<Dockerfile path on repository> as follows, a container image will be built using Dockerfile.

tanzu apps workload apply rest-service \
  --app rest-service \
  --git-repo https://github.com/making/rest-service \
  --git-branch main \
  --type web \
  --param dockerfile=./Dockerfile \
  -n demo \
  -y

Unlike when creating a container image with Paketo Buildpacks / Tanzu Buildpacks, the JVM memory size is not automatically set, so Note that you need to configure the JVM with the runtime environment variable JAVA_TOOL_OPTIONS if necessary.


✒️️ Edit  ⏰ History  🗑 Delete