Creating a Docker image using Maven

If you have used Maven to resolve dependencies but the Maven provides support to create a build by adding some plugins to the Maven configuration file pom.xml. So, if you want to create a Docker image by using the Maven commands, you have to add a new plugin in the Maven pom.xml file. Consider the following:

<properties> 
   ... 
   <docker.image.prefix>doj</docker.image.prefix> 
</properties> 
 
<build> 
   <plugins> 
         <plugin> 
               <groupId>com.spotify</groupId> 
               <artifactId>dockerfile-maven-plugin</artifactId> 
               <version>1.3.4</version> 
               <configuration> 
                     <repository>${docker.image.prefix}/${project.artifactId}</repository> 
                     <buildArgs> 
                           <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> 
                     </buildArgs> 
               </configuration> 
         </plugin> 
         ... 
   </plugins> 
</build>  
 

As you can see in the preceding Maven pom.xml file, we have configured a new plugin with groupId com.spotify and artifactId dockerfile-maven-plugin with the following two configurations:

  • The repository with the image name, which will end up here as doj/account-account-service
  • The name of the JAR file, exposing the maven configuration as a build argument for Docker

Now you can use the following Maven command to build a Docker image:

$ ./mvnw install dockerfile:build

You can also push this Docker image to Docker Hub using the following command:

./mvnw dockerfile:push

Similarly, you can use Gradle to build a Docker image using the Gradle build command.

Now let's move on and discuss Docker Compose in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.244.217