![d2718e73d86ec0ceb110625a0a9e6f12.png](https://i-blog.csdnimg.cn/blog_migrate/078ada216ac045a3357e2be3b5a7e889.jpeg)
简介
Spring Boot的快速开发特性,正受越来越多Java开发者的欢迎,配合supervisord可以轻松使其作为一个独立的服务运行。而随着Docker的流行,其轻量级、进程级、资源隔离等特性,使Spring Boot的部署、运行更加灵活,若将其打包成Docker镜像可以真正做到“一次打包,到处运行”,有效的解决了开发、测试、预生产、生产等环境的差异。
下面我们就从Docker手动、自动部署Spring Boot项目来讲解下,Docker是如何轻松部署的。因此你想要的Docker自动部署Spring Boot就在这了。
我们正式开始讲解吧。
手动构建spring boot应用
此部分通过直接手动打包、Docker手动部署Spring Boot,运行helloworld项目。
helloworld应用
- 创建spring boot工程
使用IntelliJ IDEA的“Spring Assistant”插件创建spring web项目
Group id: com.docker
Artifact id: hellworld
Project name: helloworld
Package name: com.docker.helloworld
- 创建HelloworldController
vim HelloworldControllerpackage com.docker.helloworld;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloworldController { private static final Logger logger= LoggerFactory.getLogger(HelloworldController.class); @RequestMapping("/") public String index(){ return "Hello world"; }
- 编译运行
mvn clean package && java -jar target/helloworld-0.0.1-SNAPSHOT.jar
maven手动打包后并运行jar包,通过localhost:8080可以直接hellworld项目了,下面手动将jar包放到Docker中运行。
docker构建镜像
- Dockerfile
#基础镜像java 1.8From java#匿名数据卷,在启动容器时忘记挂载数据卷,会自动挂载到匿名卷VOLUMN /tmpARG JAR_FILE=target/*.jarCOPY ${JAR_FILE} helloworld.jarENTRYPOINT ["java","-jar","/helloworld.jar"]
- 构建镜像并运行容器
#从dockerfile构建镜像[root@test]# docker build -t docker/helloworld .#docker/helloworld就是我们构建的新镜