How to create a custom Docker image with centos, JDK8 and Maven in the case of Jenkins integration with a Docker slave

Edmond Tchamie
6 min readMar 31, 2021

Today containerization platform like Docker is used to package your application and all its dependencies together in the form of containers so to make sure that your application works seamlessly in any environment which can be development or test or production.

In the process of understanding the working of Devops World, i fall on project where i have to integrate jenkins with a docker engine to launch some workers nodes and then deploy a task in these nodes. So before accomplish this task in my next write-up, i would like to create an docker image here in this write-up. Remainder: The creating of this Docker image will contain only a Base Os of centos, Maven install setup, Java jdk runtime environment and a Jenkins agent. And this is meant for my next write-up where i will integrate jenkins with a Docker engine for a Dynamic provisioning.

Goal

This demo aims to explain, step by step, how to create a custom docker image with the following components: centos, open-jdk8, and maven. After creating the image we will make it available to the community by pushing it into a container images repository like Docker Hub.

Before getting our hands dirty, it is recommended to have some knowledge about docker and containers. There are plenty of resources focused on containers technology out there, so just google for it or check the references at the end of the post. If you want to learn a quick introduction about how to run containers in docker you can have a look into an article I wrote about the topic here.

Step 1: Setup your environment

In my case i will use my Rhel8 Os from virtualbox and login on it by ssh on my local machine.

Create a folder/ workspace in which we will write our docker image file:

# mkdir dock-maven-jenk-ws
# cd dock-maven-jenk-ws/

Step 2: Create a Dockerfile

A Dockerfile lists the instructions needed to build a Docker image. So go to your project directory and create a file called Dockerfile.

Once created let’s edit it adding the following lines:

FROM centos:latestLABEL Tchamie Edmond <tchamieedmond@gmail.com>RUN  yum update -y && yum -qy install git && yum -qy install net-tools 

Let’s explain the content of the file:

  • FROM centos:latest: This is the starting point for your Dockerfile. Every Dockerfile typically starts with a FROM line. This FROM command receives as argument a basic existent docker image that we will use to build our layers on top of. The base image passed as argument is centos:latest. This image contains a jdk version 8 already installed. The latest version means we’ll download the latest version of centos available in docker hub.
  • LABEL Tchamie Edmond <tchamieedmond@gmail.com>: LABEL command defines the maintainer of the image, in case you want to contact him/her for any reason.
  • The RUN command executes instructions at build time. It means that you can use it to configure the image by adding application, installing packages, defining security rules… In our case, we use the yum package management utility (included within centos distributions) to install some tools we need to have available when running a container based on our image, like curl, tar, net-tools and git…

So, that’s how to define a very simple Dockerfile with net-tools, git, … installed (on top of centos ).

Step 3: Adding openssh-server, jdk, maven to our Dockerfile

Now we are going to update our Dockerfile by defining the openssh-server, jdk, maven installation on it. In order to do so, just open the Dockerfile and edit it as follows (the updates referring to the current step are commented in the script) :

Step 4: Adding jenkins user to our Dockerfile

Once jdk8 and maven have been successfully installed on our Dockerfile, the last step is to add a jenkins user inside it as this image is meant for launching a container which will serve as a worker node in my next write-up when i will integrate jenkins with docker container.As happened in the step 3, the changes on our Dockerfile related to the current step are commented and explained in the file below

Note:EXPOSE defines the port we want to expose to allow external world to be able to connect to this container through this port.

CMD defines the command to be executed when you start a container. You only can execute one command in this way, so if you define more than one CMD instruction, only the last one will be executed. In our case i set the service ssh to start asap when the container is launched.

Step 5: Building the image

That’s it! We have finished the Dockerfile and now it’s time to “actually” build the docker image from the just created Dockerfile. Open a terminal and go to your project folder (where the Dockerfile is placed).Then type the following command:

$ docker image build -t jenkins-slave-maven-jdk:latest .#Beware that the "point (.)" at the end of cmd means current folder of your Dockerfile

If everything went well, you should see the following line in your terminal:

Successfully tagged jenkins-slave-maven-jdk:latest

Note that the label “jenkins-slave-maven-jdk” is the name you want to assign to the image, so feel free to change it as you wish.

To verify that your image has been built, you can access your image list by typing:

Now if you want to run a container based on that image you can type the following command:

$ docker container run -d -it jenkins-slave-maven-jdk bash

Step 6: Pushing the image on Dockerhub

Finally we might want to make our docker image available to the community. In our case we decided to use Docker Hub as our central registry. The procedure to upload our image on Docker Hub consists of the following steps:

  • Navigate to Docker Hub and create a free account if you haven’t already.
  • Log in to the Docker registry account by typing docker login on your terminal and providing your credentials.
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username:
  • Tag the image with your username
$ docker tag jenkins-slave-maven-jdk  tcdocker2021/jenkins-slave-maven-jdk
  • Push your image to the Docker Hub registry:
$ docker push tcdocker2021/jenkins-slave-maven-jdk

To confirm that the image is available on Docker Hub, open a browser and go to the url related to your tag. In our case:

Conclusion

In this post we have demonstrated how easy can be to create your own custom docker image and I hope this post helped you in getting started with docker.

Follow me in my next write-up where i will integrate this image in a dynamic provisioning of Jenkins with a Docker engine server.

References

--

--

Edmond Tchamie

CI/CD Tech Explorer / ( DevOps & Cybersecurity Enthusiast ) on Lunar Planet🚀