• docker 部署 springboot项目和直接运行 jar 文件详细步骤


    目前docker发布springboot有两种方式,一种是制作镜像,另一种是直接运行jar 文件



    第一种使用镜像来部署



    一、创建项目目录

    
    [root@dex ~]# mkdir /opt/backstage_proj/leshan
    
    
    

    二、安装jdk

    这里直接使用docker pull一个openjdk镜像下来就ok了(比宿主机安装省事)

    
    	[root@dex backstage_proj]# docker pull docker.io/openjdk:8
    	8: Pulling from library/openjdk
    	146bd6a88618: Downloading [=============>                                     ]  9.337MB/33.72MB
    	146bd6a88618: Downloading [==============>                                    ]  9.682MB/33.72MB
    	db0efb86e806: Download complete 
    	146bd6a88618: Pull complete 
    	9935d0c62ace: Pull complete 
    	db0efb86e806: Pull complete 
    	e705a4c4fd31: Pull complete 
    	3d3bf7f7e874: Pull complete 
    	49371c5b9ff6: Pull complete 
    	e7873a7ca0fd: Pull complete 
    	Digest: sha256:de5312e3f2abecaafa3a58ae1752595880366ac616847bd23ee8cb2fb984b7be
    	Status: Downloaded newer image for openjdk:8
    	docker.io/library/openjdk:8
    	
    	[root@dex backstage_proj]# docker images
    	REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    	openjdk             8                   f8146facf376        9 days ago          488MB
    	vuedev              v1                  1e9d51839b07        10 days ago         1.14GB
    	node                latest              2af77b226ea7        4 weeks ago         934MB
    	[root@dex backstage_proj]# 
    
    
    

    三、配置Dockerfile

    
    	[root@dex ~]# cd /opt/backstage_proj/leshan
    	[root@dex leshan]# vi Dockerfile 
    	
    	FROM docker.io/openjdk:8
    	
    	# author 
    	MAINTAINER dex
    	
    	# 将容器挂载如下目录
    	VOLUME /opt/backstage_proj/tmp
    	
    	#将jar 添加进来
    	ADD lszz-0.0.1-SNAPSHOT.jar leshan_bk.jar
    	
    	# 时区
    	RUN echo "Asia/Shanghai" > /etc/localtime_bak
    	
    	EXPOSE 8301
    	
    	ENTRYPOINT ["java","-jar","/leshan_bk.jar"]
    	
    	
    	~                                                                                                                                                                                         
    	~                                                                                                                                                                                         
    	~                                                                                                                                                                                         
    	~                                                                                                                                                                                         
    	~                                                                                                                                                                                         
    	~                                
    	:wq
    

    现在再次查看该目录下应该有如下两个文件

    
    	[root@dex ~]# ll /opt/backstage_proj/leshan/
    	total 68056
    	-rw-r--r-- 1 root root      291 Jan  6 21:39 Dockerfile
    	-rw-r--r-- 1 root root 69681782 Jan  6 16:51 lszz-0.0.1-SNAPSHOT.jar
    	[root@dex ~]# 
    
    

    四、制作镜像

    
    	[root@dex leshan]# docker build -t leshan_bk .
    	Sending build context to Docker daemon  69.68MB
    	Step 1/7 : FROM docker.io/openjdk:8
    	 ---> f8146facf376
    	Step 2/7 : MAINTAINER dex
    	 ---> Running in b8f739ece4d5
    	Removing intermediate container b8f739ece4d5
    	 ---> a6bb80603e36
    	Step 3/7 : VOLUME /opt/backstage_proj/tmp
    	 ---> Running in 9a747c2b7b46
    	Removing intermediate container 9a747c2b7b46
    	 ---> 9a8a7ab25b22
    	Step 4/7 : ADD lszz-0.0.1-SNAPSHOT.jar leshan_bk.jar
    	 ---> 40af2b7a13f0
    	Step 5/7 : RUN echo "Asia/Shanghai" > /etc/localtime_bak
    	 ---> Running in 1eead173f57d
    	Removing intermediate container 1eead173f57d
    	 ---> 2ea15ba693a6
    	Step 6/7 : EXPOSE 8301
    	 ---> Running in e7a86d5ec4a6
    	Removing intermediate container e7a86d5ec4a6
    	 ---> 946784a2dbbf
    	Step 7/7 : ENTRYPOINT ["java","-jar","/leshan_bk.jar"]
    	 ---> Running in 3e03cf93379c
    	Removing intermediate container 3e03cf93379c
    	 ---> f068a314a3ce
    	Successfully built f068a314a3ce
    	Successfully tagged leshan_bk:latest
    	
    	[root@dex leshan]# docker images
    	REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    	leshan_bk           latest              f068a314a3ce        5 seconds ago       558MB
    	openjdk             8                   f8146facf376        9 days ago          488MB
    	vuedev              v1                  1e9d51839b07        11 days ago         1.14GB
    	node                latest              2af77b226ea7        4 weeks ago         934MB
    
    

    五、 运行容器

    
    	[root@dex leshan]# docker run -p 8301:8201 --name ls_bk -d leshan_bk
    	30bbc32a1e956d51e7d8f589611bb46b2ba85924435b79a22d9a0972f4fc5cb8
    
    

    六、查看运行的容器

    
    	[root@dex backstage_proj]# docker ps
    	CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
    	30bbc32a1e95        leshan_bk           "java -jar /leshan_b…"   2 minutes ago       Up 2 minutes        8301/tcp, 0.0.0.0:8301->8201/tcp   ls_bk
    	1719b24f00a7        vuedev:v1 
    	
    

    七、查看日志

    	
    	[root@dex leshan]# docker logs -f -t --tail 200 ls_bk
    	2020-01-06T13:42:56.203072863Z 
    	2020-01-06T13:42:56.203120909Z   .   ____          _            __ _ _
    	2020-01-06T13:42:56.203126684Z  /\ / ___'_ __ _ _(_)_ __  __ _    
    	2020-01-06T13:42:56.203130484Z ( ( )\___ | '_ | '_| | '_ / _` |    
    	2020-01-06T13:42:56.203134006Z  \/  ___)| |_)| | | | | || (_| |  ) ) ) )
    	2020-01-06T13:42:56.203137398Z   '  |____| .__|_| |_|_| |_\__, | / / / /
    	2020-01-06T13:42:56.203140655Z  =========|_|==============|___/=/_/_/_/
    	2020-01-06T13:42:56.206018092Z  :: Spring Boot ::        (v2.2.1.RELEASE)
    	2020-01-06T13:42:56.206039877Z 
    	2020-01-06T13:42:56.445091306Z 2020-01-06 13:42:56.437  INFO 1 --- [           main] com.hy.lszz.LszzApplication              : Starting LszzApplication v0.0.1-SNAPSHOT on 30bbc32a1e95 with PID 1 (/leshan_bk.jar started by root in /)
    	2020-01-06T13:42:56.449224223Z 2020-01-06 13:42:56.447  INFO 1 --- [           main] com.hy.lszz.LszzApplication              : No active profile set, falling back to default profiles: default
    	2020-01-06T13:42:57.621084992Z 2020-01-06 13:42:57.619  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
    	2020-01-06T13:42:57.844092185Z 2020-01-06 13:42:57.843  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 210ms. Found 20 repository interfaces.
    	2020-01-06T13:42:58.469091849Z 2020-01-06 13:42:58.467  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    	2020-01-06T13:42:58.962099880Z 2020-01-06 13:42:58.961  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8201 (http)
    	2020-01-06T13:42:58.979086214Z 2020-01-06 13:42:58.976  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    	2020-01-06T13:42:58.979123120Z 2020-01-06 13:42:58.976  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
    	2020-01-06T13:42:59.045084346Z 2020-01-06 13:42:59.044  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    	2020-01-06T13:42:59.050105587Z 2020-01-06 13:42:59.044  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2493 ms
    	2020-01-06T13:42:59.532109116Z 2020-01-06 13:42:59.527  INFO 1 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
    	2020-01-06T13:42:59.958087543Z 2020-01-06 13:42:59.953  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
    	2020-01-06T13:43:00.035740586Z 2020-01-06 13:43:00.035  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.4.8.Final}
    	2020-01-06T13:43:00.221403157Z 2020-01-06 13:43:00.220  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
    	2020-01-06T13:43:00.363048765Z 2020-01-06 13:43:00.361  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
    	2020-01-06T13:43:07.845655620Z 2020-01-06 13:43:07.845  INFO 1 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
    	2020-01-06T13:43:07.853288330Z 2020-01-06 13:43:07.852  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
    	2020-01-06T13:43:09.793091279Z 2020-01-06 13:43:09.789  WARN 1 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
    	2020-01-06T13:43:09.997086734Z 2020-01-06 13:43:09.994  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    	2020-01-06T13:43:10.561114670Z 2020-01-06 13:43:10.558  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8201 (http) with context path ''
    	2020-01-06T13:43:10.561149205Z 2020-01-06 13:43:10.560  INFO 1 --- [           main] com.hy.lszz.LszzApplication              : Started LszzApplication in 14.983 seconds (JVM running for 15.579)
    
    
    

    八、nginx配置反向代理

    
          location /lsBk/ {
    		proxy_pass http://127.0.0.1:8301/;
    	}
    
    

    页面就可以使用 域名+ /lsBk/ + 接口 进行访问了在这里插入图片描述



    第二种直接运行 jar文件



    一、docker直接运行jar

    有的时候将springboot 打包镜像然后运行容器比较麻烦,这个较适用于项目上线发布,在平时测试还是使用docker 直接运行jar 较方便

    • 1、先停掉刚才的docker 容器
    
    	[root@dex nginx]# docker ps
    	CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
    	30bbc32a1e95        leshan_bk           "java -jar /leshan_b…"   30 minutes ago      Up 30 minutes       8301/tcp, 0.0.0.0:8301->8201/tcp   ls_bk
    	1719b24f00a7        vuedev:v1           "docker-entrypoint.s…"   11 days ago         Up 11 days          9009/tcp, 0.0.0.0:8191->8009/tcp   vuedev2
    	[root@dex nginx]# docker stop 30b
    	30b
    	[root@dex nginx]# docker ps
    	CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
    	1719b24f00a7        vuedev:v1           "docker-entrypoint.s…"   11 days ago         Up 11 days          9009/tcp, 0.0.0.0:8191->8009/tcp   vuedev2
    	[root@dex nginx]# 
    
    
    • 2、执行run命令
    
    	[root@dex nginx]# docker run -p 8301:8201 -v /opt/backstage_proj/leshan/lszz-0.0.1-SNAPSHOT.jar:/opt/backsttage_proj/tmp02/lszz-0.0.1-SNAPSHOT.jar --name ls_bk02 docker.io/openjdk:8 java -jar /opt/backstage_proj/tmp02/lszz-0.0.1-SNAPSHOT.jar
    
      .   ____          _            __ _ _
    	 /\ / ___'_ __ _ _(_)_ __  __ _    
    	( ( )\___ | '_ | '_| | '_ / _` |    
    	 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
    	  '  |____| .__|_| |_|_| |_\__, | / / / /
    	 =========|_|==============|___/=/_/_/_/
    	 :: Spring Boot ::        (v2.2.1.RELEASE)
    	
    	2020-01-06 14:20:00.979  INFO 1 --- [           main] com.hy.lszz.LszzApplication              : Starting LszzApplication v0.0.1-SNAPSHOT on 736586c25763 with PID 1 (/opt/backstage_proj/tmp02/lszz-0.0.1-SNAPSHOT.jar started by root in /)
    	2020-01-06 14:20:00.984  INFO 1 --- [           main] com.hy.lszz.LszzApplication              : No active profile set, falling back to default profiles: default
    	2020-01-06 14:20:02.468  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
    	2020-01-06 14:20:02.724  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 240ms. Found 20 repository interfaces.
    	2020-01-06 14:20:03.352  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    	2020-01-06 14:20:03.848  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8201 (http)
    	2020-01-06 14:20:03.864  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    	2020-01-06 14:20:03.864  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
    	2020-01-06 14:20:03.957  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    	2020-01-06 14:20:03.957  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2845 ms
    	2020-01-06 14:20:04.556  INFO 1 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
    	2020-01-06 14:20:05.115  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
    	2020-01-06 14:20:05.190  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.4.8.Final}
    	2020-01-06 14:20:05.374  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
    	2020-01-06 14:20:05.514  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
    	2020-01-06 14:20:15.300  INFO 1 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
    	2020-01-06 14:20:15.308  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
    	2020-01-06 14:20:17.215  WARN 1 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
    	2020-01-06 14:20:17.426  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    	2020-01-06 14:20:18.009  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8201 (http) with context path ''
    	2020-01-06 14:20:18.012  INFO 1 --- [           main] com.hy.lszz.LszzApplication              : Started LszzApplication in 17.935 seconds (JVM running for 18.565)
    
    

    看这个和上面第七步是不是一模一样呢。

    二、容器查看

    
    	[root@dex leshan]# docker ps -a
    	CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                              NAMES
    	736586c25763        openjdk:8           "java -jar /opt/back…"   2 minutes ago       Up 2 minutes                 0.0.0.0:8301->8201/tcp             ls_bk02
    	30bbc32a1e95        leshan_bk           "java -jar /leshan_b…"   39 minutes ago      Exited (143) 8 minutes ago                                      ls_bk
    	1719b24f00a7        vuedev:v1           "docker-entrypoint.s…"   11 days ago         Up 11 days                   9009/tcp, 0.0.0.0:8191->8009/tcp   vuedev2
    	[root@dex leshan]# 
    
    

    使用 docker ps -a查看所有容器 ;发现刚才使用docker 直接运行jar 其实也是创建了一个容器ls_bk02

    三、 配置说明

    -d 表示在后台启动

    -p 8301:8201 表示将容器的8201端口 映射到宿主主机的8301端口,否则外部8301端口访问不到

    -v /opt/backstage_proj/leshan/lszz-0.0.1-SNAPSHOT.jar:/opt/backsttage_proj/tmp02/lszz-0.0.1-SNAPSHOT.jar
    表示将宿主主机的jar文件,挂载到容器中(分号前为宿主主机的路径,分号后为容器中的路径)
    
    --name ls_bk02 表示为该容器取一个全局唯一的名称(这个随意)
    
      docker.io/openjdk:8 表示运行jar 所选的镜像文件的名称和tag
    

    java -jar /usr/springboot-1.jar 表示运行jar包,注意:这里的jar包为容器中的位置,是通过前面的-v属性映射的

  • 相关阅读:
    Pyhont 高阶函数
    Python 函数式编程
    Python 递归函数
    Python 函数的参数定义
    Lniux学习-AWK使用
    Windows10 下 VirtualBox6 中 Centos8 无法安装"增强功能"
    Linux学习-Shell-系统启动过程与执行方式
    接口测试-工具介绍
    Linux学习-Sed 命令
    Linux学习-命令行参数、函数
  • 原文地址:https://www.cnblogs.com/dengxiaoning/p/12168861.html
Copyright © 2020-2023  润新知