1.构建spring boot 项目 和 安装docker(略)
2.引入 docker-maven-plugin
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<dockerHost>http://localhost:2375</dockerHost>
<imageName>mavendemo</imageName>
<baseImage>java</baseImage>
<maintainer>docker_maven docker_maven@email.com</maintainer>
<workdir>/ROOT</workdir>
<cmd>["java", "-version"]</cmd>
<entryPoint>["java", "-jar", "${project.build.finalName}.jar"] </entryPoint>
<!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
<resources>
<resource>
<targetPath>/ROOT</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
3.项目打包
4.docker 构建
效果
Dckerfile
FROM java
MAINTAINER docker_maven docker_maven@email.com
WORKDIR /ROOT
ADD /ROOT/demo-0.0.1-SNAPSHOT.jar /ROOT/
ENTRYPOINT ["java", "-jar", "demo-0.0.1-SNAPSHOT.jar"]
CMD ["java", "-version"]
问题1
[ERROR] Failed to execute goal com.spotify:dockerfile-maven-plugin:1.3.6:build (default-cli) on project test: Could not build image: com.spotify.docker.client.shaded.com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.spotify.docker.client.messages.RegistryAuth: no String-argument constructor/factory method to deserialize from String value ('swarm')
[ERROR] at [Source: N/A; line: -1, column: -1] (through reference chain: java.util.LinkedHashMap["stackOrchestrator"])
解决办法:maven 插件去掉版本号
问题2
tcp protocol is not supported
解决办法:maven 插件 docker host 需要配置 http
<dockerHost>http://localhost:2375</dockerHost>
构建后的效果
5.运行docker image
docker run -d -p 8080:8080 mavendemo