Skip to main content

Dockerize Gradle application using Jenkins

 Dockerize Gradle application using Jenkins


In this challenge we will build and deploy a application using docker and Jenkins

  1. A Gradle web project structure is given in the gradle webapp directory which is in the path mentioned below.
    dev/workspace/sample-project
  2. Initialize the gradle webapp directory as a gradle project and configure the build script to
    - generate a war file
    - build a docker image named "gradle_image"
    - run the docker image on a container named "gradle_container", expose to the port 8001
  3. Create a Dockerfile to deploy the generated war file in tomcat server
  4. Initialize git to make gradle webapp directory as your local git repository.
  5. Launch jenkins and create a declarative pipeline named gradle-build to run and deploy the gradle application.
  6. Ensure that your Gradle application is running on http://localhost:8001/gradle_webapp



Let's get started...

1. Initialize the Gradle project:
Open the command prompt/terminal in the dev/workspace/sample-project directory.
Run the command gradle init and select the option 2: application and then select 1: Java
Now, the Gradle project is initialized in the 'dev/workspace/sample-project directory'

2. Configure the build script:
Open the 'build.gradle' file in the 'dev/workspace/sample-project' directory.
Add the following code to generate a war file:

apply plugin: 'war'
war {
    baseName = 'gradle_webapp'
    version = '1.0'
}
plugins {
    id 'com.spotify.docker' version '1.2.0'
}
docker {
    name 'gradle_image'
    copySpec.from war
    buildArgs(['TOMCAT_VERSION': '9.0.0.M26'])
}
task startContainer(type: DockerRunTask) {
    dependsOn buildDocker
    containerName 'gradle_container'
    imageId project.docker.imageId
    ports '8001:8080'
    daemonize true
    remove true
}

3. Create a Dockerfile:
Create a new file named Dockerfile in the dev/workspace/sample-project directory.
Add the following code to deploy the generated war file in Tomcat server:

FROM tomcat:9.0.0.M26-jre8
COPY build/libs/gradle_webapp-1.0.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]

4. Initialize git:
Open the command prompt/terminal in the 'dev/workspace/sample-project directory'
Run the command 'git init'

5. Launch Jenkins and create a declarative pipeline:

Open Jenkins in your browser and login.
Click on "New Item" and enter a name for the pipeline (e.g. "gradle-build").
Select "Pipeline" and click "OK".
In the "Pipeline" section, add the following code:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'gradle clean build'
            }
        }
        stage('Docker Build') {
            steps {
                sh 'docker build -t gradle_image .'
            }
        }
        stage('Docker Run') {
            steps {
                sh 'docker run -p 8001:8080 --name gradle_container gradle_image'
            }
        }
    }
    post {
        always {
            sh 'docker stop gradle_container || true && docker rm gradle_container || true'
        }
    }
}


Ensure that the Gradle application is running on http://localhost:8001/gradle_webapp:

Open your browser and go to http://localhost:8001/gradle_webapp.
If the Gradle application is running successfully, you will see the default Tomcat homepage.

---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: ...

Mini-Projects for Docker, Docker Swarm

 Mini-Projects for Docker Challenge 1 Welcome to the Docker challenge, your task is to follow the below steps and complete them successfully. Step 1: Pull latest nginx image Step 2: Create a new bridge 'bridge_sample' Step 3: Run a couple of images (Cont1 and Cont2) and connect these to the new bridge created. Now try to ping from cont1 to cont2 to verify connectivity. Step 4: Stop containers Step 5: Remove network, containers, and images using docker commands Note: Execute "history -w" in the terminal before submitting the scenario. Before moving to Answers try to solve yourself... Let's get started... Step 1: Pull latest nginx image docker pull nginx:latest Step 2: Create a new bridge 'bridge_sample' docker network create bridge_sample Step 3: Run containers and connect them to the new bridge docker run -d --name Cont1 --network bridge_sample nginx:latest docker run -d --name Cont2 --netw...

Hands-on with Docker and Docker-Compose

  Hands-on with Docker & Docker-Compose (1) Create a "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose working-with-multiple-services/app", with base image "python:3.7" to dockerize the given Flask application "app.py" (2) Create another "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services/db", with base image "postgres" and configure the environment variables postgres user "abc", postgres password "abc@123", postgres db "postgres". (3) Create one more "Dockerfile" inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services/cache", with base image as "redis". (4) Create a "docker-compose.yml" file inside the path "-/Desktop/Project/docker-docker-compose-working-with-multiple-services" with the below specifications,      (4.1) Create three s...