本篇文章为大家展示了springboot中怎么使用maven构建docker镜像,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
首先注册阿里云镜像仓库,新建仓库,并获得仓库公网地址.
registry.cn-shanghai.aliyuncs.com/smartlife-docker/deploy
此处,需要注意,镜像仓库有密码,需要在首页进行密码重置.页面显示使用登录密码即可,但是实际使用中还是要重置一次比较靠谱.
以下为本地操作.
首先配置 maven 的 setting.xml
添加如下内容
<server>
<id>docker</id> <!--此处为配置ID信息-->
<username>a@a.com</username> <!--用户名-->
<password>123123</password> <!--密码-->
<configuration>
<email>a@a.com</email> <!--对应用户名-->
</configuration>
</server>
下面在程序中新增dockerfile,构建docker镜像
# Version 0.0.1
FROM java:8
VOLUME /data/deploy/tmp
ADD @project.build.finalName@.@project.packaging@ app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 7074
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
dockerfile文件位于
/src/main/docker/
修改pom文件
<docker.image.prefix>镜像名</docker.image.prefix>
<docker.repostory>阿里云仓库地址</docker.repostory>
<docker.registry.name>命名空间</docker.registry.name>
在build部分增加
docker-maven-plugin
插件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<!--配置使用mvn clean package的同时进行docker构建-->
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<skipDocker>false</skipDocker>
<!-- 私有仓库配置,需要settings.xml文件配合serverId对应的服务地址 -->
<serverId>docker-aliyun</serverId>
<registryUrl>${docker.repostory}/${docker.registry.name}/${project.name}</registryUrl>
<!-- <forceTags>true</forceTags> -->
<!--install阶段也上传,否则只有deploy阶段上传-->
<pushImage>true</pushImage>
<dockerDirectory>target/docker</dockerDirectory>
<imageName>
${docker.repostory}/${docker.registry.name}/${project.name}:${project.version}
</imageName>
<dockerHost>http://192.168.125.245:2375</dockerHost>
<resources>
<rescource><!-- 将打包文件放入dockerDirectory指定的位置 -->
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</rescource>
</resources>
</configuration>
</plugin>
在 build节点下增加 resources节点
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/docker</directory>
<filtering>true</filtering>
<includes>
<include>**/Dockerfile</include>
</includes>
<targetPath>../docker</targetPath>
</resource>
</resources>
上述内容就是springboot中怎么使用maven构建docker镜像,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/bddiudiu/blog/3115373