Creating an image, and launching the latest version of java in the image with jenkins

Objective: The objective of this blog is to guide users through the process of creating a Docker image with the latest version of Java on CentOS using a Dockerfile through jenkins.

Prerequisites:

  • Access to a CentOS system with Docker installed.

  • Basic knowledge of Docker and CentOS command-line interface.

  • Basic knowledge of github and jenkins

1. Create a Dockerfile and push it into github repo:

dockerfile code:

#Use an official CentOS as a parent image

FROM centos:7

#Set environment variables for Java installation

ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk ENV PATH $JAVA_HOME/bin:$PATH

#Install OpenJDK 11

RUN yum update -y && yum install -y java-11-openjdk-devel && yum clean all

#Verify Java installation

RUN java -version

#Set the working directory

WORKDIR /app

#Set the working directory

CMD ["java", "-jar", "your-application.jar"]

  1. Open jenkins and create a job using freestyle

  2. Give the repo address on git and write this code

    docker build -t my-java-app . in the execute shell.

  3. Build now you image will be created and you can see it through this command(docker run -it --rm my-java-app).

  4. Thanks for reading !!