Skip to main content

Posts

Showing posts from October, 2021

Docker multi-stage build for Spring Boot application

  The post of this blog is to show the advantages of using multi-stage build on Docker for image size optimization. In case you've gotten here and never heard of the word "multi-stage", don't worry. Basically it is to build your image in different stages, and copy the artifacts from one stage to another, this allows you to keep the readability and maintenance of your Dockerfile, as well as reduce the size of the resulting image. For practice, let's use this Spring Boot learning project here , in which we use Maven to compile our project. Then let's not wait any longer, and let's get to work. Variant 1 This was the first variant that crossed my mind, basically it was, let me use a JDK base image to be able to compile the project, in this case openjdk:8, install Maven, compile the source code, and declare the entrypoint to the artifact binary .jar file. FROM openjdk:8 RUN apt-get update && apt-get install -y maven COPY . /usr/app WORKDIR