lovegigi 发表于 2017-12-5 18:48:45

使用docker部署springboot应用

  1、构造Dockerfile打包文件



# from base image centos
FROM centos
#MAINTAINER lirenqing
#Java Version
#ENV JAVA_VERSION_MAJOR 7
#ENV JAVA_VERSION_MINOR 60
#ENV JAVA_PACKAGE       jdk
#install java
RUN yum -y install java
#inatall app
ADD tar /usr/project/
#start app
ENTRYPOINT ["java" ,"-jar","/usr/project/docker-spring-boot-0.0.1-SNAPSHOT.jar"]
  2、构建app部署文件路径
  根据Dockerfile文件中定义的,app需要放置到tar目录下。dockerfile同目录下建立tar目录,并复制docker-spring-boot-0.0.1-SNAPSHOT.jar文件到tar目录。
  3、制作docker镜像



➜docker$: docker build -t lirenqing/springboot .
Sending build context to Docker daemon 416.4 MB
Step 1 : FROM centos
---> 67591570dd29
Step 2 : ADD tar /usr/project/
---> Using cache
---> 6953cdf7e647
Step 3 : RUN yum -y install java
---> Using cache
---> 24307a233aff
Step 4 : ENTRYPOINT java -jar /usr/project/docker-spring-boot-0.0.1-SNAPSHOT.jar
---> Using cache
---> 1d32b755d223
Successfully built 1d32b755d223
  4、运行docker image



➜docker$: docker run -p 8082:8081 lirenqing/springboot
2017-01-10 19:06:38.862INFO 1 --- [         main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@deb6432: startup date ; root of context hierarchy
2017-01-10 19:06:39.805INFO 1 --- [         main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-01-10 19:06:39.908INFO 1 --- [         main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ ____ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/___)| |_)| | | | | || (_| |) ) ) )
'|____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::      (v1.4.3.RELEASE)
2017-01-10 19:06:40.659INFO 1 --- [         main] com.example.DockerSpringBootApplication: No active profile set, falling back to default profiles: default
2017-01-10 19:06:40.726INFO 1 --- [         main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@51565ec2: startup date ; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@deb6432
2017-01-10 19:06:42.960WARN 1 --- [         main] o.s.c.a.ConfigurationClassPostProcessor: Cannot enhance @Configuration bean definition 'refreshScope' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2017-01-10 19:06:43.297INFO 1 --- [         main] o.s.cloud.context.scope.GenericScope   : BeanFactory id=99495abd-c419-3697-8a34-170d1b24e479
2017-01-10 19:06:43.519INFO 1 --- [         main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-01-10 19:06:43.840INFO 1 --- [         main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-01-10 19:06:44.676INFO 1 --- [         main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8081 (http)
2017-01-10 19:06:44.723INFO 1 --- [         main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-01-10 19:06:44.731INFO 1 --- [         main] org.apache.catalina.core.StandardEngine: Starting Servlet Engine: Apache Tomcat/8.5.6
2017-01-10 19:06:44.971INFO 1 --- o.a.c.c.C...[/]       : Initializing Spring embedded WebApplicationContext
2017-01-10 19:06:44.973INFO 1 --- o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4247 ms
2017-01-10 19:06:45.569INFO 1 --- o.s.b.w.servlet.ServletRegistrationBean: Mapping servlet: 'dispatcherServlet' to [/]
2017-01-10 19:06:45.581INFO 1 --- o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'metricsFilter' to: [/*]
2017-01-10 19:06:45.582INFO 1 --- o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-01-10 19:06:45.584INFO 1 --- o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-01-10 19:06:45.585INFO 1 --- o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-01-10 19:06:45.589INFO 1 --- o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-01-10 19:06:45.590INFO 1 --- o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2017-01-10 19:06:45.591INFO 1 --- o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'applicationContextIdFilter' to: [/*]
2017-01-10 19:06:46.594INFO 1 --- [         main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@51565ec2: startup date ; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@deb6432
2017-01-10 19:06:47.111INFO 1 --- [         main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{}" onto public java.lang.String com.example.DockerSpringBootApplication.hell()
2017-01-10 19:06:47.124INFO 1 --- [         main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{,produces=}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-01-10 19:06:47.132INFO 1 --- [         main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{}" 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-01-10 19:06:47.225INFO 1 --- [         main] o.s.w.s.handler.SimpleUrlHandlerMapping: Mapped URL path onto handler of type
2017-01-10 19:06:47.225INFO 1 --- [         main] o.s.w.s.handler.SimpleUrlHandlerMapping: Mapped URL path [/**] onto handler of type
2017-01-10 19:06:47.391INFO 1 --- [         main] o.s.w.s.handler.SimpleUrlHandlerMapping: Mapped URL path [/**/favicon.ico] onto handler of type
2017-01-10 19:06:49.669INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2017-01-10 19:06:49.670INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.673INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.677INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
2017-01-10 19:06:49.678INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal)
2017-01-10 19:06:49.681INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.685INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.687INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.696INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.698INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=}" onto public java.lang.Object org.springframework.cloud.context.environment.EnvironmentManagerMvcEndpoint.value(java.util.Map<java.lang.String, java.lang.String>)
2017-01-10 19:06:49.699INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.cloud.context.environment.EnvironmentManagerMvcEndpoint.reset()
2017-01-10 19:06:49.701INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=}" onto public java.lang.Object org.springframework.cloud.context.restart.RestartMvcEndpoint.invoke()
2017-01-10 19:06:49.709INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.710INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
2017-01-10 19:06:49.724INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.727INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2017-01-10 19:06:49.728INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.735INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
2017-01-10 19:06:49.738INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.741INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=,produces=}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-10 19:06:49.743INFO 1 --- [         main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{,methods=}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
2017-01-10 19:06:50.094INFO 1 --- [         main] o.s.ui.freemarker.SpringTemplateLoader   : SpringTemplateLoader for FreeMarker: using resource loader ; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@deb6432] and template loader path
2017-01-10 19:06:50.103INFO 1 --- [         main] o.s.w.s.v.f.FreeMarkerConfigurer         : ClassTemplateLoader for Spring macros added to FreeMarker configuration
2017-01-10 19:06:50.358WARN 1 --- [         main] o.s.c.n.a.ArchaiusAutoConfiguration      : No spring.application.name found, defaulting to 'application'
2017-01-10 19:06:50.372WARN 1 --- [         main] c.n.c.sources.URLConfigurationSource   : No URLs will be polled as dynamic configuration sources.
2017-01-10 19:06:50.372INFO 1 --- [         main] c.n.c.sources.URLConfigurationSource   : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2017-01-10 19:06:50.391WARN 1 --- [         main] c.n.c.sources.URLConfigurationSource   : No URLs will be polled as dynamic configuration sources.
2017-01-10 19:06:50.396INFO 1 --- [         main] c.n.c.sources.URLConfigurationSource   : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2017-01-10 19:06:50.569INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Registering beans for JMX exposure on startup
2017-01-10 19:06:50.601INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Bean with name 'refreshScope' has been autodetected for JMX exposure
2017-01-10 19:06:50.603INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Bean with name 'environmentManager' has been autodetected for JMX exposure
2017-01-10 19:06:50.608INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure
2017-01-10 19:06:50.611INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Bean with name 'refreshEndpoint' has been autodetected for JMX exposure
2017-01-10 19:06:50.613INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Bean with name 'restartEndpoint' has been autodetected for JMX exposure
2017-01-10 19:06:50.622INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Located managed bean 'environmentManager': registering with JMX server as MBean
2017-01-10 19:06:50.655INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Located managed bean 'restartEndpoint': registering with JMX server as MBean
2017-01-10 19:06:50.700INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Located managed bean 'refreshScope': registering with JMX server as MBean
2017-01-10 19:06:50.728INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean
2017-01-10 19:06:50.757INFO 1 --- [         main] o.s.j.e.a.AnnotationMBeanExporter      : Located managed bean 'refreshEndpoint': registering with JMX server as MBean
2017-01-10 19:06:50.964INFO 1 --- [         main] o.s.c.support.DefaultLifecycleProcessor: Starting beans in phase 0
2017-01-10 19:06:51.157INFO 1 --- [         main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2017-01-10 19:06:51.166INFO 1 --- [         main] com.example.DockerSpringBootApplication: Started DockerSpringBootApplication in 14.798 seconds (JVM running for 16.392)
  当看到上面信息,证明已经运行成功了。可以直接访问localhost:8082即可访问构建的springboot应用了。
  注:
  本次构建由于网络原因,使用docker默认的镜像仓库下载经常出错,本次采用国内镜像网站下载。镜像代理设置可以访问:daodocker.io,需要注册一个帐号后有配置说明,参照配置说明配置即可。
页: [1]
查看完整版本: 使用docker部署springboot应用