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
相关推荐
木卯彳亍4 小时前
番外-linux系统运行.net framework 4.0的项目
linux·docker·.net
退役小学生呀7 小时前
十一、K8s细粒度权限管理RBAC
linux·docker·云原生·容器·kubernetes·k8s
gadiaola8 小时前
【SSM面试篇】Spring、SpringMVC、SpringBoot、Mybatis高频八股汇总
java·spring boot·spring·面试·mybatis
昭阳~8 小时前
Kubernetes 高级调度特性
云原生·容器·kubernetes
麦兜*8 小时前
Spring Boot 集成Reactive Web 性能优化全栈技术方案,包含底层原理、压测方法论、参数调优
java·前端·spring boot·spring·spring cloud·性能优化·maven
天上掉下来个程小白8 小时前
MybatisPlus-06.核心功能-自定义SQL
java·spring boot·后端·sql·微服务·mybatisplus
小马爱打代码10 小时前
Spring Boot:将应用部署到Kubernetes的完整指南
spring boot·后端·kubernetes
Jiangnan_Cai11 小时前
Linux 系统 docker 部署 Dify
linux·docker·大模型·dify
LuckyLay11 小时前
使用 Docker 搭建 Rust Web 应用开发环境——AI教你学Docker
前端·docker·rust
代码的余温12 小时前
Spring Boot集成Logback日志全攻略
xml·spring boot·logback