Run Maven with Jenkins Docker image via custom Docker build

To run Maven with Jenkins using a custom Docker build, you can follow these steps:

Create a Dockerfile for your custom Jenkins image

FROM jenkins/jenkins:latest
USER root
# Install Maven
RUN apt-get update && apt-get install -y maven
# Switch back to the Jenkins user
USER jenkins

In this Dockerfile, we start with the official Jenkins image (jenkins/jenkins:latest), switch to the root user to install Maven using apt-get, and then switch back to the Jenkins user.

Build the Docker image

docker build -t my-custom-jenkins .

This command builds the Docker image based on the Dockerfile in the active directory and tags it as my-custom-jenkins.

Run the Docker container:

docker run -d -p 8080:8080 -p 50000:50000 -v $(pwd):/my_files my-custom-jenkins

This command starts a Docker container based on the custom Jenkins image. It maps the container on port 8080 (Jenkins web interface) and 50000 (Jenkins agent communication) to the corresponding ports on the host machine. -v $(pwd):/my_files my-custom-jenkins The command adds the current working directory to the Docker image. The active directory contains the maven application and POM file.

Access Jenkins in a web browser

Go to the web browser and navigate to http://localhost:8080. This should take you to the Jenkins web interface.

Web interface
Web interface

Complete Jenkins setup

During the initial setup, Jenkins will ask for an administrator password. To obtain the password, run the following command:

docker exec <container_id> cat /var/jenkins_home/secrets/initialAdminPassword

Replace <container_id> with the ID or name of your running Jenkins container. Copy the generated password and type it into the Jenkins web interface to proceed with the setup. Return to the Jenkins web interface and paste the copied administrator password into the provided field. Then, click the "Continue" button.

In the next step, Jenkins offers you two options: "Install suggested plugins" and "Select plugins to install." Choose the "Install suggested plugins" option to let Jenkins install the recommended plugins automatically.

Install plugins
Install plugins

Jenkins will start installing the plugins selected in the previous step. This will take a few minutes as it downloads and installs the required plugins.

Create an admin user

After installing the plugins, Jenkins will ask you to create an admin user. Fill in the required information, including the username, password, full name, and email address. Remember these credentials, as they will be used to access Jenkins.

Admin user creation
Admin user creation

Jenkins is ready

Once the admin user is created, Jenkins will display a "Jenkins is ready!" message. You can click the "Start using Jenkins" button to access the Jenkins dashboard.

Verify plugin installation

We can verify that the Maven Integration plugin is installed on the Jenkins dashboard. Click "Manage Jenkins" in the left sidebar and select "Manage Plugins." You should see the Maven Integration plugin listed among the installed plugins in the Installed tab.

If the plugin is unavailable, go to the available plugins and install the Maven integration plugin.

Jenkins job creation and Maven build test

  1. create Jenkins job.

Creat Jenkins job
Creat Jenkins job

Enter the job name, select Maven app from the listed project's type, then press “OK”.

Create Maven project
Create Maven project

Add path to POM file.

Add POM file path
Add POM file path

Add post-build batch commands and save the job.

cd /my_files/maven-demo-master/
mvn clean test
Post-build cmd
Post-build cmd

Now we have successfully configured the Jenkins job. Click on "Build Now" to test the app.

Build successful
Build successful

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved