Comprehensive Guide to DevOps Automation with Docker, Jenkins, Maven, and SpringBoot
This article provides a step‑by‑step tutorial on building a DevOps pipeline that integrates Docker, Jenkins, GitLab, Maven, and SpringBoot, covering environment setup on CentOS, Maven pom configuration, Dockerfile creation, shell scripting for image building, multi‑machine deployment, and practical Jenkins job configuration.
The article introduces DevOps as a set of practices that bridge development and operations, emphasizing automation of software delivery and infrastructure changes.
It outlines the preparation work required on a CentOS 7 host, including installing JDK 1.8, Maven 3.6.1, Git, Docker, GitLab, Docker Registry, and Jenkins.
Project Structure : Create a SpringBoot application using IDEA or Eclipse.
pom.xml Configuration :
<dependencies>
<!-- Springboot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Devtools for hot reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- Configuration processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springboot</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.flong.SpringbootApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>Dockerfile Example (development environment) :
FROM frolvlad/alpine-oraclejdk8:slim
MAINTAINER [email protected]
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN mkdir -p /home/devsoft/springboot_dev
WORKDIR /home/devsoft/springboot_dev
EXPOSE 7011
ADD ./target/springboot.jar ./
CMD java ${JAVA_OPTS_DEFAULT} -Djava.security.egd=file:/dev/./urandom -jar springboot.jarKey Docker instructions such as WORKDIR, ADD, and CMD are explained, along with environment variable handling.
build.sh Shell Script (used by Jenkins to build, tag, push, and run images):
#!/usr/bin/env bash
# Dynamic variables must not contain spaces or tabs
IMG_SERVER="$1"
IMG_NAME="$2"
IMG_VERSION="$3"
IMG_PORT="$4"
RUN_EVN="$5"
IMG_PATH="$6"
echo "服务地址:$IMG_SERVER"
echo "工程镜像名称:$IMG_NAME"
echo "工程版本号:$IMG_VERSION"
echo "工程端口:$IMG_PORT"
echo "服务环境:$RUN_EVN"
REGISTRY_URL="192.168.1.235:5000"
IMG_TAR_GZ_PATH="/home/img_tar_gz_path/"
if [ "$IMG_SERVER" != "" ] && [ "$IMG_NAME" != "" ] && [ "$IMG_VERSION" != "" ] && [ "$IMG_PORT" != "" ]; then
# Clean up old containers and images
CONTAINER_ID=$(docker ps -a | grep $IMG_NAME | awk '{print $1}')
IMAGE_ID=$(docker images | grep $IMG_NAME | awk '{print $3}')
if [[ "$CONTAINER_ID" != "" ]]; then
docker rm -f $CONTAINER_ID
fi
if [[ "$IMAGE_ID" != "" ]]; then
docker rmi -f $IMAGE_ID
fi
# Build image (environment‑specific Dockerfile)
docker build -t $IMG_NAME:$IMG_VERSION -f $IMG_PATH"env/"Dockerfile_$RUN_EVN $IMG_PATH
# Tag and push to private registry
docker tag $IMG_NAME:$IMG_VERSION $REGISTRY_URL/$IMG_NAME:$IMG_VERSION
docker push $REGISTRY_URL/$IMG_NAME:$IMG_VERSION
# Save image tarball
docker save $IMG_NAME -o $IMG_TAR_GZ_PATH/$IMG_NAME.tar.gz
# Run container
docker run -d --network default_network --restart=always --env-file=./.env -e spring.profiles.active=$RUN_EVN \
--expose=$IMG_PORT --name=$IMG_NAME -p $IMG_PORT:$IMG_PORT $IMG_NAME:$IMG_VERSION
else
echo ".......Illegal Command Operation......."
fiThe script demonstrates conditional checks, cleanup of dangling images, building with environment‑specific Dockerfiles, tagging, pushing to a private registry, saving the image, and running the container.
Docker Commands such as docker save, docker load, docker tag, and docker push are described with examples.
Jenkins Pipeline : The article walks through creating a parameterized Maven job, adding build parameters (server IP, app name, version, port, environment, paths), configuring source code management, setting Maven goals (e.g., clean install -U -Dmaven.test.skip=true), and using SSH Publishers to execute the build.sh script on target machines. Both single‑machine and multi‑machine deployment scenarios are covered, with separate shell scripts for the Jenkins host and remote nodes.
Finally, the article provides a summary, suggestions for further learning, and a list of reference URLs covering Docker documentation, Docker Maven plugin, Fabric8, and related tutorials.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
