1. 程式人生 > >使用Docker部署Spring Boot 應用 Dockerfile方式

使用Docker部署Spring Boot 應用 Dockerfile方式

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 同目錄下

[[email protected] docker]# ls
Dockerfile  tar

4:製作Docker 映象

[[email protected] 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
[[email protected] 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
[email protected]
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[email protected]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 語法很多我上面只用了一些最基本的,有興趣的可以去官網上去看。