当前位置: 首页 > web >正文

docker构建springboot镜像

先在Linux服务器安装docker

由于镜像现在访问不了,所以采用其它国内镜像的方法拉去镜像

直接运行该命令:

docker pull docker.1ms.run/library/openjdk:21

前面是镜像地址,可以替换的地址有:


https://docker.1ms.run    毫秒镜像    可用
https://docker.xuanyuan.me    轩辕镜像    可用
https://dislabaiot.xyz    -    可用
https://docker.sunzishaokao.com    云港网络镜像    5.26不可用
https://hub.rat.dev    耗子面板    可用
https://doublezonline.cloud    -    可用
https://dislabaiot.xyz    -    可用
https://xdark.top    -    可用

注意不要前面的https,library得有,看运气有时候能拉去下来

Idea点击maven中的build生成xxx.jar文件,将jar包复制到Linux系统中重命名为app.jar,写一个Dockerfile文件

注意文件名称就叫Dockerfile没有文件后缀,内容如下


FROM docker.1ms.run/library/openjdk:21WORKDIR /appCOPY app.jar app.jarEXPOSE 8080ENTRYPOINT ["java", "-jar", "/app/app.jar"]

在Dockerfile目录下运行命令构建镜像

 docker build -t myjava .

看好命令最后面有个点,这个点不能没有


[root@localhost ~]# docker build -t myjava .
[+] Building 1.5s (8/8) FINISHED                                                                                  docker:default=> [internal] load build definition from Dockerfile                                                                        0.0s=> => transferring dockerfile: 233B                                                                                        0.0s=> [internal] load metadata for docker.1ms.run/library/openjdk:21                                                          0.0s=> [internal] load .dockerignore                                                                                           0.0s=> => transferring context: 2B                                                                                             0.0s=> [1/3] FROM docker.1ms.run/library/openjdk:21                                                                            0.0s=> [internal] load build context                                                                                           0.0s=> => transferring context: 91B                                                                                            0.0s=> [2/3] WORKDIR /app                                                                                                      0.0s=> [3/3] COPY app.jar app.jar                                                                                              0.7s=> exporting to image                                                                                                      0.6s=> => exporting layers                                                                                                     0.6s=> => writing image sha256:f568f3d1d78eb15bf061c0f80b63b48815ce7dee46f2ff2e6ddc99a97db08565                                0.0s=> => naming to docker.io/library/myjava                                                                                   0.0s
[

顺利的话直接构建成功

在docker中运行该Image

 docker run -d -p 8080:8080 myjava:latest

使用docker logs看到日志输出,镜像构建成功!

.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v3.4.7)2025-07-19T14:55:49.375Z  INFO 1 --- [demo1] [           main] org.example.demo1.Demo1Application       : Starting Demo1Application v0.0.1-SNAPSHOT using Java 21 with PID 1 (/app/app.jar started by root in /app)
2025-07-19T14:55:49.393Z  INFO 1 --- [demo1] [           main] org.example.demo1.Demo1Application       : The following 1 profile is active: "pro"
2025-07-19T14:55:55.771Z  INFO 1 --- [demo1] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-07-19T14:55:55.865Z  INFO 1 --- [demo1] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-07-19T14:55:55.868Z  INFO 1 --- [demo1] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.42]
2025-07-19T14:55:56.542Z  INFO 1 --- [demo1] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-07-19T14:55:56.556Z  INFO 1 --- [demo1] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 6788 ms
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Get /172.17.0.2 network interface
Get network interface info: name:eth0 (eth0)
Initialization Sequence datacenterId:9 workerId:24_ _   |_  _ _|_. ___ _ |    _
| | |\/|_)(_| | |_\  |_)||_|_\/               |3.5.12
2025-07-19T14:56:07.301Z  INFO 1 --- [demo1] [           main] m.e.s.MybatisPlusApplicationContextAware : Register ApplicationContext instances org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1722011b
2025-07-19T14:56:08.166Z  INFO 1 --- [demo1] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-07-19T14:56:08.222Z  INFO 1 --- [demo1] [           main] org.example.demo1.Demo1Application       : Started Demo1Application in 21.389 seconds (process running for 23.923)
^C2025-07-19T15:03:00.791Z  INFO 1 --- [demo1] [ionShutdownHook] o.s.b.w.e.tomcat.GracefulShutdown        : Commencing graceful shutdown. Waiting for active requests to complete

浏览器访问http://192.168.68.29:8080/

访问成功!

还有好多细节:例如如果使用了mysql需要先运行mysql容器再启动springboot镜像,并且注意ip地址,如果想用mysql主机名需要指定网络alias。Linux服务器需要关闭防火墙或者放行8080端口等等。

http://www.xdnf.cn/news/15910.html

相关文章:

  • 数据结构之图
  • 【办公类-107-02】20250719视频MP4转gif(削减MB)
  • MyBatis分页神器PageHelper深度解析
  • 深入解析文件操作(上)- 二进制文件和文本文件,流的概念,文件的打开和关闭
  • 计算机网络1.1:计算机网络在信息时代的作用
  • Redis常见线上问题
  • Javascript进程和线程通信
  • VIT速览
  • Nestjs框架: RxJS 核心方法实践与错误处理详解
  • XSS漏洞----基于Dom的xss
  • 混沌趋势指标原理及交易展示
  • python爬虫之获取渲染代码
  • Python 数据分析模板在工程实践中的问题诊断与系统性解决方案
  • 探索量子计算与法律理论的交叉领域
  • Zephyr环境搭建 - Board GD32A503
  • 力扣 hot100 Day49
  • 数据集下载网站
  • XSS漏洞知识总结
  • [spring6: AspectMetadata AspectInstanceFactory]-源码解析
  • PCIe RAS学习专题(3):AER内核处理流程梳理
  • 消息队列:数字化通信的高效纽带
  • 1009 - 数组逆序
  • Spring监听器
  • 2.4 组件间通信Props(父传子)
  • Rust Web 全栈开发(九):增加教师管理功能
  • 【SVM smote】MAP - Charting Student Math Misunderstandings
  • Custom SRP - Custom Render Pipeline
  • RabbitMQ01——基础概念、docker配置rabbitmq、内部执行流程、五种消息类型、测试第一种消息类型
  • RabbitMQ—事务与消息分发
  • 软考 系统架构设计师系列知识点之杂项集萃(113)