概述
采用dockerfile-maven-plugin插件配合编写Dockerfile文件,生成docker镜像至本地/远程Docker服务中,所以需要提前安装好docker服务
实现过程
- 在springboot项目的pom文件中引入dockerfile-maven-plugin插件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>install-after</id>
<!--goal(build) 默认绑定在package阶段,会导致maven-install-plugin执行报错,故调整至install阶段-->
<phase>install</phase>
<goals>
<!--将镜像推送到远程docker服务器-->
<goal>build</goal>
<!--将远程docker服务器的镜像push到镜像仓库-->
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<!--默认推送到本地的docker(windows本地docker的交互端口为2375)-->
<repository>${project.artifactId}</repository>
<tag>latest</tag>
<buildArgs>
<APP_Path>/opt/app/${project.artifactId}</APP_Path>
<APP_Name>${project.build.finalName}</APP_Name>
</buildArgs>
</configuration>
</plugin>
- repository:镜像的名称
- tag:镜像的tag
- buildArgs:向Dockerfile中传递参数值
配置参考:https://github.com/spotify/dockerfile-maven
FROM williamyeh/java8:latest
# pom中buildArgs传递过来的参数
ARG APP_Path
ARG APP_Name
# docker run -e传递过来的参数
ENV Java_Memery_Options=""
ENV Spring_Profiles_Active="dev"
ENV Spring_Config_Location="config/"
ENV Dloader_Path="lib"
ENV Jar_File=${APP_Path}/${APP_Name}.jar
VOLUME ${APP_Path}/config
VOLUME ${APP_Path}/logs
COPY target/${APP_Name}.jar ${APP_Path}/${APP_Name}.jar
COPY src/main/resources/application.yaml ${APP_Path}/config/application.yaml
COPY src/main/resources/application-dev.yaml ${APP_Path}/config/application-dev.yaml
COPY src/main/resources/license-config.properties ${APP_Path}/config/license-config.properties
COPY src/main/resources/logback-custom.xml ${APP_Path}/config/logback-custom.xml
COPY lib/bcprov-jdk15on-1.67.jar ${APP_Path}/lib/bcprov-jdk15on-1.67.jar
WORKDIR ${APP_Path}
ENTRYPOINT ["sh","-c","java -jar $Java_Memery_Options -Dspring.profiles.active=$Spring_Profiles_Active -Dspring.config.location=$Spring_Config_Location -Dloader.path=$Dloader_Path $Jar_File"]
默认情况下,dockerfile-maven-plugin会调用本地的2375端口,如果需要构建到远程docker服务则需要配置环境变量
DOCKER_HOST
tcp://IP:2375
因为dockerfile-maven-plugin的插件目标(build)绑定在install阶段,所以在meven上执行install便会在相应的服务上生成docker镜像
暂未研究
https://www.jianshu.com/p/3929099711ec
问题记录
- shell执行报错 Syntax error: “(” unexpected
https://blog.csdn.net/Panda_813/article/details/88634181
- CMD和Entrypoint命令使用变量的用法
https://www.cnblogs.com/sanduzxcvbnm/p/15830845.html
- 如何将ARG值传递给ENTRYPOINT
https://qa.1r1g.com/sf/ask/2402699421/#
- 集成dockerfile-maven-plugin插件后maven-install-plugin打包报错
https://www.cnblogs.com/StarkBrothers/p/11986666.html
启动镜像脚本记录
docker run -itd --name xy-im-preload --network=host --restart=always --privileged=true -v /opt/app/xy-im-preload:/opt/app/xy-im-preload xy-im-preload:latest /bin/bash
|