<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Himanshu Pareek</title>
    <description>The latest articles on DEV Community by Himanshu Pareek (@himanshu-pareek).</description>
    <link>https://dev.to/himanshu-pareek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2042299%2Fe44264bf-b199-4d61-b178-15ab4f6558d9.JPG</url>
      <title>DEV Community: Himanshu Pareek</title>
      <link>https://dev.to/himanshu-pareek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/himanshu-pareek"/>
    <language>en</language>
    <item>
      <title>Creating Docker Image of Spring Boot Application using Buildpacks</title>
      <dc:creator>Himanshu Pareek</dc:creator>
      <pubDate>Sun, 08 Sep 2024 11:11:55 +0000</pubDate>
      <link>https://dev.to/himanshu-pareek/creating-docker-image-of-spring-boot-application-using-buildpacks-5dbo</link>
      <guid>https://dev.to/himanshu-pareek/creating-docker-image-of-spring-boot-application-using-buildpacks-5dbo</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;You have created a &lt;a href="https://spring.io/projects/spring-boot" rel="noopener noreferrer"&gt;Spring Boot&lt;/a&gt; application. It is working great on your local machine and now, you need to deploy the application somewhere else. On some platforms, you can directly submit the &lt;code&gt;jar&lt;/code&gt; file and it will be deployed. At some places, you can spin up a virtual machine, download the source code there, build it, and run it. But, most of the time you will need to deploy the application using containers. Most of the time, &lt;a href="https://www.docker.com/" rel="noopener noreferrer"&gt;Docker&lt;/a&gt; is used to build and run the image in a container. Also, when you upload the &lt;code&gt;jar&lt;/code&gt; file to some platforms, the application is run inside a container under the hood.&lt;/p&gt;

&lt;p&gt;So, in this blog, we will see &lt;code&gt;3&lt;/code&gt; different ways to build a Docker image for the given Spring Boot application. Let's start:&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Container Image
&lt;/h2&gt;

&lt;p&gt;The naive and insufficient way to build the Docker image for any application is to use a simple &lt;a href="https://docs.docker.com/get-started/docker-concepts/building-images/writing-a-dockerfile/" rel="noopener noreferrer"&gt;Dockerfile&lt;/a&gt; which copies the &lt;code&gt;jar&lt;/code&gt; file inside the image and run it using &lt;code&gt;java -jar&lt;/code&gt; command.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create Dockerfile
&lt;/h3&gt;

&lt;p&gt;Here is the &lt;code&gt;Dockerfile&lt;/code&gt; which you can put at the root of the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; eclipse-temurin:21-jre-ubi9-minimal&lt;/span&gt;

&lt;span class="k"&gt;ARG&lt;/span&gt;&lt;span class="s"&gt; JAR_FILE&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; ${JAR_FILE} application.jar&lt;/span&gt;

&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["java", "-jar", "/application.jar"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have specified one argument &lt;code&gt;JAR_FILE&lt;/code&gt; which is the location of the &lt;code&gt;jar&lt;/code&gt; file to use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building Docker Image
&lt;/h3&gt;

&lt;p&gt;After creating the above &lt;code&gt;Dockerfile&lt;/code&gt;, the below steps are used to create the Docker image:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Build the &lt;code&gt;jar&lt;/code&gt; file for the Spring Boot project:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew bootJar &lt;span class="c"&gt;# For Gradle build system&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./mvnw spring-boot:build-jar &lt;span class="c"&gt;# For Maven build system&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use the &lt;code&gt;Dockerfile&lt;/code&gt; to build the Docker image using the latest &lt;code&gt;jar&lt;/code&gt; file. In the below command replace the &lt;code&gt;{IMAGE_NAME}&lt;/code&gt; with the required image name and &lt;code&gt;{JAR_FILE}&lt;/code&gt; with the path to the generated &lt;code&gt;jar&lt;/code&gt; file. The image name contains a tag as well, like - &lt;code&gt;mycompany/product-service:0.0.1-SNAPSHOT&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;--build-arg&lt;/span&gt; &lt;span class="nv"&gt;JAR_FILE&lt;/span&gt;&lt;span class="o"&gt;={&lt;/span&gt;JAR_FILE&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="nt"&gt;--tag&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;IMAGE_NAME&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Verify if the Docker image is built using the following command. You should be able to see the image with the name specified in the command above:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker images
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Efficient Container Image using Layered Jar
&lt;/h2&gt;

&lt;p&gt;While it is possible and easy to package a Spring Boot uber &lt;code&gt;jar&lt;/code&gt; as a Docker image (as mentioned in the previous method), there are many downsides to copying and running the fat &lt;code&gt;jar&lt;/code&gt; as-is in the Docker image. For instance,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is some extra overhead when running uber &lt;code&gt;jar&lt;/code&gt; without unpacking it.&lt;/li&gt;
&lt;li&gt;Putting the application's code and all of its dependencies in a single layer is not optimal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since we compile our code more often than upgrading the Spring Boot version, it is better to separate things a bit more. If we put those &lt;code&gt;jar&lt;/code&gt; files (which are rarely changed) in the layer before the application layer, then Docker often needs to change only the bottom layer and can pick the rest from its cache.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enable Layered Jar
&lt;/h3&gt;

&lt;p&gt;To create a layered Docker image, we need to create a layered jar first. Nowadays, it is enabled by default in Gradle and Maven. You can enable or disable the layered jar behavior using the following setting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="c1"&gt;// build.gradle&lt;/span&gt;
&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;named&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"bootJar"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;layered&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;enabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// build.gradle.kts&lt;/span&gt;
&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;named&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BootJar&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"bootJar"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nf"&gt;layered&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;enabled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- pom.xml --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;project&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;build&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;plugins&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.springframework.boot&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;spring-boot-maven-plugin&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
               &lt;span class="nt"&gt;&amp;lt;layers&amp;gt;&lt;/span&gt;
                  &lt;span class="nt"&gt;&amp;lt;enabled&amp;gt;&lt;/span&gt;true&lt;span class="nt"&gt;&amp;lt;/enabled&amp;gt;&lt;/span&gt;
               &lt;span class="nt"&gt;&amp;lt;/layers&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/plugins&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/build&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/project&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can even tune how the layers are created. See the documentation for &lt;a href="https://docs.spring.io/spring-boot/gradle-plugin/packaging.html#packaging-executable.configuring.layered-archives" rel="noopener noreferrer"&gt;gradle&lt;/a&gt; or &lt;a href="https://docs.spring.io/spring-boot/maven-plugin/packaging.html#packaging.layers" rel="noopener noreferrer"&gt;maven&lt;/a&gt; configuration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create Dockerfile
&lt;/h3&gt;

&lt;p&gt;Below is the &lt;code&gt;Dockerfile&lt;/code&gt;, which can be used to take advantage of the layered &lt;code&gt;jar&lt;/code&gt; and to create a layered Docker image of the Spring Boot application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Perform the extraction in a separate builder container&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;eclipse-temurin:21-jre-ubi9-minimal&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;builder&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /builder&lt;/span&gt;

&lt;span class="c"&gt;# This points to the built jar file in the target folder&lt;/span&gt;
&lt;span class="c"&gt;# Adjust this to 'build/libs/*.jar' if you're using Gradle&lt;/span&gt;
&lt;span class="k"&gt;ARG&lt;/span&gt;&lt;span class="s"&gt; JAR_FILE=target/*.jar&lt;/span&gt;

&lt;span class="c"&gt;# Copy the jar file to the working directory and rename it to application.jar&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; ${JAR_FILE} application.jar&lt;/span&gt;

&lt;span class="c"&gt;# Extract the jar file using an efficient layout&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;java &lt;span class="nt"&gt;-Djarmode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;tools &lt;span class="nt"&gt;-jar&lt;/span&gt; application.jar extract &lt;span class="nt"&gt;--layers&lt;/span&gt; &lt;span class="nt"&gt;--destination&lt;/span&gt; extracted

&lt;span class="c"&gt;# This is the runtime container&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; eclipse-temurin:21-jre-ubi9-minimal&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /application&lt;/span&gt;

&lt;span class="c"&gt;# Copy the extracted jar contents from the builder container into the working directory in the runtime container&lt;/span&gt;
&lt;span class="c"&gt;# Every copy step creates a new docker layer&lt;/span&gt;
&lt;span class="c"&gt;# This allows docker to only pull the changes it really needs&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /builder/extracted/dependencies/ ./&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /builder/extracted/spring-boot-loader/ ./&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /builder/extracted/snapshot-dependencies/ ./&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /builder/extracted/application/ ./&lt;/span&gt;

&lt;span class="c"&gt;# Start the application jar - this is not the uber jar used by the builder&lt;/span&gt;
&lt;span class="c"&gt;# This jar only contains application code and references to the extracted jar files&lt;/span&gt;
&lt;span class="c"&gt;# This layout is efficient to start up and CDS friendly&lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["java", "-jar", "application.jar"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Building Docker Image
&lt;/h3&gt;

&lt;p&gt;The steps to build the layered Docker image are the same as building a basic Docker image. Please refer there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cloud Native Buildpacks
&lt;/h2&gt;

&lt;p&gt;What if I tell you that you can create a Docker image without creating a &lt;code&gt;Dockerfile&lt;/code&gt;? We can build docker images directly from the Gralde or Maven plugin using &lt;a href="https://buildpacks.io/" rel="noopener noreferrer"&gt;Cloud Native Buildpacks&lt;/a&gt;. Some platforms (like &lt;a href="https://www.heroku.com/" rel="noopener noreferrer"&gt;Heroku&lt;/a&gt; or &lt;a href="https://www.cloudfoundry.org/" rel="noopener noreferrer"&gt;Cloud Foundry&lt;/a&gt;) use Buildpacks to convert provided &lt;code&gt;jar&lt;/code&gt; files into runnable images.&lt;/p&gt;

&lt;p&gt;Spring Boot includes buildpack support directly for Maven and Gradle. We don't need to include any additional plugins. Just run the below command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew bootBuildImage &lt;span class="c"&gt;# For gradle build system&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./mvnw spring-boot:build-image &lt;span class="c"&gt;# For maven build system&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command generates an image with the default name &lt;code&gt;{PROJECT_NAME}:${PROJECT_VERSION}&lt;/code&gt;. If you want to configure the name of the generated image, you can follow the below steps:&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure image name for Gradle build system
&lt;/h3&gt;

&lt;p&gt;We can configure the &lt;code&gt;bootBuildImage&lt;/code&gt; task to set the name of the image, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// For build.gradle.kts&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;imagePrefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"javarush"&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;dockerImageName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"docker-example"&lt;/span&gt;
&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;named&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BootBuildImage&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"bootBuildImage"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;imageName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${imagePrefix}/${dockerImageName}:${version}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="c1"&gt;// For build.gradle&lt;/span&gt;
&lt;span class="kt"&gt;def&lt;/span&gt; &lt;span class="n"&gt;imagePrefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"javarush"&lt;/span&gt;
&lt;span class="kt"&gt;def&lt;/span&gt; &lt;span class="n"&gt;dockerImageName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"docker-example"&lt;/span&gt;
&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;named&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"bootBuildImage"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;imageName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"${imagePrefix}/${dockerImageName}:${version}"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure image name for Maven build system
&lt;/h3&gt;

&lt;p&gt;We can configure &lt;code&gt;spring-boot-maven-plugin&lt;/code&gt; to use another image name, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;properties&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;imagePrefix&amp;gt;&lt;/span&gt;javarush&lt;span class="nt"&gt;&amp;lt;/imagePrefix&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/properties&amp;gt;&lt;/span&gt;

...

&lt;span class="nt"&gt;&amp;lt;project&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;build&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;plugins&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.springframework.boot&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;spring-boot-maven-plugin&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;image&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;name&amp;gt;&lt;/span&gt;${imagePrefix}/${project.artifactId}:${project.version}&lt;span class="nt"&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;/image&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/plugins&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/build&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/project&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure image name while running the command
&lt;/h3&gt;

&lt;p&gt;We can even define the name of the image while running the command to build the image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew bootBuildImage &lt;span class="nt"&gt;--imageName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;javarush/docker-example:1.0.0 &lt;span class="c"&gt;# For grade build system&lt;/span&gt;

./mvnw spring-boot:build-image &lt;span class="nt"&gt;-Dspring-boot&lt;/span&gt;.build-image.imageName&lt;span class="o"&gt;=&lt;/span&gt;javarush/docker-example:1.0.0 &lt;span class="c"&gt;# For maven build system&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see the documentation to further configure &lt;a href="https://docs.spring.io/spring-boot/gradle-plugin/packaging-oci-image.html" rel="noopener noreferrer"&gt;Gradle&lt;/a&gt; or &lt;a href="https://docs.spring.io/spring-boot/maven-plugin/build-image.html#build-image" rel="noopener noreferrer"&gt;Maven&lt;/a&gt; plugin.&lt;br&gt;
This is my go-to method to create a Docker image for any Spring Boot application.&lt;/p&gt;
&lt;h2&gt;
  
  
  Running Docker Container
&lt;/h2&gt;

&lt;p&gt;Once you create a docker image, you need to make sure that it works as expected. After you make sure that the image is created, you can directly run it using the &lt;code&gt;docker run&lt;/code&gt; command. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"8080:8080"&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;IMAGE_NAME&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, this is not how images are used in production applications. &lt;a href="https://docs.docker.com/compose/" rel="noopener noreferrer"&gt;Docker Compose&lt;/a&gt; is used to run and manage multiple docker images.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog, we have seen how to build Docker images for Spring Boot applications using different methods. Being able to build docker images for your apps is a must skill to know because the image is what gets delivered. Thanks for reading the article till the end. I appreciate it. I will meet you in the next one. As always, all feedback and suggestions are welcome.&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>docker</category>
      <category>buildpack</category>
      <category>java</category>
    </item>
  </channel>
</rss>
