Skip to main content

Build & Deploy docker image using Gradle

 Build & Deploy docker image using Gradle


You are tasked to create a gradle java application, Dockerfile to run the 'springbootify.jar' file on port 8080, use suitable gradle plugins to configure the gradle application to build a docker image and run the docker container. On successful deployment of docker container, hit the url (http://localhost:8080/doit) and you could see the message on the webpage.

Note:

  • Jar file 'springbootify.jar' is already given in folder /Desktop/Project no need to build a need jar file.
  • Please do install gradle using the command "sudo apt install gradle"
  • Please do use "sudo service jenkins stop"
  • Create a Docker & gradle application in the path /Desktop/Project
  • Make the gradle project operable for both gradle & gradlew commands Docker base image: openj dk: 12-jdk-alpine.

Solving the tasks...

sudo apt install gradle
sudo service jenkins stop
cd /Desktop/Project

Create a new Gradle project using the following command:

gradle init

Add the following plugins to the build.gradle file to configure the Gradle application to build a Docker image:

build.gradle

plugins {
    id 'com.palantir.docker' version '0.22.1'
    id 'com.palantir.docker-run' version '0.22.1'
}
version '1.0-SNAPSHOT'
docker {
    name "${project.name}:${project.version}"
    files 'springbootify.jar'
}
dockerRun {
    name "${project.name}"
    image "${project.name}:${project.version}"
    ports '8080:8080'
    clean true
}

Now create a Dockerfile

Dockerfile

FROM openjdk:12-jdk-alpine
COPY springbootify.jar springbootify.jar
CMD ["java","-jar", "springbootify.jar"]

Lets first create a docker image

./gradlew docker

Check the created the docker image

docker ps

Next, run the docker image

./gradlew dockerRun

After this you can visit https://localhost:8080/doit, you can view the message

That's it! You have successfully created a Gradle Java application and Dockerfile, and deployed it using Docker.

Just some useful commands

# you can run jar file with, and check the https://localhost:8080/doit
java -jar springbootify.jar

# you can also use gradle to build java project
gradle init --type java-application

# to see all images
docker ps -a

# to remove image, use option -f to force delete
docker rmi <image-name/id>

# to remove container, use option -f to force delete
docker rm <container-name/id>

References: https://tomgregory.com/automating-docker-builds-with-gradle/


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

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