Spring boot 是现在web开发的首选框架, 这篇文章不是介绍Spring boot 特性的,介绍一种用Docker 容器简单的部署Spring boot 应用的方式。
1: Spring boot 项目的构建我在这里就不详情的介绍了,不明白的童鞋可以百度下很简单, 将Spring boot 项目使用Maven (我使用的是Maven) 打包如: docker-spring-boot-0.0.1-SNAPSHOT.jar
2:构建Dockerfile 文件 内容如下:
# from base image centos
FROM centos
MAINTAINER yueli
#install java
RUN yum -y install java
#inatall app
#ADD <src> <dest> from appUrl to container destUrl
ADD tar /usr/project/
#start app
ENTRYPOINT ["java" ,"-jar","/usr/project/docker-spring-boot-0.0.1-SNAPSHOT.jar"]
3: 构建项目部署路径
根据上文Dockerfile 文件 , 应用应放到 tar 目录下 和 Dockerfile 同目录下
[root@test102 docker]# ls
Dockerfile tar
4:制作Docker 镜像
[root@localhost docker]#docker build -t yueli/springboot .
Sending build context to Docker daemon 13.43 MB
Step 1 : FROM centos
---> 67591570dd29
Step 2 : MAINTAINER yueli
---> Running in ddb6a0a2ff86
---> 7b0f0201b773
Removing intermediate container ddb6a0a2ff86
Step 3 : RUN yum -y install java
---> Running in 8b8ba6457fa9
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.yun-idc.com
* extras: mirrors.nwsuaf.edu.cn
* updates: mirrors.nwsuaf.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.121-0.b13.el7_3 will be installed
.........
Complete!
---> 92e467ca8206
Removing intermediate container 8b8ba6457fa9
Step 4 : ADD tar/user/project /usr/project/
---> aa2d58e99a00
Removing intermediate container 34b01449abc4
Step 5 : ENTRYPOINT java -jar /usr/project/docker-spring-boot-0.0.1-SNAPSHOT.jar
---> Running in e827b108553d
---> 5c80a025b5ca
Removing intermediate container e827b108553d
Successfully built 5c80a025b5ca
5: 运行Docker image
[root@localhost docker]# docker run -p 8080:8080 yueli/spirngboot
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.3.RELEASE)
2017-02-24 03:33:48.769 INFO 1 --- [ main] docker_spring_boot.Application : Starting Application v0.0.1-SNAPSHOT on 306f7afe901a with PID 1 (/usr/project/docker-spring-boot-0.0.1-SNAPSHOT.jar started by root in /)
2017-02-24 03:33:48.779 INFO 1 --- [ main] docker_spring_boot.Application : No active profile set, falling back to default profiles: default
2017-02-24 03:33:49.481 INFO 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3a35748e: startup date [Fri Feb 24 03:33:49 UTC 2017]; root of context hierarchy
2017-02-24 03:33:52.798 INFO 1 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2017-02-24 03:33:56.079 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-02-24 03:33:56.175 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-02-24 03:33:56.177 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.32
2017-02-24 03:33:57.139 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-02-24 03:33:57.139 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 7681 ms
2017-02-24 03:33:59.360 INFO 1 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-02-24 03:33:59.381 INFO 1 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-02-24 03:33:59.401 INFO 1 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-02-24 03:33:59.426 INFO 1 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-02-24 03:33:59.426 INFO 1 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-02-24 03:33:59.681 INFO 1 --- [ost-startStop-1] o.a.c.util.SessionIdGeneratorBase : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [122] milliseconds.
2017-02-24 03:34:00.663 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3a35748e: startup date [Fri Feb 24 03:33:49 UTC 2017]; root of context hierarchy
2017-02-24 03:34:00.899 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String docker_spring_boot.Application.home()
2017-02-24 03:34:00.906 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-02-24 03:34:00.907 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-02-24 03:34:01.051 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-24 03:34:01.052 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-24 03:34:01.264 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-24 03:34:01.616 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-02-24 03:34:02.219 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-02-24 03:34:02.226 INFO 1 --- [ main] docker_spring_boot.Application : Started Application in 15.422 seconds (JVM running for 17.617)
这是可以看到spring boot 项目已经启动
6:访问项目
http://172.16.xxx.xxx:8080/
可以正常访问
这样简单的Docker 部署spring boot 程序完成 Dockerfile 语法很多我上面只用了一些最基本的,有兴趣的可以去官网上去看。