---
title: Build a Spring Boot app with a Dockerfile to deploy to Tanzu Application Platform
tags: ["Kubernetes", "Docker", "Tanzu", "TAP", "Spring Boot"]
categories: ["Dev", "CaaS", "Kubernetes", "TAP"]
date: 2023-08-01T10:26:25Z
updated: 2023-08-01T10:26:25Z
---

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.

```dockerfile
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.
