Building Docker Image with Maven
You are a DevOps Engineer working for ABC Infotech. A client has requested you to build a Docker image using Maven plugin, so that when you package your Maven project. Docker image will be built, and the generated artifacts will be deployed in local git repository
1. Create a Maven java project using CLI with the following configurations
- groupId=javaApp
- artifactId maven-docker-image
2. Replace the 'Hello World!' message in the 'App.java' file with 'Hurray! You have successfully built a docker image using Maven'
3. Configure your 'pom.xml' to deploy the generated artifacts in the local git repository to build a docker image
4. The project should be deployed in the local repositary with the following details:
- id = maven.repo
- name = Maven Artifacts
- URL = file:///home/labuser/Desktop/Remote/mavenArtifacts
5. The name of the jar file, repository name af the image should be same as the project artifactId, the tag of the image sould be same as the project version.
6. Create a Dockerfile to build an OpenJDK image using the artifact generated
Note: The Dockerfile should be created inside the path '-/Desktop/Project/wingst5-august2022-mediumchallenge3/maven-docker-image/'
Solution
Step 1: Create a Maven Java project
Open your command prompt or terminal and execute the following command to create a Maven Java project:
mvn archetype:generate -DgroupId=javaApp -DartifactId=maven-docker-image -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This will create a new directory called maven-docker-image with the Maven project structure and default files.
Step 2: Replace 'Hello World!' message
Navigate to the project directory:
cd maven-docker-image
Open the App.java file located in the src/main/java/javaApp directory and replace the line that says System.out.println("Hello World!"); with the following:
System.out.println("Hurray! You have successfully built a docker image using Maven");
Save and close the file.
Step 3: Configure pom.xml for Docker image deployment
Open the pom.xml file in the project's root directory and add the following plugins and configurations inside the <build> section:
<build>
<plugins>
<!-- Maven Docker Plugin -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.37.0</version>
<configuration>
<images>
<image>
<name>${project.artifactId}</name>
<build>
<tags>
<tag>${project.version}</tag>
</tags>
<dockerFileDir>${project.basedir}</dockerFileDir>
</build>
</image>
</images>
</configuration>
</plugin>
<!-- Maven Git Commit ID Plugin -->
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.0.2</version>
</plugin>
</plugins>
</build>
This configuration adds the Docker Maven Plugin and the Git Commit ID Plugin. The Docker Maven Plugin is used to build the Docker image, and the Git Commit ID Plugin provides information about the Git commit in the project.
Step 4: Configure the local Git repository
Add the following configuration inside the <distributionManagement> section of the pom.xml file:
<distributionManagement>
<repository>
<id>maven.repo</id>
<name>Maven Artifacts</name>
<url>file:///home/labuser/Desktop/Remote/mavenArtifacts</url>
</repository>
</distributionManagement>
This configuration adds a local repository with the specified ID, name, and URL.
local git repo should be already configured having '.git' file if not initialize it using
git init
The main difference between the two elements is that the
repositories
element is used for downloading dependencies, while the distributionManagement
element is used for deploying artifacts.Step 5: Create a Dockerfile
Create a new file named Dockerfile in the project's root directory and add the following content:
FROM openjdk:11-jre-slim
WORKDIR /app
COPY target/${project.artifactId}-${project.version}.jar /app/app.jar
CMD ["java", "-jar", "app.jar"]
This Dockerfile uses the OpenJDK 11 slim image as the base image, sets the working directory to /app, copies the generated JAR file to the container, and defines the command to run the JAR file.
Step 6: Build the Docker image
To build the Docker image and deploy the artifacts to the local Git repository, execute the following command:
mvn clean package docker:build deploy
This command will compile the code, run tests, package
---THANK YOU---
Comments
Post a Comment