当前位置:   article > 正文

Dockerfile - 基于 SpringBoot 项目自定义镜像(项目上线全过程)_springboot dockerfile编写

springboot dockerfile编写

 

目录

一、Dockerfile 自定义项目镜像

1.1、创建 SpringBoot 项目并编写

1.2、打包项目(jar)

1.3、编写 Dockerfile 文件,构建镜像

1.4、运行镜像并测试


一、Dockerfile 自定义项目镜像


1.1、创建 SpringBoot 项目并编写

a)简介:就是一个对 用户表 简单的增删改查的 SpringBoot 项目.

b)接口:采用 restful 风格

这里简单回顾以下 restful 风格接口规范

  • 原则: GET(查询)、POST(添加)、PUT(全字段更新)、PATCH(部分字段更新)、DELETE(删除)
  • 使用复数名词: user -> users、 car -> cars
  • 请求和响应指定: request: @RequestBody; response: @ResponseBody
  • 资源唯一标识需要通过路径传参,例如 id
  1. @RestController
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @Autowired
  5. private UserService userService;
  6. @PutMapping("id")
  7. public String updateUser(@PathVariable("id") Integer id, @RequestBody User user) {
  8. user.setId(id);
  9. userService.update(user);
  10. return "ok";
  11. }
  12. @GetMapping("/{id}")
  13. public User getUser(@PathVariable("id") Integer id) {
  14. return userService.queryById(id);
  15. }
  16. @PostMapping
  17. public String addUser(@RequestBody User user) {
  18. userService.insert(user);
  19. return "ok";
  20. }
  21. @DeleteMapping("/{id}")
  22. public String delUser(@PathVariable("id") Integer id) {
  23. userService.deleteById(id);
  24. return "ok";
  25. }
  26. @GetMapping
  27. public List<User> getUserList() {
  28. return userService.queryAll();
  29. }
  30. }

c)配置文件:我们重点关注 服务器端口号 和 mysql 连接 ip 地址

  1. server:
  2. port: 8081
  3. spring:
  4. datasource:
  5. url: jdbc:mysql://mysql:3306/demo?characterEncoding=utf8&useSSL=false
  6. username: root
  7. password: 1111
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. # 时间处理
  10. jackson:
  11. date-format: yyyy-MM-dd
  12. time-zone: GMT+8
  13. # mybatis xml save path
  14. mybatis:
  15. mapper-locations: classpath:mapper/*Mapper.xml
  16. configuration:
  17. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Ps:由于将来会将 springboot 项目和 mysql 配置到同一个自定义的 docker 网络下,因此 mysql 的 url 配置中的 ip 就使用容器名称即可.

1.2、打包项目(jar)

a)打包完毕后,根据以下目录找到包地址

或者在 target 的目录下也能看到

b)在云服务器上创建一个工作目录,用来存放 jar包 和 Dockerfile文件,将 jar 包从主机移到远端服务器.

  1. [root@VM-8-17-centos apps]# ls
  2. ssm-docker-0.0.1-SNAPSHOT.jar
  3. [root@VM-8-17-centos apps]# pwd
  4. /root/cyk/apps
  5. [root@VM-8-17-centos apps]#

1.3、编写 Dockerfile 文件,构建镜像

a)选取 openjdk1.8 镜像作为根基(open 表示开源的意思)

在 dockerhub 官网搜索 openjdk,找到对应的版本

找到 8-jdk

可以看到有下载命令,将 openjdk:8-jdk 写到 FROM 后面,表示以此镜像为基础进行开发.

b)编写 Dockerfile 文件

  1. FROM openjdk:8-jdk
  2. ENV BASE_PATH=/cyk/apps
  3. WORKDIR $BASE_PATH
  4. # 将宿主机上的 ssm-docker-0.0.1-SNAPSHOT.jar 移动到 /cyk/apps 目录下并改名为 apps.jar
  5. ADD ssm-docker-0.0.1-SNAPSHOT.jar $BASE_PATH/apps.jar
  6. EXPOSE 8081
  7. ENTRYPOINT ["java", "-jar"]
  8. CMD ["apps.jar"]

c)将 Dockerfile 文件移动到云服务器的工作目录( /cyk/apps )

  1. [root@VM-8-17-centos apps]# rz -E
  2. rz waiting to receive.
  3. [root@VM-8-17-centos apps]# ls
  4. Dockerfile ssm-docker-0.0.1-SNAPSHOT.jar
  5. [root@VM-8-17-centos apps]# pwd
  6. /root/cyk/apps
  7. [root@VM-8-17-centos apps]#

d)构建镜像

  1. [root@VM-8-17-centos apps]# docker build -t app:1.0 .
  2. [+] Building 46.7s (8/8) FINISHED docker:default
  3. => [internal] load build definition from Dockerfile 0.0s
  4. => => transferring dockerfile: 326B 0.0s
  5. => [internal] load .dockerignore 0.0s
  6. => => transferring context: 2B 0.0s
  7. => [internal] load metadata for docker.io/library/openjdk:8-jdk 16.2s
  8. => [1/3] FROM docker.io/library/openjdk:8-jdk@sha256:8a9d5c43f540e8d0c003c723a2c8bd20ae350a2efed6fb5719cae33b026f8e7c 30.0s
  9. => => resolve docker.io/library/openjdk:8-jdk@sha256:8a9d5c43f540e8d0c003c723a2c8bd20ae350a2efed6fb5719cae33b026f8e7c 0.0s
  10. => => sha256:e24ac15e052e04a3462ef4984b5d83214f7f65c06f54acd3745a1926e226be16 7.81kB / 7.81kB 0.0s
  11. => => sha256:9b829c73b52b92b97d5c07a54fb0f3e921995a296c714b53a32ae67d19231fcd 5.15MB / 5.15MB 0.9s
  12. => => sha256:cb5b7ae361722f070eca53f35823ed21baa85d61d5d95cd5a95ab53d740cdd56 10.87MB / 10.87MB 0.7s
  13. => => sha256:8a9d5c43f540e8d0c003c723a2c8bd20ae350a2efed6fb5719cae33b026f8e7c 1.29kB / 1.29kB 0.0s
  14. => => sha256:9413213335131c4e06b21a8e379bd9b6a20afcd6b762540463d5f7c72942dcdd 1.79kB / 1.79kB 0.0s
  15. => => sha256:0e29546d541cdbd309281d21a73a9d1db78665c1b95b74f32b009e0b77a6e1e3 54.92MB / 54.92MB 18.6s
  16. => => sha256:6494e4811622b31c027ccac322ca463937fd805f569a93e6f15c01aade718793 54.57MB / 54.57MB 21.5s
  17. => => sha256:668f6fcc5fa5532a1dd793456f64daf954192e0521fd65d42af584d5e2d93f55 5.42MB / 5.42MB 1.3s
  18. => => sha256:c0879393b07ef5fa816c292b00e3eb4945890bc2a69ab0d1754240cbe9cedf21 212B / 212B 1.6s
  19. => => sha256:bef50c41a74d450f2d708be5971c3ba635ed1a714af7f4fa1497886adb2fa734 106.00MB / 106.00MB 12.4s
  20. => => extracting sha256:0e29546d541cdbd309281d21a73a9d1db78665c1b95b74f32b009e0b77a6e1e3 3.1s
  21. => => extracting sha256:9b829c73b52b92b97d5c07a54fb0f3e921995a296c714b53a32ae67d19231fcd 0.3s
  22. => => extracting sha256:cb5b7ae361722f070eca53f35823ed21baa85d61d5d95cd5a95ab53d740cdd56 0.3s
  23. => => extracting sha256:6494e4811622b31c027ccac322ca463937fd805f569a93e6f15c01aade718793 3.2s
  24. => => extracting sha256:668f6fcc5fa5532a1dd793456f64daf954192e0521fd65d42af584d5e2d93f55 0.2s
  25. => => extracting sha256:c0879393b07ef5fa816c292b00e3eb4945890bc2a69ab0d1754240cbe9cedf21 0.0s
  26. => => extracting sha256:bef50c41a74d450f2d708be5971c3ba635ed1a714af7f4fa1497886adb2fa734 3.3s
  27. => [internal] load build context 0.1s
  28. => => transferring context: 9.88kB 0.0s
  29. => [2/3] WORKDIR /cyk/apps 0.2s
  30. => [3/3] ADD ssm-docker-0.0.1-SNAPSHOT.jar /cyk/apps/apps.jar 0.1s
  31. => exporting to image 0.1s
  32. => => exporting layers 0.1s
  33. => => writing image sha256:89839bc7dd0c546892b523403eb18d8b3a5d84993c4be71a9dd4ca2875bd1e9d 0.0s
  34. => => naming to docker.io/library/app:1.0 0.0s
  35. [root@VM-8-17-centos apps]# docker images
  36. REPOSITORY TAG IMAGE ID CREATED SIZE
  37. app 1.0 89839bc7dd0c 28 seconds ago 526MB

1.4、运行镜像并测试

a)先自定义一个网桥,方便后续运行容器时直接加入到统一网络下.

  1. [root@VM-8-17-centos apps]# docker network create mynet
  2. dd8d11bdbd1ff64905a877c350752521f8dc772480b0a9db378769d283a143ac
  3. [root@VM-8-17-centos apps]#
  4. [root@VM-8-17-centos apps]#
  5. [root@VM-8-17-centos apps]# docker network ls
  6. NETWORK ID NAME DRIVER SCOPE
  7. f84ca6fc4b48 bridge bridge local
  8. a1b8a8a8e372 host host local
  9. dd8d11bdbd1f mynet bridge local
  10. 38b4ab15453c none null local

d)启动容器

启动 mysql:

docker run -d --name mysql -p 3306:3306 -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=1111 --network mynet mysql:5.7

Ps:这里不要忘了准备 MySQL 的数据 

启动 springboot 项目:

docker run -d --name app -p 8081:8081 --network mynet app:1.0

检查启动日志:

  1. [root@VM-8-17-centos apps]# docker logs -f app
  2. . ____ _ __ _ _
  3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  6. ' |____| .__|_| |_|_| |_\__, | / / / /
  7. =========|_|==============|___/=/_/_/_/
  8. :: Spring Boot :: (v2.7.6)
  9. 2023-12-28 10:40:00.259 INFO 1 --- [ main] com.cyk.SsmDockerApplication : Starting SsmDockerApplication using Java 1.8.0_312 on 4ecafb679d7f with PID 1 (/apps/apps.jar started by root in /apps)
  10. 2023-12-28 10:40:00.273 INFO 1 --- [ main] com.cyk.SsmDockerApplication : No active profile set, falling back to 1 default profile: "default"
  11. 2023-12-28 10:40:02.271 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
  12. 2023-12-28 10:40:02.290 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  13. 2023-12-28 10:40:02.290 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.69]
  14. 2023-12-28 10:40:02.391 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  15. 2023-12-28 10:40:02.391 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1959 ms
  16. Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
  17. Parsed mapper file: 'class path resource [mapper/UserMapper.xml]'
  18. 2023-12-28 10:40:03.399 INFO 1 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
  19. 2023-12-28 10:40:03.675 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
  20. 2023-12-28 10:40:03.690 INFO 1 --- [ main] com.cyk.SsmDockerApplication : Started SsmDockerApplication in 4.412 seconds (JVM running for 5.138)

 

e)注意事项:运行容器时,一旦出现以下情况

no main manifest attribute, in apps.jar

一定要看一下 pom.xml 文件中是否存在 <skip>true<skip> 的配置,因为打包时他会跳过主类.

f)postman 测试接口

这里我为了防止黑客攻击,建立隧道和远端服务器连接,开放映射本地 9091 端口.

访问后,云服务器上检查日志,也可以观察到打印相应日志(测试成功)

  1. [root@VM-8-17-centos ~]# docker logs app
  2. . ____ _ __ _ _
  3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  6. ' |____| .__|_| |_|_| |_\__, | / / / /
  7. =========|_|==============|___/=/_/_/_/
  8. :: Spring Boot :: (v2.7.6)
  9. 2023-12-28 10:40:00.259 INFO 1 --- [ main] com.cyk.SsmDockerApplication : Starting SsmDockerApplication using Java 1.8.0_312 on 4ecafb679d7f with PID 1 (/apps/apps.jar started by root in /apps)
  10. 2023-12-28 10:40:00.273 INFO 1 --- [ main] com.cyk.SsmDockerApplication : No active profile set, falling back to 1 default profile: "default"
  11. 2023-12-28 10:40:02.271 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
  12. 2023-12-28 10:40:02.290 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  13. 2023-12-28 10:40:02.290 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.69]
  14. 2023-12-28 10:40:02.391 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  15. 2023-12-28 10:40:02.391 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1959 ms
  16. Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
  17. Parsed mapper file: 'class path resource [mapper/UserMapper.xml]'
  18. 2023-12-28 10:40:03.399 INFO 1 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
  19. 2023-12-28 10:40:03.675 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
  20. 2023-12-28 10:40:03.690 INFO 1 --- [ main] com.cyk.SsmDockerApplication : Started SsmDockerApplication in 4.412 seconds (JVM running for 5.138)
  21. 2023-12-28 10:43:17.431 INFO 1 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
  22. 2023-12-28 10:43:17.431 INFO 1 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  23. 2023-12-28 10:43:17.432 INFO 1 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
  24. Creating a new SqlSession
  25. SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@45f8529a] was not registered for synchronization because synchronization is not active
  26. 2023-12-28 10:43:17.524 INFO 1 --- [nio-8081-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
  27. 2023-12-28 10:43:17.819 INFO 1 --- [nio-8081-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
  28. JDBC Connection [HikariProxyConnection@1585874780 wrapping com.mysql.cj.jdbc.ConnectionImpl@1380045] will not be managed by Spring
  29. ==> Preparing: select * from user;
  30. ==> Parameters:
  31. <== Columns: id, name, age
  32. <== Row: 1, cyk, 20
  33. <== Row: 2, lyj, 19
  34. <== Row: 4, pdder, 100
  35. <== Total: 3
  36. Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@45f8529a]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/1007931
推荐阅读
相关标签
  

闽ICP备14008679号