部署文章目录:目录
docker部署,原理,命令,可以参考:docker原理图,部署,命令
目录
将springboot部署到docker中
遇到过的问题:
pom配置
操作步骤
生成jar
构建镜像
查看镜像docker images
运行镜像
查看运行的镜像
将springboot部署到docker中
遇到过的问题:
- 生成出来的jar包缺少依赖JAR的
- 启动时找不到主类
- 容器启动后主机拒绝
pom配置
pom.xml的文件,下面红色的,是docker相关的
<?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" 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.7.10-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sheng</groupId> <artifactId>myDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>myDemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2021.0.5</spring-cloud.version> <dockerfile-maven-plugin.version>1.4.10</dockerfile-maven-plugin.version> <docker.image.prefix>springboot</docker.image.prefix> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>${dockerfile-maven-plugin.version}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!--docker和maven结合的配置--> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>${dockerfile-maven-plugin.version}</version> <executions> <execution> <id>default</id> <goals> <!--如果package时不想用docker打包,就注释掉这个goal, 运行mvn package时,会自动执行build目标,构建Docker镜像。 当运行mvn deploy命令时,会自动执行push目标,将Docker镜像push到Docker仓库--> <!-- <goal>build</goal> <goal>push</goal> --> </goals> </execution> </executions> <configuration> <!--在maven运行docker时,dockerFile文件必须在这个文件夹内--> <contextDirectory>${project.basedir}/docker</contextDirectory> <!--repository指定docker镜像的repo名字--> <repository>${docker.image.prefix}/demo</repository> <!--tag指定docker镜像的tag--> <tag>v1</tag> <!--buildArgs可以指定一个或多个变量,传递给Dockerfile,在Dockerfile中通过ARG指令进行引用。暂时不用,注释掉--> <!-- <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> --> </configuration> </plugin> <plugin> <!--用于生成jar的时候,将依赖的包也导入进去,否则无法启动,另外MANIFEST.MF文件中会找不到启动类--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> <includeSystemScope>true</includeSystemScope> <mainClass>${start-class}</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </pluginRepository> </pluginRepositories> </project>
操作步骤
生成jar
先maven上点击clean compile package 在target生成JAR包(myDemo-0.0.1-SNAPSHOT.jar) 把它用解压缩包打开,看看MANIFEST.MF中有没有主函数入口,有红圈的就行,不然无法启动,会报错no main manifest attribute, in /xxxxx.jar异常 将 F:\workspace\arch\spring-cloud-demo\target\myDemo-0.0.1-SNAPSHOT.jar 拷贝到 F:\workspace\arch\spring-cloud-demo\docker F:\workspace\arch\spring-cloud-demo\docker 文件夹中应该有Dockerfile 和 myDemo-0.0.1-SNAPSHOT.jar
然后用bash做docker命令
docker images 查看镜像
构建镜像
docker build -t springboot/demo .
最后的警告没有影响
查看镜像docker images
删除镜像:只需删除镜像对应的image id即可,例如删除java执行命令: docker rmi d23bdf5b1b1b
若存在两个镜像的image id相同,需要通过REPOSITORY+TAG删除,例如删除java执行命令: docker rmi java 8
若报删除的镜像存在依赖镜像错误,则需通过 docker rm 【依赖镜像id】,命令依次删除依赖
若镜像已经运行,需要先停止运行再删除
运行镜像
docker run -p 8005:8005 -d springboot/demo
-p表示端口映射,此时将服务器中的8005端口映射到容器中的8005端口
查看运行的镜像
docker ps
查看所有容器,包含未运行的:docker ps -a
停止运行镜像:docker stop ceb5f1a71a29
11.浏览器访问 http://192.168.99.100:8005/ 出现项目页面,即发布成功