最简单的spring boot打包docker镜像的实现
这个spring boot项目只在网页输出一个hello world文本,没有其他复杂的配置和页面,属于入门级,可以放心食用。
本项目通过maven打包,打包和构建镜像的命令为:
mvn clean install package docker:build
spring boot打包docker镜像步骤如下:
(一)
在pom.xml文件中添加docker配置:
<!--docker maven plugin,在目录src/main/docker下创建Dockerfile文件,Dockerfile文件用来说明如何构建按镜像--> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>${project.artifactId}</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <!--下面的.jar不要忘记写,否则会报“ Exception caught: ADD failed: stat /var/lib/docker/tmp/docker-builder646478477/yang-0.0.1.jar: no such file or directory”错误--> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>
(二)
在src/main/docker文件夹(新建文件夹)下建Dockerfile文件,该文件不要后缀,可以新建一个txt文件,再把.txt后缀去掉,文件内容如下:
From java:8 VOLUME /TMP ADD yang-0.0.1.jar /yang-0.0.1.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/yang.jar"]
ADD yang-0.0.1.jar /yang-0.0.1.jar的名字 要和pom的<artifactId>yang</artifactId>
保持名字一样,不然maven打出来的包,docker找不到。
比如,我这个项目中的<artifactId>标签内容为:
pom.xml文件:
<artifactId>yang</artifactId> <version>0.0.1</version> <name>yang</name> <packaging>jar</packaging>
Dockerfile文件:
ADD yang-0.0.1.jar /yang-0.0.1.jar
ADD yang-0.0.1.jar /yang-0.0.1.jar这一句前面的jar包是本地打包的jar包名称,后面是复制到docker后的重命名。
本项目完整的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 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.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.springboot</groupId> <artifactId>yang</artifactId> <version>0.0.1</version> <name>yang</name> <packaging>jar</packaging> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <!--<docker.image.prefix>yang-0.0.1</docker.image.prefix>--> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--redis--> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis --> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-redis</artifactId>--> <!--<version>1.4.7.RELEASE</version>--> <!--</dependency>--> <!--<!– https://mvnrepository.com/artifact/mysql/mysql-connector-java –>--> <!--<dependency>--> <!--<groupId>mysql</groupId>--> <!--<artifactId>mysql-connector-java</artifactId>--> <!--<version>8.0.18</version>--> <!--</dependency>--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--docker maven plugin,在目录src/main/docker下创建Dockerfile文件,Dockerfile文件用来说明如何构建按镜像--> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <!--<imageName>${docker.image.prefix}/${project.artifactId}</imageName>--> <imageName>${project.artifactId}</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> </project>
到此这篇关于最简单的spring boot打包docker镜像的实现的文章就介绍到这了,更多相关spring boot打包docker镜像内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)