在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
将Spring Boot项目部署到docker中有两种方法,手动部署和插件部署 手动部署1、idea创建spring boot项目pom.xml文件 <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-examples</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>DockerDemo</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 必须添加 启动类 package dockerdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @RequestMapping("/hello") public String hello(){ return "Hello Docker World!"; } } 2、项目打成 Jar 包然后在项目pom.xml文件所在目录执行maven命令将项目打成 Jar 包 $ mvn package 从输出日志可知 Jar 在 target 目录下,直接运行 Jar 包 $ java -jar DockerDemo-1.0-SNAPSHOT.jar 然后在浏览器中输入 http://localhost:8080/hello 进行测试 3、构建 docker image创建Dockerfile文件 FROM java:8 VOLUME /tmp ADD DockerDemo-1.0-SNAPSHOT.jar DockerDemo.jar RUN bash -c "touch /DockerDemo.jar" ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/DockerDemo.jar"] 参数解释:
创建好 Dockerfile 后,把打包好的 Spring Boot 项目 jar 包和 Dockerfile 文件放在任意一个目录下,使用 docker 命令构建镜像文件:
参数解释:
4、查看并运行镜像#查看镜像: $ docker images #运行镜像: $ docker container run --name DockerDemo -d -p 80:8080 DockerDemo:1 参数解释:
插件部署插件部署要在项目的 pom.xml 文件中添加 pom.xml <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-docker</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-eureka</artifactId> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 镜像前缀,推送镜像到远程库时需要,这里配置了一个阿里云的私有库 --> <docker.image.prefix> registry.cn-huhehaote.aliyuncs.com/monkeybrain </docker.image.prefix> <!-- docker镜像的tag --> <docker.tag>latest</docker.tag> <!-- 激活的profile --> <!--<activatedProperties></activatedProperties>--> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <profiles> <!-- docker环境 --> <!--<profile> <id>docker</id> <properties> <activatedProperties>docker</activatedProperties> <docker.tag>docker-demo-${project.version}</docker.tag> </properties> </profile>--> </profiles> <build> <!--默认maven命令--> <defaultGoal>install</defaultGoal> <finalName>${project.artifactId}</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!-- 配置spring boot maven插件,把项目打包成可运行的jar包 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> <!-- 打包时跳过单元测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <!-- 配置docker maven插件,绑定install生命周期,在运行maven install时生成docker镜像 --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <!--<executions> <execution> <phase>install</phase> <goals> <goal>build</goal> <goal>tag</goal> </goals> </execution> </executions>--> <configuration> <!-- 修改这里的docker节点ip,需要打开 docker节点的远程管理端口2375, 具体如何配置可以参照之前的 docker安装和配置的文章 --> <dockerHost>http://localhost:2375</dockerHost> <imageName>${docker.image.prefix}/${project.build.finalName}</imageName> <serverId>aliyun-docker-registry</serverId> <registryUrl>registry.cn-huhehaote.aliyuncs.com</registryUrl> <pushImage>true</pushImage> <!--镜像的标签--> <imageTags> <imageTag>latest</imageTag> </imageTags> <!--基础镜像--> <baseImage>java:8</baseImage> <!-- 这里的 entryPoint 定义了容器启动时的运行命令,容器启动时运行 java -jar 包名 --> <entryPoint> ["java","-jar","/${project.build.finalName}.jar"] </entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!--<image>${docker.image.prefix}/${project.build.finalName}</image> <newName>${docker.image.prefix}/${project.build.finalName}:${docker.tag}</newName> <forceTags>true</forceTags>--> <!-- 如果需要在生成镜像时推送到远程库,pushImage设为true --> <!--<pushImage>false</pushImage>--> </configuration> </plugin> </plugins> </build> </project> 运行推送命令$ mvn clean package docker:build -DpushImage 到此这篇关于Spring Boot应用通过Docker发布部署的流程分析的文章就介绍到这了,更多相关Spring Boot应用Docker部署内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论