一、打包SpringBoot项目
1.检查pom文件是否有maven打包插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.选择IDEA右侧Maven,点击install(如果配置了maven的环境变量,则可以在控制台输入:mvn install 命令,同样可以打包jar包)
等待maven打包,当控制台出现下列日志时表示打包成功
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:16 min
[INFO] Finished at: 2019-11-12T21:49:47+08:00
[INFO] Final Memory: 48M/392M
[INFO] ------------------------------------------------------------------------
如下图所示,打包完会出现一个.jar文件,即打包好的jar包。
3.在控制台输入:java -jar /Users/heyou/Desktop/springboot-0.0.1-SNAPSHOT.jar 来检查jar包是否可以正常运行。
192:~ heyou$ java -jar /Users/heyou/Desktop/springboot-0.0.1-SNAPSHOT.jar
启动中....
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.9.RELEASE)
2019-11-12 21:54:33.478 INFO 1831 --- [ main] c.h.springboot.SpringbootApplication : Starting SpringbootApplication v0.0.1-SNAPSHOT on 192.168.0.118 with PID 1831 (/Users/heyou/Desktop/springboot-0.0.1-SNAPSHOT.jar started by heyou in /Users/heyou)
2019-11-12 21:54:33.481 INFO 1831 --- [ main] c.h.springboot.SpringbootApplication : No active profile set, falling back to default profiles: default
2019-11-12 21:54:34.479 INFO 1831 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-11-12 21:54:34.482 INFO 1831 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-11-12 21:54:34.518 INFO 1831 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17ms. Found 0 repository interfaces.
2019-11-12 21:54:34.935 INFO 1831 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$904eee42] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-12 21:54:35.539 INFO 1831 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8800 (http)
2019-11-12 21:54:35.571 INFO 1831 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-12 21:54:35.571 INFO 1831 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.26]
2019-11-12 21:54:35.677 INFO 1831 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-12 21:54:35.677 INFO 1831 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2149 ms
2019-11-12 21:54:35.846 INFO 1831 --- [ main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2019-11-12 21:54:36.217 INFO 1831 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
2019-11-12 21:54:37.511 INFO 1831 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
2019-11-12 21:54:37.847 INFO 1831 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-12 21:54:38.487 INFO 1831 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2019-11-12 21:54:38.510 INFO 1831 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2019-11-12 21:54:38.549 INFO 1831 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
2019-11-12 21:54:38.781 INFO 1831 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8800 (http) with context path ''
2019-11-12 21:54:38.784 INFO 1831 --- [ main] c.h.springboot.SpringbootApplication : Started SpringbootApplication in 5.736 seconds (JVM running for 6.126)
2019-11-12 21:54:53.190 INFO 1831 --- [nio-8800-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-11-12 21:54:53.190 INFO 1831 --- [nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-11-12 21:54:53.218 INFO 1831 --- [nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 28 ms
二、Docker部署jar包
前提条件:
- 可以运行jar包的环境
- 机器上已经安装了docker
- 准备部署的springboot的jar包
- Dockerfile文件
1.构建Dockerfile
在已经打包好的jar包的同级目录下新建Dockerfile
Mac 可在IDEA控制台输入vim Dockerfile 进入vim编辑,输入:
FROM java:8
VOLUME /tmp
ADD springboot-0.0.1-SNAPSHOT.jar /springboot-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/springboot-0.0.1-SNAPSHOT.jar"]
其中:
FROM 命令指定运行环境和版本
VOLUME:一个特别指定的目录,用于存储数据,该命令的作用是在/var/lib/docker创建一个名为tmp的目录,在开启redis服务时,需要特别指定redis的数据存储在哪个文件夹,此时这个命令就十分有用
ADD:拷贝文件并且重命名(需要输入两个参数:即jar包名称和重命名后的名称)
ENTRYPOINT:容器启动时运行的命令,相当于我们在命令行中输入java -jar xxxx.jar,为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为 ENTRYPOINT
2.创建docker文件夹,将jar包与Dockerfile移动到该文件夹,进入该文件夹,运行命令:
docker build -t docker .
此时会将该项目构建成一个image(镜像),运行命令:
docker images
3.运行镜像:
docker run -d -p 4567:8080 springboot-demo
注:“:”左边的端口号是暴露给外部访问的端口,而右边端口是docker内部镜像运行的端口
4.打开浏览器访问部署的项目地址,检查项目是否运行正常