Skip to main content

Building Docker Image with Maven

 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

Popular posts from this blog

Kubernetes2

  Challenge 2 ConfigMaps: Step-1: To create a ConfigMap named 'fresco-config' with the key-value pair SERVER_URL= https://www.fresco.me , use the following command: kubectl create configmap fresco-config --from-literal=SERVER_URL=https://www.fresco.me To verify if the ConfigMap is created, use the following command: kubectl get configmap fresco-config Step-2: To create an nginx pod with the environmental variable SERVER_URL_ENV using the ConfigMap created earlier, use the following YAML template: apiVersion: v1 kind: Pod metadata: name: fresco-nginx-pod spec: containers: - name: fresco-nginx-container image: nginx env: - name: SERVER_URL_ENV valueFrom: configMapKeyRef: name: fresco-config key: SERVER_URL Deploy the above file you can name it deployment.yaml and use the command: kubectl apply -f deployment.yaml To test your configuration by executing the following command:

Kubernetes1

  Challenge 1 Welcome to the Kubernetes challenge, your task is to follow the below steps and complete them successfully. Environment Setup Check whether docker & minikube are properly installed and configured. Start Minikube and execute this command to sync host docker with minikube docker minikube -p minikube docker-env and eval $(minikube docker-env) Step-1 Create a pod object using kubectl run command with google's sample image: gcr.io/google-samples/kubernetes-bootcamp:v1 and expose it on port 8080, name the pod as firstapp. Check if the pod creation is successful by running the command: kubectl get pod firstapp Step-2 Expose the application to the local VM by creating a Service object of type NodePort. Check if the service is created by running the command: kubectl get svc firstapp Step-3 Create another deployment using a 'YAML' file, create a deployment.yaml file that contains information of the number of replicas and the images to be

Docker + Docker Compose + Ansible

 Docker + Docker Compose + Ansible ----------------------------------------------------------------------------------------------------------------------------- Flask Application Deployment using Ansible roles and Docker-Compose 1. Run the "setup.sh" file given in the path to install the required dependencies before starting the challenge. 2. A Flask application named "app.py" is given in the path . 3. Create an Ansible role named "Installation" in the path "/etc/ansible/roles" to install docker-compose. 4. Write an Ansible playbook named "creation.yaml" in the path , to perform the following tasks: 1. Using "file" and "copy" modules, create a Multi-stage "Dockerfile" in the path to  - Dockerize the given Flask application with "python:alpine3.7" as its base image, using the given 'requirements.txt' file.  - Build an image using "postgres" as its base image. 2. Using &q