Spring Boot项目打包Docker镜像

1.编写 Dockerfile 文件

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/xiaotong_receive_data_service-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} xiaotong_receive_data_service-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/xiaotong_receive_data_service-0.0.1-SNAPSHOT.jar"]

2.加入 spring boot 根路径, 进行 mvn package 打包

在这里插入图片描述

3.打包上传项目并解压

drwxr-xr-x   4 root root     4096 Mar 11 21:14 xiaotong_receive_data_service
-rw-r--r--   1 root root 64181817 Mar 11 20:21 xiaotong_receive_data_service.zip
drwxr-xr-x   5 root root     4096 Mar 11 22:21 xiaotong_receive_data_web
-rw-r--r--   1 root root 44638431 Mar 11 20:21 xiaotong_receive_data_web.zip

4.进入 xiaotong_receive_data_web 项目目录执行打包命令、查看镜像、运行容器、查看容器、根据容器ID导出镜像文件 xiaotong_receive_data_web.tar

[root@rensun /]# cd xiaotong_receive_data_web
[root@rensun xiaotong_receive_data_web]# ll
total 36
-rw-r--r-- 1 root root   230 Mar 11 19:55 Dockerfile
drwxr-xr-x 2 root root  4096 Mar  4 11:22 logs
-rw-r--r-- 1 root root  1790 Mar  9 01:52 pom.xml
drwxr-xr-x 4 root root  4096 Jan 14 15:23 src
drwxr-xr-x 8 root root  4096 Mar 11 20:18 target
-rw-r--r-- 1 root root 14013 Mar  9 01:53 xiaotong_receive_data_web.iml
[root@rensun xiaotong_receive_data_web]# docker build  -t docker.io/receive-data.io:xiaotong_receive_data_web .
Sending build context to Docker daemon  50.7 MB
Step 1/4 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/4 : ARG JAR_FILE=target/xiaotong_receive_data_web-0.0.1-SNAPSHOT.jar
 ---> Using cache
 ---> 76283f52d6e7
Step 3/4 : COPY ${JAR_FILE} xiaotong_receive_data_web-0.0.1-SNAPSHOT.jar
 ---> Using cache
 ---> 4364a9bae961
Step 4/4 : ENTRYPOINT java -jar /xiaotong_receive_data_web-0.0.1-SNAPSHOT.jar
 ---> Using cache
 ---> deb721008ea6
Successfully built deb721008ea6
[root@rensun xiaotong_receive_data_web]# docker image ls 
REPOSITORY                      TAG                             IMAGE ID            CREATED             SIZE
docker.io/receive-data.io       xiaotong_receive_data_service   26b876d48521        About an hour ago   176 MB
docker.io/receive-data.io       xiaotong_receive_data_web       deb721008ea6        About an hour ago   155 MB
docker.io/portainer/portainer   latest                          62771b0b9b09        7 months ago        79.1 MB
docker.io/openjdk               8-jdk-alpine                    a3562aa0b991        22 months ago       105 MB
[root@rensun xiaotong_receive_data_web]# docker run docker.io/receive-data.io:xiaotong_receive_data_web

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.0.RELEASE)

2021-03-11 14:31:01.937 -- [main] INFO  c.x.r.d.XiaotongReceiveDataWebApplication.logStarting - Starting XiaotongReceiveDataWebApplication v0.0.1-SNAPSHOT on 3f62c734a446 with PID 1 (/xiaotong_receive_data_web-0.0.1-SNAPSHOT.jar started by root in /)
2021-03-11 14:31:01.964 -- [main] INFO  c.x.r.d.XiaotongReceiveDataWebApplication.logStartupProfileInfo - No active profile set, falling back to default profiles: default
2021-03-11 14:31:04.561 -- [main] INFO  o.s.b.w.e.tomcat.TomcatWebServer.initialize - Tomcat initialized with port(s): 2021 (http)
2021-03-11 14:31:04.595 -- [main] INFO  o.a.coyote.http11.Http11NioProtocol.log - Initializing ProtocolHandler ["http-nio-2021"]
2021-03-11 14:31:04.596 -- [main] INFO  o.a.catalina.core.StandardService.log - Starting service [Tomcat]
2021-03-11 14:31:04.598 -- [main] INFO  o.a.catalina.core.StandardEngine.log - Starting Servlet engine: [Apache Tomcat/9.0.27]
2021-03-11 14:31:04.778 -- [main] INFO  o.a.c.c.C.[.[.[/xiaotong-ai-web-data].log - Initializing Spring embedded WebApplicationContext
2021-03-11 14:31:04.779 -- [main] INFO  o.s.web.context.ContextLoader.prepareWebApplicationContext - Root WebApplicationContext: initialization completed in 2698 ms
2021-03-11 14:31:05.496 -- [main] INFO  o.h.validator.internal.util.Version.<clinit> - HV000001: Hibernate Validator 6.0.17.Final
2021-03-11 14:31:06.144 -- [main] INFO  o.s.s.c.ThreadPoolTaskExecutor.initialize - Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 14:31:06.573 -- [main] INFO  o.a.coyote.http11.Http11NioProtocol.log - Starting ProtocolHandler ["http-nio-2021"]
2021-03-11 14:31:06.672 -- [main] INFO  o.s.b.w.e.tomcat.TomcatWebServer.start - Tomcat started on port(s): 2021 (http) with context path '/xiaotong-ai-web-data'
2021-03-11 14:31:06.685 -- [main] INFO  c.x.r.d.XiaotongReceiveDataWebApplication.logStarted - Started XiaotongReceiveDataWebApplication in 5.998 seconds (JVM running for 7.082)
[root@rensun xiaotong_receive_data_web]# docker ps -a
CONTAINER ID        IMAGE                                                     COMMAND                  CREATED             STATUS                           PORTS                    NAMES
3f62c734a446        docker.io/receive-data.io:xiaotong_receive_data_web       "java -jar /xiaoto..."   2 minutes ago       Exited (130) 34 seconds ago                               ecstatic_mestorf
7bd9d40daa65        mysql:5.7.11                                              "docker-entrypoint..."   2 months ago        Exited (0) 2 hours ago                                    mysql-lv
8107acbd998b        portainer/portainer                                       "/portainer"             4 months ago        Up About an hour                 0.0.0.0:9000->9000/tcp   docker-web
[root@rensun xiaotong_receive_data_web]# docker export 3f62c734a446 > xiaotong_receive_data_web.tar
[root@rensun xiaotong_receive_data_web]# ll
total 152996
-rw-r--r-- 1 root root       230 Mar 11 19:55 Dockerfile
drwxr-xr-x 2 root root      4096 Mar  4 11:22 logs
-rw-r--r-- 1 root root      1790 Mar  9 01:52 pom.xml
drwxr-xr-x 4 root root      4096 Jan 14 15:23 src
drwxr-xr-x 8 root root      4096 Mar 11 20:18 target
-rw-r--r-- 1 root root     14013 Mar  9 01:53 xiaotong_receive_data_web.iml
-rw-r--r-- 1 root root 156630016 Mar 11 22:37 xiaotong_receive_data_web.tar
[root@rensun xiaotong_receive_data_web]# 

5.镜像上传部署服务器,导入镜像运行容器

[root@rensun local]# ll
total 153020
drwxr-xr-x   7 root root      4096 Feb 24 04:01 aegis
drwxr-xr-x.  2 root root      4096 Apr 11  2018 bin
drwxr-xr-x.  2 root root      4096 Apr 11  2018 etc
drwxr-xr-x.  2 root root      4096 Apr 11  2018 games
drwxr-xr-x.  2 root root      4096 Apr 11  2018 include
drwxr-xr-x   3 root root      4096 Nov 10 14:00 install_package
drwxr-xr-x   3 root root      4096 Nov  9 15:32 java
drwxr-xr-x.  2 root root      4096 Apr 11  2018 lib
drwxr-xr-x.  2 root root      4096 Apr 11  2018 lib64
drwxr-xr-x.  2 root root      4096 Apr 11  2018 libexec
drwxr-xr-x  12 root root      4096 Nov 10 18:44 nginx
drwxr-xr-x.  2 root root      4096 Apr 11  2018 sbin
drwxr-xr-x.  7 root root      4096 Jan 28 22:43 share
drwxr-xr-x.  2 root root      4096 Apr 11  2018 src
drwxr-xr-x   2 root root      4096 Nov 11 10:09 test
-rw-r--r--   1 root root 156630016 Mar 11 22:37 xiaotong_receive_data_web.tar
[root@rensun local]# docker import - xiaotong_receive_data_web < xiaotong_receive_data_web.tar 
sha256:db13725517354315ed9671ec94e0934b9f1e712bf867d732533caec167cdd874
[root@rensun local]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
xiaotong_receive_data_web       latest              db1372551735        10 seconds ago      155 MB
docker.io/portainer/portainer   latest              62771b0b9b09        7 months ago        79.1 MB
docker.io/openjdk               8-jdk-alpine        a3562aa0b991        22 months ago       105 MB

Alt

在这里插入图片描述

提示
本人以抱着学习的态度去分享,以上内容如有雷同,不胜荣幸!如有不足,欢迎评论留言!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值