1 新建一个maven项目
2 代码
@RestController
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "hello spring boot demo";
}
}
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
3 构建启动测试
4 编写dockfile resources下面
FROM java:8
MAINTAINER "zhangjin"<
[email protected]>
ADD maven/hello.jar test.jar
EXPOSE 8080
CMD java -jar test.jar
5 maven的docker部署plugin配置 Image配置好自己的阿里云私有镜像仓库地址
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33.0</version>
<configuration>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<dockerHost>unix:///var/run/docker.sock</dockerHost>
<verbose>true</verbose>
<images>
<image>
<!--如果要推送到docker hub,这里需要使用 用户名 -->
<name>${docker.registry}/${project.artifactId}:${project.version}</name>
<build>
<!--Dockerfile所在目录-->
<dockerFileDir>${project.basedir}/src/main/docker/</dockerFileDir>
<!--将jar包拷贝到docker build目录中-->
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
<!--latest标签-->
<tags>
<tag>latest</tag>
</tags>
</build>
</image>
</images>
</configuration>
</plugin>
6 构建之前登陆好自己的阿里云私有镜像仓库账号 参考自己的账号
docker login --username=
[email protected]
registry.cn-hangzhou.aliyuncs.com
7 然后,项目根目录执行
mvn docker:build
8 检查自己的阿里云镜像
推送到远程阿里云镜像仓库
mvn clean package docker:build docker:push
9 检查是否推送成功,并运行镜像
docker run -d -p 58080:8080
registry.cn-hangzhou.aliyuncs.com/zhangjin1988/springbootdemo:latest