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:21

WORKDIR /app

COPY app.jar app.jar

EXPOSE 8080

ENTRYPOINT ["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端口等等。

你可能感兴趣的:(Spring,Docker,Springboot,docker,spring,boot,java)