Dockerizing a Maven Project
Image Courtesy : https://medium.com/containers-101/using-docker-from-maven-and-maven-from-docker-1494238f1cf6

Dockerizing a Maven Project

What should you know about Docker?

IT engineers often get confused with Docker as new age Virtual Machine. Docker can be considered as a child process with better isolation and environment control. If you are new to Docker, I would suggest you go through below article first,

What files need to be included in a Maven Project’s Docker image?

Let us first list down the required files/infrastructure to execute a maven project,

  1. A machine with Windows/Linux/Mac OS
  2. Installation/Setup of Java
  3. Installation/Setup Maven
  4. Maven Project with tests which needs to be executed
  5. Browser for execution (If our tests need it)

These are the same files/infrastructure we require to create a Docker Image. We would need a Base Image with Java and Maven already setup/installed in it and a maven project. We will read more about this in the next section.

Choosing a base Docker image for your Maven Project

One of the important and time-consuming parts of the Dockerizing process is to select the base image, IT associates who work in this area often spend more time searching for Base Image which is suitable for their needs (I really have a hard time searching for the base image initially). But thanks for Docker community, they have created a website which really helps a lot for searching the Base Image,

Based on my requirement to execute Maven Project, I understand that below base image would serve our solution,

No alt text provided for this image


Build a Maven Project’s Docker image

Now that we have our base image ready, what we need to do is just copy the files inside this base image and start the execution. But it is not that straight forward as it seems, to create the Maven Project’s Docker image we need to first create a Dockerfile and then build that Dockerfile to create a Docker image with Maven project in it ready to be executed.

We need to set up the WORKDIR first, this means that whatever action should be performed inside Docker container should be done on this folder.

No alt text provided for this image


Now we have our WORKDIR set, next what we have to do now is copy the maven project files. For this action the Docker command is,

No alt text provided for this image


Next what we have to do is installation of chrome, the normal installation of any browser require is to download the .exe file (depends on OS) and then double click to install the browser after following the steps (Usually Next > Next > Next > Install), we have to find an alternate way to install browser using command prompt or shell commands. For this below is the command which needs to be added to the DockerFile,

No alt text provided for this image

Note that, the installation command varies from OS to OS and therefore we need to get the proper command with which the installation of the browser would take place.

Once we have all the things in place, all we have to do is to add the maven commands which will start the execution of tests. For this below are the lines which need to be added to the DockerFile,

No alt text provided for this image


Now the DockerFile should look like this,

No alt text provided for this image

To build the image from this Dockerfile, below are the steps needs to be followed,

  • Copy this Dockerfile and paste it to the root location of the maven project.
  • Open the command prompt in the same folder and execute below command,
No alt text provided for this image
  • Once the build process is completed, verify the docker images by executing below command,
No alt text provided for this image

Running a Maven Project inside Docker container

Now we have a Docker image ready with us which has our project and base image on which the maven project should be executed. Below command will run the Docker image, which will also start executing the tests inside Docker container which are there in the Maven project,

No alt text provided for this image

Maven project starts to execute inside the container,

No alt text provided for this image

Test execution is completed

No alt text provided for this image

Below is the sample test which executed

No alt text provided for this image





Build execution completed

No alt text provided for this image

Use below command to check the status of the container

No alt text provided for this image


👏🏻👏🏻nicely written with all needed details

To view or add a comment, sign in

More articles by Akshay Sharma

Others also viewed

Explore content categories