xml配置
修改 ${maven}/conf/settings.xml, 加入:
<pluginGroups>
<pluginGroup>com.spotify</pluginGroup>
</pluginGroups>
然后在工程的pom.xml 里, 如此如此:
<plugin><!-- https://mvnrepository.com/artifact/com.spotify/dockerfile-maven-plugin -->
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<configuration>
<imageName>${docker.image.name}:${docker.image.tag}</imageName>
<dockerDirectory>${basedir}/target/dockerfile</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
docker 脚本
在 src/main 下新建docker 目录, 并新建 Dockerfile , 内容如下:
FROM openjdk:8-jdk-alpine
VOLUMN /tmp
ADD @project.build.finalName@.jar ~/app.jar
EXPOSE 9001
ENTRYPOINT ["java", "-jar", "~/app.jar"]
from: 基于xx模板构建
volumn: 指定宿主机的挂载点. 用于数据持久化&修改(在宿主机修改后可以在docker 容器里生效)
run: 在docker 容器里运行命令
@project.build.finalName@.jar: 环境变量
expose: 暴露docker 端口
ENTRYPOINT: 容器启动之后运行的命令
可能出错/异常
权限问题
如果遇到:
INFO: Retrying request to {}->unix://localhost:80
.....
Failed to execute goal com.spotify:docker-maven-plugin:1.2.2:build (default-cli) on project licensing-service: Exception caught: java.util.concurrent.ExecutionException: com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: java.io.IOException: Permission denied -> [Help 1]
原因是当前用户没有执行docker 权限. 将docker 权限设置为666 即可:
sudo chmod 666 /var/run/docker.sock
Could not acquire image ID
在1.3.X版本会出现,更新到1.4.X后修复
Must specify baseImage if dockerDirectory is null
镜像没有pull 下来, 运行这个:
docker pull openjdk:8-jdk-alpine
网友评论