前言

今天完成对SpringBoot项目的部署。

准备好SpringBoot项目

构建一个SpringBoot项目,写一个路由hello,访问返回hello world, docker!

  1. @Slf4j
  2. @RestController
  3. @SpringBootApplication
  4. public class DockerWebApplication implements CommandLineRunner {
  5. @Value("${spring.application.name}")
  6. private String applicationName;
  7. public static void main(String[] args) {
  8. SpringApplication.run(DockerWebApplication.class, args);
  9. }
  10. @GetMapping(value = "/hello")
  11. public String hello() {
  12. log.info("[hello] hello world, docker!");
  13. return "hello world, docker!";
  14. }
  15. @Override
  16. public void run(String... args) throws Exception {
  17. log.info("{} 启动成功...", applicationName);
  18. }
  19. }

项目打包

在项目根目录运行mvn clean package将项目打包成jar包,完成之后会在target目录下生成一个可执行的jar文件。

  1. <build>
  2. <plugins>
  3. <!--打包-->
  4. <plugin>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-maven-plugin</artifactId>
  7. <configuration>
  8. <mainClass>com.ronan.docker.DockerWebApplication</mainClass>
  9. </configuration>
  10. </plugin>
  11. </plugins>
  12. </build>

编写Dockerfile

在项目根目录编写Dockerfile文件

  1. # 指定基础镜像
  2. FROM java:8
  3. # 指向了一个`/tmp`的目录,由于Spring Boot使用内置的Tomcat容器,Tomcat默认使用`/tmp`作为工作目录。
  4. VOLUME /tmp
  5. # 项目的`ronan-docker-web.jar`作为`app.jar`添加到容器
  6. ADD ronan-docker-web.jar app.jar
  7. # 指定与外界交互的端口9090
  8. EXPOSE 9090
  9. # 执行项目app.jar
  10. ENTRYPOINT ["java","-jar","app.jar"]

上传服务器

用工具将jar文件和Dockerfile文件上传到服务器的/workspace工作目录

  1. # 制作镜像
  2. [root@izwz9gvghuefob3ifhv3ppz docker-web]# docker build -t ronan-docker-web .
  3. Sending build context to Docker daemon 19.52MB
  4. Step 1/5 : FROM java:8
  5. ---> d23bdf5b1b1b
  6. Step 2/5 : VOLUME /tmp
  7. ---> Running in 76e275045769
  8. Removing intermediate container 76e275045769
  9. ---> 4493e9815453
  10. Step 3/5 : ADD ronan-docker-web.jar app.jar
  11. ---> 4de7c3e65497
  12. Step 4/5 : EXPOSE 9090
  13. ---> Running in efae4a77e05b
  14. Removing intermediate container efae4a77e05b
  15. ---> cba2a305f4e2
  16. Step 5/5 : ENTRYPOINT ["java","-jar","app.jar"]
  17. ---> Running in 45641296e12e
  18. Removing intermediate container 45641296e12e
  19. ---> 1a8a3e60bd47
  20. Successfully built 1a8a3e60bd47
  21. Successfully tagged ronan-docker-web:latest
  22. # 查看镜像
  23. [root@izwz9gvghuefob3ifhv3ppz docker-web]# docker images
  24. REPOSITORY TAG IMAGE ID CREATED SIZE
  25. ronan-docker-web latest 1a8a3e60bd47 19 seconds ago 663MB
  26. java 8 d23bdf5b1b1b 5 years ago 643MB
  27. # 创建容器
  28. [root@izwz9gvghuefob3ifhv3ppz docker-web]# docker run -itd --name ronan-docker-web-1 -p 9090:9090 ronan-docker-web:latest
  29. 0d0ce82668e3f01f8022e013383018db4ca54195af00de9af74385e7342a4324
  30. # 查看容器
  31. [root@izwz9gvghuefob3ifhv3ppz docker-web]# docker ps -a
  32. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  33. 0d0ce82668e3 ronan-docker-web:latest "java -jar app.jar" 6 seconds ago Up 5 seconds 0.0.0.0:9090->9090/tcp ronan-docker-web-1
  34. # 进入容器
  35. [root@izwz9gvghuefob3ifhv3ppz docker-web]# docker exec -it 0d0ce82668e3 bash
  36. root@0d0ce82668e3:/# ls -l
  37. total 19124
  38. -rw-r--r-- 1 root root 19515332 May 9 16:27 app.jar
  39. drwxr-xr-x 1 root root 4096 Jan 17 2017 bin
  40. drwxr-xr-x 2 root root 4096 Dec 28 2016 boot
  41. drwxr-xr-x 5 root root 360 May 10 15:25 dev
  42. drwxr-xr-x 1 root root 4096 May 10 15:25 etc
  43. drwxr-xr-x 2 root root 4096 Dec 28 2016 home
  44. drwxr-xr-x 1 root root 4096 Jan 17 2017 lib
  45. drwxr-xr-x 2 root root 4096 Jan 16 2017 lib64
  46. drwxr-xr-x 2 root root 4096 Jan 16 2017 media
  47. drwxr-xr-x 2 root root 4096 Jan 16 2017 mnt
  48. drwxr-xr-x 2 root root 4096 Jan 16 2017 opt
  49. dr-xr-xr-x 92 root root 0 May 10 15:25 proc
  50. drwx------ 2 root root 4096 Jan 16 2017 root
  51. drwxr-xr-x 3 root root 4096 Jan 16 2017 run
  52. drwxr-xr-x 2 root root 4096 Jan 16 2017 sbin
  53. drwxr-xr-x 2 root root 4096 Jan 16 2017 srv
  54. dr-xr-xr-x 13 root root 0 Jun 29 2021 sys
  55. drwxrwxrwt 5 root root 4096 May 10 15:25 tmp
  56. drwxr-xr-x 1 root root 4096 Jan 17 2017 usr
  57. drwxr-xr-x 1 root root 4096 Jan 17 2017 var

测试

  1. # 请求
  2. ronan@luonandeMacBook-Pro ~ % curl http://120.77.35.233:9090/hello
  3. hello world, docker!

总结

本文是一个Docker部署SpringBoot项目的示例,万变不离其宗。