官网:https://spring.io/guides/gs/spring-boot-docker/
参考文档:https://my.oschina.net/AmosWang/blog/2088358
阿里云docker信息:https://cr.console.aliyun.com/cn-hangzhou/instances/repositories
1.在项目的pom文件中添加:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}
</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
<properties>
<java.version>1.8</java.version>
<docker.image.prefix>pheony</docker.image.prefix>
</properties>
在项目目录下添加Dockerfile文件
FROM openjdk:8-jdk-alpine VOLUME /tmp ARG JAR_FILE COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"]
2.项目打包
idea直接右边栏install即可或mvn install打包,生成文件在target目录下
3.本地安装docker 打开shell在项目目录下运行:mvn clean package docker:build
错误:No plugin found for prefix 'docker' in the current project and in the plugin groups.
修改 maven 的配置文件 settings.xml
<pluginGroups> <pluginGroup>com.spotify</pluginGroup> </pluginGroups>
错误:Must specify baseImage if dockerDirectory
使用mvn clean package dockerfile:build
4.本地镜像上传仓库,服务器上docker拉取
https://cr.console.aliyun.com/repository/cn-hangzhou/sycamore/sycamore/details
参照模板,此案例命令:
本地镜像上传 docker login --username=darksinco registry.cn-hangzhou.aliyuncs.com docker tag 854bcdabbe68 house docker tag 854bcdabbe68 registry.cn-hangzhou.aliyuncs.com/sycamore/sycamore:house docker push registry.cn-hangzhou.aliyuncs.com/sycamore/sycamore:house 服务器端拉取镜像 docker login --username=darksinco registry.cn-hangzhou.aliyuncs.com docker pull registry.cn-hangzhou.aliyuncs.com/sycamore/sycamore:house
5.服务器运行项目
docker run -p 8090:8090 -t registry.cn-hangzhou.aliyuncs.com/sycamore/sycamore:house