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属性映射的
docker 部署 springboot项目和直接运行 jar 文件详细步骤的更多相关文章
- springboot 项目打包可运行jar文件
eclipse 运行run as maven bulid ,填入package ,运行打包 java -jar xxx.jar
- docker部署springboot项目
本文介绍一下docker如何部署springboot项目. 前提条件: 1.可以运行jar包的环境 2.机器上已经安装了docker 3.准备部署的springboot的jar包 4.Dockerfi ...
- springboot(十七)-使用Docker部署springboot项目
Docker 技术发展为微服务落地提供了更加便利的环境,使用 Docker 部署 Spring Boot 其实非常简单,这篇文章我们就来简单学习下. 首先构建一个简单的 Spring Boot 项目, ...
- 超简单的 Docker部署 SpringBoot项目 步骤
很久之前就用过,一直没有好好写篇博客,今天就总结一下 创建一个 SpringBoot项目 创建一个SpringBoot项目并打成jar包,结构如图 编写 Dockerfile文件 FROM java: ...
- Centos8.3、docker部署springboot项目实战记录
引言 目前k8s很是火热,我也特意买了本书去学习了一下,但是k8s动辄都是成百上千的服务器运维,对只有几台服务器的应用来说使用k8s就有点像大炮打蚊子.只有几台服务器的应用运维使用传统的tomc ...
- 部署springboot项目时 打包成jar时包中html,js,css文件缺失
问题 打包出来的jar包里面没有html,js,css文件 解决方案 在pom.xml文件下的build选项中的src/main/resources的目录下 添加配置 <build> &l ...
- docker 部署springboot项目【转载】
https://www.cnblogs.com/ityouknow/p/8599093.html
- Docker快速上手之部署SpringBoot项目
Docker是基于Go语言实现的云开源项目. Docker的主要目标是“Build,Ship and Run Any App,Anywhere”,也就是通过对应用组件的封装.分发.部署.运行等生命周期 ...
- Docker下部署springboot项目
1.背景 如何在docker容器环境下部署一个springboot项目? 2.具体步骤 第一步:准备一个springboot项目的xxxx.jar包 jar包中用于测试的一个接口如下 第二步:编写Do ...
- Docker部署Springboot+Vue项目
1 docker使用nginx部署vue项目 1.1 打包vue项目 npm run build vue项目路径下会增加一个dist文件夹,里面就是网页文件 1.2 使用docker 拉取nginx ...
随机推荐
- MySQL学习(十)索引
1.索引的种类 聚簇索引,非聚簇索引 主键索引,唯一索引,普通索引(前缀索引),全文索引 单值索引,复合索引 二级索引 覆盖索引 1.1 聚簇索引,非聚簇索引 参考文档: https://www.cn ...
- 使用go的并发性来解决Hilbert酒店问题
译自:Designing for Concurrency: the Hilbert's Hotel Problem in Go,本文使用go的并发性来解决Hilbert酒店问题.本文比较有意思的是它对 ...
- ros系统(1)
在虚拟机上安装好ros系统之后,打开终端,启动ROS Master,输入roscore命令,结果如下: 再启动小海龟仿真器,输入命令:rosrun turtlesim turtlesim_node,结 ...
- 深入理解 Python 虚拟机:字典(dict)的优化
深入理解 Python 虚拟机:字典(dict)的优化 在前面的文章当中我们讨论的是 python3 当中早期的内嵌数据结构字典的实现,在本篇文章当中主要介绍在后续对于字典的内存优化. 字典优化 在前 ...
- MYSQL DQL语句(基础)
MySQL引入 数据库的好处 持久化数据到本地 可以实现结构化查询,方便管理 数据库的相关概念 DB:数据库(database):存储数据的"仓库",它保存了一系列有组织的数据. ...
- js面试题学习整理
1. 异步操作有哪些? 回调函数,事件监听,promise,ajax,async,setTimeout,Generator 2. Promise是什么? Promise是异步编程的一种解决方案. 从语 ...
- [ACM]NEFUOJ-最长上升子序列
Description 给出长度为n的数组,找出这个数组的最长上升子序列 Input 第一行:输入N,为数组的长度(2=<N<=50000) 第二行:N个值,表示数组中元素的值(109&l ...
- 建木在 Rainbond 上使用实践
建木 是一个面向 DevOps 领域的极易扩展的开源无代码(图形化)/低代码(GitOps)工具,可以帮助用户轻松编排各种DevOps流程并分发到不同平台执行. 建木的图形化编排提供了多个节点,节点可 ...
- Zookeeper的深入分析
运⾏时状态分析 在ZAB协议的设计中,每个进程都有可能处于如下三种状态之⼀ · LOOKING:Leader选举阶段. · FOLLOWING:Follower服务器和Leader服务器保持同步状态. ...
- list列表和tuple、条件判断、循环、dict和set、调用函数、定义函数
1.list列表是有序的可变的列表,可以进通过append()方法末尾添加,通过pop删除末尾以及根据索引pop(i)来删除指定索引对应的元素 通过给指定的列表元素赋值更改元素值,通过列表的索引查看元 ...