Build Jenkins master Docker image with preinstalled plugins

Key takeaways:

  • Create a Dockerfile to set up Jenkins with preinstalled plugins.

  • Use a plugins.txt file to specify desired plugins and versions.

  • Build the Docker image with docker build -t <image_name> . command.

  • Run the Jenkins container with mapped ports and persistent storage using Docker volume.

  • Access Jenkins at http://localhost:8080 to verify plugins are preinstalled.

  • Setting JAVA_OPTS=-Djenkins.install.runSetupWizard=false skips the Jenkins setup wizard, saving setup time.

To build a Jenkins master Docker image with preinstalled plugins, follow these steps. This involves creating a Dockerfile that installs Jenkins, the required plugins, and configures the environment for smooth operation.

Prerequisites

You’ll have to install Docker on your machine if you want to follow along locally.

Steps to create a Jenkins Docker image with preinstalled plugins

The following are the steps to create a Jenkins master Docker image with preinstalled plugins:

1. Install Docker

Ensure that Docker is installed on your system. Follow the code below:

docker --version

2. Create a Dockerfile

Create a Dockerfile that installs Jenkins, Jenkins plugins, and any other dependencies. Below is a sample Dockerfile:

FROM jenkins/jenkins:lts-jdk17
ENV JAVA_OPTS=-Djenkins.install.runSetupWizard=false
COPY --chown=jenkins:jenkins workflow-aggregator.hpi /usr/share/jenkins/ref/plugins/
COPY --chown=jenkins:jenkins plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN jenkins-plugin-cli -f /usr/share/jenkins/ref/plugins.txt
  • Line 1: To build the Jenkins Master image, we’ll include the Jenkins base image in our Dockerfile.

  • Line 3: We use downloaded .hpi files and copy them to /usr/share/jenkins/ref/plugins/ within the Dockerfile.

  • Line 4: We specify plugins to download via a plugins.txt file.

  • Line 5: Plugins can be manually installed by specifying them in the jenkins-plugin-cli command.

3. Create a plugins.txt file

This file lists all the Jenkins plugins you want to preinstall. Here’s an example of plugins.txt:

maven-plugin

You can customize this list by adding or removing plugins based on your needs. Make sure the plugin versions you specify are compatible with the version of Jenkins you’re using.

4. Build the Docker image

Now that your Dockerfile and plugins.txt are ready, you can build your Docker image. Run the following command in the directory where your Dockerfile is located:

docker build -t jenkins-master:preinstalled-plugins .

5. Run the Jenkins container

Once the Docker image is built, you can run it as a container using the following command:

docker run -d --name jenkins-master -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins-master:preinstalled-plugins
  • -d: Runs the container in detached mode.

  • -p 8080:8080: Maps the container’s port 8080 to the host’s port 8080 (for the Jenkins web interface).

  • -p 50000:50000: Maps the container’s port 50000 to the host’s port 50000 (for Jenkins agents).

  • -v jenkins_home:/var/jenkins_home: Creates a volume for persistent storage of Jenkins data.

6. Access Jenkins

Open your browser and go to http://localhost:8080. Jenkins will start with the plugins you preinstalled.

Try it out!

Apply what you have learned by using the provided terminal. You’re provided with the necessary files.

Note: The Dockerfile also includes the ENV JAVA_OPTS=-Djenkins.install.runSetupWizard=false statement to skip the installation wizard.

Press the “Run” button, build the image, and run the Jenkins container using the commands below. Then click the link in the widget below to see Jenkins with the plugins you preinstalled.

# Build the docker image
docker build -t jenkins-master:preinstalled-plugins .
# RUn the Jenkins container
docker run -d --name jenkins-master -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins-master:preinstalled-plugins
FROM jenkins/jenkins:lts-jdk17
ENV JAVA_OPTS=-Djenkins.install.runSetupWizard=false
COPY --chown=jenkins:jenkins workflow-aggregator.hpi /usr/share/jenkins/ref/plugins/
COPY --chown=jenkins:jenkins plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN jenkins-plugin-cli -f /usr/share/jenkins/ref/plugins.txt
Build Jenkins Master docker image with Preinstalled plugins

Conclusion

In conclusion, building a Jenkins master Docker image with preinstalled plugins streamlines the setup and ensures that Jenkins is configured and ready for immediate use with all necessary plugins. By following the steps outlined—creating a Dockerfile, specifying plugins in a plugins.txt file, building the Docker image, and running the container—you can automate the Jenkins setup process and improve consistency across environments. This approach is ideal for maintaining a reproducible Jenkins setup, making it easier to manage plugin dependencies and configurations, which ultimately enhances efficiency in CI/CD workflows.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to automate Docker build and auto deploy using Jenkins?

To automate Docker builds and deployment using Jenkins, create a Jenkins pipeline job. In the pipeline script:

  1. Pull the code from a repository (like GitHub).
  2. Build a Docker image using docker build.
  3. Push the image to a container registry (e.g., Docker Hub or AWS ECR).
  4. Deploy the container to the desired environment. Use Jenkins triggers to automate the process, such as webhook triggers on code commits or scheduled builds.

How do you configure the job in Jenkins?

To configure a job:

  1. Go to “New Item” in Jenkins, select “Freestyle project” or “Pipeline.”
  2. Configure the “Source Code Management” section with repository details.
  3. In the “Build” section, add build steps (e.g., shell commands for Docker commands).
  4. Configure “Post-build Actions” to automate deployment or notify via email/Slack.
  5. Save and run the job. Jenkins will follow the configured steps.

What is a Jenkins agent?

A Jenkins agent is a machine (physical or virtual) connected to the Jenkins master that executes jobs. Agents allow distributed job processing, helping Jenkins manage heavy workloads. Agents can be set up on remote servers or as Docker containers, and they connect to the Jenkins controller to receive and execute build tasks.


Does Jenkins have an API?

Yes, Jenkins has a REST API that allows programmatic access to manage Jenkins jobs, build details, and configurations. You can trigger builds, fetch build information, and manage job configurations through the API. The API supports JSON and XML formats and can be accessed through HTTP requests, making it versatile for integrations with other tools and services.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved