Search code examples
spring-bootdockerbuildpackjava-fontpaketo

How to add extra linux dependencies into a spring-boot buildpack image?


I updated my spring-boot application to use buildpacks to create my docker-image instead of a dockerfile. I also use Apache POI in my application and since that update I get an error when generating an xlsx file. After some digging, I think it happens because the fontconfig and/or ttf-dejavu packages are missing. But how do I add these in the dockerimage? With a dockerfile I would just add something like

RUN apt-get update && apt-get install fontconfig ttf-dejavu

But how do I achieve the same with buildpacks?


Solution

  • This answer assumes that by "... spring-boot application to use buildpacks" you mean the use of the spring-boot:build-image maven goal.

    The issue lays with the default builder (gcr.io/paketo-buildpacks/builder:base) used by the maven plugin. Builder is responsible for configuring the OS image, and the "base" builder doesn't include fontconfig package. .


    The easiest way to enable fontconfig package is to use the "full" builder (gcr.io/paketo-buildpacks/builder:full-cf or gcr.io/paketo-buildpacks/builder:latest); you can do so for example in one of the following ways:

    • by specifying the builder configuration parameter in the maven plugin,

      <project>
      <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <version>2.3.3.BUILD-SNAPSHOT</version>
                 <configuration>
                     <image>
                         <builder>gcr.io/paketo-buildpacks/builder:latest</builder>
                     </image>
                 </configuration>
             </plugin>
         </plugins>
      </build>
      </project>
      
    • or directly on your mvn command line by adding -Dspring-boot.build-image.builder=gcr.io/paketo-buildpacks/builder:latest.

    However, this is not ideal because the full OS image is much larger (approx. 1.45GB for "full" vs. 644MB for "base" - observed in docker image listing), a fair bit of overhead "just" for enabling fontconfig.


    A more involved approach would require creating a custom builder with custom mixins, in order to create a tailored "base" image with the extra packages. But I personally found it easier to just use the dockerfile approach in this scenario. Some articles on creating a custom builder: