Comprehensive DevOps Automation: Docker, Jenkins, GitLab, Maven and SpringBoot Deployment Guide
This article provides a step‑by‑step DevOps tutorial covering Docker fundamentals, Jenkins pipeline setup, GitLab integration, Maven builds, SpringBoot configuration, multi‑machine remote deployment scripts, and practical tips for automating containerized applications.
1. Introduction and Purpose
The article introduces a practical DevOps guide that combines Docker commands, Jenkins pipelines, GitLab, Maven, and SpringBoot to achieve automated build and deployment, especially for SpringCloud projects.
2. What is DevOps?
DevOps (Development + Operations) is a set of practices and tools that promote collaboration between software developers and IT operations, enabling faster, more reliable software delivery through automation of build, test, and release processes.
3. Environment Setup Topics
Installing JDK 1.8 on CentOS 7
Installing Maven 3.6.1 on CentOS 7
Installing Git on CentOS 7
Configuring password‑less SSH for GitLab
Deploying GitLab, Docker network, Docker Registry, and Jenkins inside Docker containers
4. Required Preparations
4.1 Project Structure
Create a new SpringBoot project in IDEA or Eclipse.
4.2 SpringBoot Configuration and Code Details
4.2.1 pom.xml Dependencies
<dependencies>
<!-- SpringBoot starter -->
<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 starter -->
<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>4.2.2 Fixing "no main manifest attribute" Error
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.flong.SpringbootApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>4.2.3 Environment Variable File (.env)
JAVA_OPTS_DEFAULT=-Xmx512m4.2.4 Dockerfile for Building the Image
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.jar4.2.5 build.sh Script
#!/usr/bin/env bash
# Dynamic variables must not contain spaces around '='
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
echo ".......进入删除 Container & 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
echo ".......进入 Building & Images 操作......."
docker build -t $IMG_NAME:$IMG_VERSION -f $IMG_PATH"env/"Dockerfile_$RUN_EVN $IMG_PATH
docker tag $IMG_NAME:$IMG_VERSION $REGISTRY_URL/$IMG_NAME:$IMG_VERSION
docker push $REGISTRY_URL/$IMG_NAME:$IMG_VERSION
if [ -d "$IMG_PATH" ]; then echo "已经存在:"$IMG_PATH; else mkdir -p $IMG_PATH; fi
docker save $IMG_NAME -o $IMG_TAR_GZ_PATH/$IMG_NAME.tar.gz
echo ".......进入 Running 操作....."
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
echo ".......Build & Run Finish Success~...."
else
echo ".......Illegal Command Operation......."
fi4.2.6 Docker Commands (save, load, tag, push, pull)
docker save 镜像名 -o /path/镜像名.tar.gz
docker load -i /path/镜像名.tar.gz
docker tag 镜像名:版本 私服路径/镜像名:版本
docker push 私服路径/镜像名:版本4.2.9 Sample Controller
@RestController
public class SimpleController {
@Value("${runEvn}")
private String runEvn;
@GetMapping("/test")
public String test() {
return "this spring boot " + runEvn + " date long " + System.currentTimeMillis();
}
}5. Single‑Machine Remote Login & Jenkins Deployment
5.1 Special Notes
Demo uses the same machine for Jenkins and the target deployment to save resources.
5.2 Create Maven Project
Use Jenkins "New Item" to create a Maven job.
5.3 Parameterized Build
Add parameters (e.g., server, appName, version, port, env, serverPath).
Configure source code management (GitLab repository).
Set build goals: clean install -U -Dmaven.test.skip=true.
5.4 SSH Publisher Script (single machine)
#!/bin/bash
mkdir -p ${serverPath}
cd ${serverPath}
sh build.sh ${server} ${appName} ${version} ${port} ${env} ${serverPath}5.5 Build & Deploy Result
Screenshots show successful build logs and running container output.
6. Multi‑Machine Remote Login & Jenkins Deployment
6.1 Special Notes
Demo uses a separate test machine as the deployment target.
6.2 Create Maven Project
Same steps as the single‑machine case.
6.4 SSH Publisher Scripts
docker_server1 (creates directory on remote host)
#!/bin/bash
echo "用户名${userName}"
echo "服务器${server}"
echo "服务器目录${serverPath}"
ssh $server mkdir -p ${targetServerPath}
scp -r ${serverPath}/ ${userName}@${server}:${targetServerPath}docker_server2 (executes build on remote host)
#!/bin/bash
cd ${serverPath}
sh build.sh ${server} ${appName} ${version} ${port} ${env} ${serverPath}6.5 Build & Deploy Result
Shows build logs for both machines and successful container deployment.
7. Summary & Recommendations
The article emphasizes that the guide is for reference, encourages readers to consult official documentation, practice hands‑on, and explore further resources such as Docker docs, Maven plugins, and community articles.
References
https://docs.docker.com/engine/reference/commandline/docker/
https://yeasy.gitbooks.io/docker_practice/
https://github.com/spotify/docker-maven-plugin
https://dmp.fabric8.io/
https://github.com/jilongliang/springboot
https://www.cnblogs.com/kakaln/p/7872873.html
https://www.cnblogs.com/lucoo/p/10209892.html
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.
