Docker部署SpringBoot项目

准备好pom.xml的插件

1.准备好pom.xml的插件

bash 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.7.RELEASE</version>
		<relativePath/>
	</parent>

	<groupId>com.weiz</groupId>
	<artifactId>student-management-system</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>学生信息管理系统</name>
	<description>Student Management System</description>
	<packaging>jar</packaging>

	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.24</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-springsecurity5</artifactId>
			<version>3.0.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.1</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.17</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.17</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<!--maven依赖打jar包插件-->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- 静态网页资源打jar包插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>3.2.0</version>
				<executions>
					<execution>
						<id>copy-resources</id>
						<phase>validate</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/classes</outputDirectory>
							<resources>
								<resource>
									<directory>src/main/resources</directory>
									<includes>
										<include>**/*.yml</include>
										<include>**/*.properties</include>
										<include>mapper/**</include>
										<include>static/**</include>
										<include>templates/**</include>
									</includes>
								</resource>
							</resources>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>3.1.2</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib/</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

这样修改静态网页资源打jar包插件后,您的静态资源(templates/, static/等)将会被打包进最终的JAR文件中,路径位于BOOT-INF/classes/目录下【用winrar打开检查】

2.安装docker:

bash 复制代码
yum install docker

检查安装是否成功

bash 复制代码
docker --version

启动docker

bash 复制代码
systemctl start docker

更换镜像源

bash 复制代码
sudo vim /etc/docker/daemon.json
bash 复制代码
{
 "registry-mirrors":["https://m9r2r2uj.mirror.aliyuncs.com"]
}

重启docker

bash 复制代码
systemctl restart docker

3.安装mysql(服务器放行3307端口):

bash 复制代码
docker pull mysql:8.0.24

创建自定义网络,确保MySQL容器和Spring Boot容器在同一个网络

bash 复制代码
docker network create student-net

运行mysql(修改MySQL大小写敏感配置):

bash 复制代码
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=admin -d -p 3307:3306 \
  --network student-net \
  --restart unless-stopped \
  -v mysql_data:/var/lib/mysql \
  mysql:8.0.24 \
  --lower_case_table_names=1
  

这样主机的 3307 端口会映射到容器的 3306 端口

navicat连接方式如下:

主机:服务器ip

端口:3307

用户名:root

密码:admin

4.构建springboot项目镜像

4.1对数据库连接等url起别名 (用于docker镜像间的连接):

因为使用的是自定义网络组网 → 应用配置中写 容器名:3306

bash 复制代码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://mymysql:3306/student_manage_system?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=admin

mysql8以后使用com.mysql.cj.jdbc.Driver

4.2对项目进行jar打包(cmd命令与src同级目录下输入)

bash 复制代码
mvn clean package -Dmaven.test.skip=true

这个是跳过错误进行打包

4.3上传jar包到服务器root目录

4.4创建目录,拷贝application.properties

bash 复制代码
mkdir -p src/main/resources/

将application.properties拷贝一份到src/main/resources目录下

4.5创建dockerfile文件:

bash 复制代码
vim Dockerfile
bash 复制代码
FROM openjdk:17-jdk
EXPOSE 8080

VOLUME /tmp

ENV TZ=Asia/Shanghai
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ADD student-management-system-0.0.1-SNAPSHOT.jar /app.jar
ADD src/main/resources/application.properties /config/

ENTRYPOINT ["java","-jar","/app.jar","--spring.config.location=classpath:/,file:/config/"]

4.6构建镜像:

bash 复制代码
docker build -t student-management-system:1.0 .

查看jar包镜像:

bash 复制代码
docker images

5.启动项目

Spring Boot应用启动命令

bash 复制代码
docker run -d --name student-app \
  --network student-net \
  -p 8080:8080 \
  -p 9326:9326 \
  student-management-system:1.0

注意这里的student-net

查看项目启动日志:

bash 复制代码
docker logs -f student-app
相关推荐
程序员小奕12 分钟前
Springboot 高校报修与互助平台小程序
spring boot·后端·小程序
MarkGosling33 分钟前
苹果原生 Linux 容器工具正式上线!Mac 开发者迎来效率革命
容器·wwdc
old_power1 小时前
Docker加入用户组
运维·docker·容器
晨曦~~1 小时前
SpringCloudAlibaba和SpringBoot版本问题
java·spring boot·后端
武昌库里写JAVA1 小时前
iview组件库:关于分页组件的使用与注意点
java·vue.js·spring boot·学习·课程设计
ytttr8732 小时前
docker快速部署OS web中间件 数据库 编程应用
前端·docker·中间件
liux35282 小时前
Kubernetes 从入门到精通-pod基础管理
云原生·容器·kubernetes
LUCIAZZZ2 小时前
Java设计模式基础问答
java·开发语言·jvm·spring boot·spring·设计模式
Cha0DD2 小时前
向量数据库Milvus在windows环境下的安装
docker·容器·milvus·向量数据库
异常君2 小时前
ZooKeeper ACL 权限模型详解:实现递归权限管理的有效方案
java·spring boot·zookeeper