Step-by-Step Guide to Installing Jenkins with Docker and Building Spring Boot Projects
This tutorial walks you through preparing the environment, installing Jenkins via Docker, configuring initial settings, integrating JDK, Maven, and Git, adding SSH servers and credentials, creating Maven jobs, and automating builds for Spring Boot applications on both GitHub and GitLab, including multi‑module project handling.
Continuous integration (CI) is essential for modern software development, and Jenkins is a popular tool for automating builds, tests, and deployments. This article explains how to install Jenkins using Docker, configure it, and set up automated builds for Spring Boot projects.
How to Install Jenkins
Jenkins can be deployed as a war file on Tomcat, but the guide uses the more convenient Docker method.
Environment Preparation
Before installation, ensure you have an Ubuntu server (or a local machine), JDK 1.8, Maven, Git, and a code hosting platform such as GitHub or GitLab.
Docker Installation
Pull the desired Jenkins image, for example: docker pull jenkins/jenkins:2.222.3-centos Create a local data volume: mkdir -p /data/jenkins_home/ Adjust permissions so the Jenkins user (uid 1000) can write to the volume: chown -R 1000:1000 /data/jenkins_home/ Run the container, mounting the data volume and the host's JDK and Maven directories:
docker run -d --name jenkins -p 8040:8080 -p 50000:50000 \
-v /data/jenkins_home:/var/jenkins_home \
-v /usr/local/jdk:/usr/local/jdk \
-v /usr/local/maven:/usr/local/maven \
jenkins/jenkins:2.222.3-centosThe command runs the container in the background ( -d), names it jenkins, and maps ports 8040→8080 and 50000→50000.
Initial Configuration
After the container starts, open http://<i>ip</i>:8040 to access Jenkins. Retrieve the initial admin password from /data/jenkins_home/secrets/initialAdminPassword, create an admin user, and install the recommended plugins.
If plugin installation is slow, replace the update URLs in /data/jenkins_home/updates/default.json with faster mirrors and run:
sed -i 's/www.google.com/www.baidu.com/g' default.json
sed -i 's/updates.jenkins-ci.org\/download/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.jsonBuilding a Spring Boot Project
Configure global tools in Jenkins (JDK, Maven, Git) under System → Global Tool Configuration . Ensure the paths point to the directories mounted inside the Docker container.
Install Additional Plugins
Install Maven Integration and Publish Over SSH via System → Manage Plugins → Available , then restart Jenkins.
Add SSH Server
Configure Publish Over SSH with the target server’s IP, port, credentials, and remote directory. Test the connection to confirm it reports success.
Add Credentials
Add GitHub or GitLab credentials (username/password or API token) under Credentials → System → Global credentials so Jenkins can clone the repository.
Create a Maven Job
From the Jenkins home page, click New Item, select Maven project, and configure:
Source Code Management: choose Git and provide the repository URL and credentials.
Build Environment: enable Add timestamps to the Console Output.
Build: set Root POM and Goals and options (e.g., clean package).
Post-build Actions: enable Send files or execute commands over SSH to deploy the built JAR.
The post‑build script (shown below) stops any running instance, backs up the previous JAR, copies the new JAR, and starts it:
# jdk environment (if not globally configured)
export JAVA_HOME=/xx/xx/jdk
export JRE_HOME=/xx/xx/jdk/jre
export CLASSPATH=/xx/xx/jdk/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
JAR_PATH=/data/jenkins_home/workspace/test/target
DIR=/data/test
JARFILE=swagger-demo-0.0.1-SNAPSHOT.jar
if [ ! -d $DIR/backup ]; then
mkdir -p $DIR/backup
fi
ps -ef | grep $JARFILE | grep -v grep | awk '{print $2}' | xargs kill -9
if [ -f $DIR/backup/$JARFILE ]; then
rm -f $DIR/backup/$JARFILE
fi
mv $JAR_PATH/$JARFILE $DIR/backup/$JARFILE
java -jar $DIR/backup/$JARFILE > out.log &
if [ $? = 0 ]; then
sleep 30
tail -n 50 out.log
fi
cd $DIR/backup/
ls -lt|awk 'NR>5{print $NF}'|xargs rm -rfBuilding Projects Hosted on GitLab
Install the GitLab Plugin, add a GitLab API token as a credential, and configure the plugin under System → Configure System → GitLab . Test the connection to ensure it reports Success. Then create a Maven job similar to the GitHub case, but use the GitLab repository URL and the GitLab credential.
Multi‑Module Project Build
For multi‑module Maven projects, specify the appropriate module build command in the Goals and options field (e.g., clean install -pl api) to control the build order.
Summary
The article provides a complete, from‑scratch guide to installing Jenkins in Docker, configuring the necessary tools, setting up SSH deployment, handling credentials, creating Maven jobs, and automating builds for both GitHub and GitLab hosted Spring Boot applications, including multi‑module scenarios.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
