Master Automated SpringBoot Deployment with Jenkins and Docker: A Step‑by‑Step Guide
This article walks you through half‑automatic and fully automated deployment of a SpringBoot application using Docker, a custom Bash script, and Jenkins, covering project packaging, Dockerfile creation, script details, Jenkins SSH publishing, and practical tips for reliable CI/CD pipelines.
SpringBoot+Jenkins automated deployment techniques, applicable to remote servers, with a reusable automation script.
Half‑automatic Deployment
Previously we packaged SpringBoot applications into Docker images via Maven plugins, which required exposing the Docker daemon port 2375 and posed security risks. This guide shows an alternative using a Dockerfile, a JAR file, and automation scripts, requiring some manual steps and thus called half‑automatic deployment.
Project Packaging
Comment out the Docker Maven plugin in pom.xml.
Run Maven's package command to produce a JAR file.
The generated JAR appears in the target directory and will be used for building the Docker image.
DockerFile
The Dockerfile defines how to turn the JAR into a Docker image. For details see the referenced article.
# The base image the container needs
FROM java:8
# Copy the JAR into the container root
ADD mall-tiny-jenkins-1.0-SNAPSHOT.jar /mall-tiny-jenkins-1.0-SNAPSHOT.jar
# Expose the service port
EXPOSE 8088
# Run the JAR when the container starts
ENTRYPOINT ["java", "-jar", "/mall-tiny-jenkins-1.0-SNAPSHOT.jar"]
# Maintainer
MAINTAINER macroAutomation Script
The following Bash script serves as a generic template; adjust parameters as needed. It stops the old service, removes the old container and image, builds a new image, and runs the new container.
#!/usr/bin/env bash
# Define application group name
group_name='mall-tiny'
# Define application name
app_name='mall-tiny-jenkins'
# Define application version
app_version='1.0-SNAPSHOT'
# Define environment profile
profile_active='qa'
echo '----copy jar----'
# Stop old container
docker stop ${app_name}
echo '----stop container----'
# Remove old container
docker rm ${app_name}
echo '----rm container----'
# Remove old image
docker rmi ${group_name}/${app_name}:${app_version}
echo '----rm image----'
# Build new Docker image
docker build -t ${group_name}/${app_name}:${app_version} .
echo '----build image----'
# Run new container
docker run -p 8088:8088 --name ${app_name} \
--link mysql:db \
-e 'spring.profiles.active'=${profile_active} \
-e TZ="Asia/Shanghai" \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/${app_name}/logs:/var/logs \
-d ${group_name}/${app_name}:${app_version}
echo '----start container----'Key points in the script: group_name, app_name, and app_version define image attributes. profile_active selects the configuration profile (e.g., qa for testing, prod for production).
Removing the old image with docker rmi prevents “none” images from accumulating.
Setting the timezone with -e TZ="Asia/Shanghai" ensures container time matches the host.
Deployment Run
Upload the JAR, Dockerfile, and automation script to the target directory on the server.
Make the script executable: chmod +x run.sh.
Execute the script with ./run.sh.
Integrating Jenkins for Automated Deployment
Previously the packaging and file transfer steps were manual; Jenkins can automate them, achieving true continuous deployment.
Prerequisites
Familiarity with Jenkins is assumed; see the referenced Jenkins tutorial for details.
Publish Over SSH Plugin
Install the Jenkins "Publish Over SSH" plugin to transfer files and run commands on remote servers via SSH.
Install the plugin via System → Plugin Management.
Add SSH configuration in the same section.
Create a Jenkins job with Maven build steps, then add a post‑build step "Send files over SSH and execute commands".
Configure the SSH Publisher with source and destination paths and the script to run.
Run the job; the same method works across multiple servers.
Conclusion
Linux commands and Docker commands are essential for automated deployment; Jenkins simply orchestrates these commands.
Project Source Code
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-jenkins
Recommended Reading
Don’t reinvent the wheel—use open‑source utility classes.
Redis 6.0 with multithreading released.
My favorite IDEA plugins.
Beware of dangerous "INSERT INTO SELECT" statements.
MongoDB quick start guide.
SpringBoot annotation cheat sheet.
Database design tools I recommend.
Vue learning roadmap in 5 days.
Must‑see Spring Cloud project.
My open‑source projects reaching 20k stars.
Welcome to follow and like!
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.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
