Master Automated SpringBoot Deployment with Jenkins and Docker
Learn how to automate the packaging, Docker image creation, and remote deployment of a SpringBoot application using a semi‑automatic script and Jenkins, covering Dockerfile setup, Bash deployment scripts, Jenkins Publish Over SSH configuration, and best practices for reliable production releases.
SpringBoot + Jenkins automation deployment techniques, applicable to remote deployment, with a universal automation script! This article starts with semi‑automatic deployment and progresses to full automation, presenting a production‑ready deployment solution.
Semi‑Automatic Deployment
Previously the SpringBoot application was packaged into a Docker image using the Maven Docker plugin, which required exposing the Docker daemon port 2375 and posed security risks. This guide introduces an alternative method using a Dockerfile, a Jar file, and an automation script, requiring some manual steps and thus termed semi‑automatic deployment.
Project Packaging
Comment out the Docker Maven plugin in pom.xml.
Run Maven's package command to produce a Jar file.
The Jar appears in the target directory and will be used for Docker image building.
Dockerfile
The Dockerfile defines how to turn the Jar into a Docker image. The core content is:
# This image needs a base image
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
A generic Bash script template that stops the old service, removes the old container and image, builds a new image, and runs it.
#!/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----'
docker stop ${app_name}
echo '----stop container----'
docker rm ${app_name}
echo '----rm container----'
docker rmi ${group_name}/${app_name}:${app_version}
echo '----rm image----'
# Build Docker image
docker build -t ${group_name}/${app_name}:${app_version} .
echo '----build image----'
# Run 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 the image attributes. profile_active selects the configuration profile (e.g., qa for testing, prod for production).
Running docker rmi is essential; otherwise stale none images accumulate.
Setting the timezone with -e TZ="Asia/Shanghai" prevents an 8‑hour time drift inside the container.
Deployment Run
Upload the Jar, Dockerfile, and script to the target directory on the server.
Make the script executable: chmod +x run.sh.
Execute ./run.sh to start the container.
Integrating Jenkins for Automated Deployment
Earlier steps were manual; Jenkins can automate them, achieving true CI/CD.
Prerequisites
Familiarity with Jenkins is assumed; refer to the linked tutorial for a quick overview.
Publish Over SSH Plugin
Install the Jenkins "Publish Over SSH" plugin to transfer files and execute commands on remote servers. Configure the plugin via “System → Plugin Management”, add SSH credentials, and create a build job that packages the code with Maven. The final build step should be “Send files over SSH”.
Configure the SSH Publisher with source and destination paths and the script to run.
Run the job to perform automated deployment across different servers.
Summary
Linux‑based automated deployment primarily relies on a series of Linux and Docker commands. Jenkins automates these commands, so mastering Linux and Docker is essential for effective CI/CD pipelines.
Project Source
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-jenkins
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.
