1.登录网站https://start.spring.io/,生成一个基本的SpringBoot应用。
2.将应用导入Eclipse IDE并创建Application类。目录结构如下:
Application代码如下:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Application {
@RequestMapping(value="/phone")
public String index(){
return "Hello World!";
}
}
3.在pom.xml文件中增加插件配置:
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed-->
<imageName>microservice-eureka-demo</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
4.pom.xml文件中增加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
5.配置本地环境变量,设置docker文件构建主机(主机192.168.244.130已经安装完docker环境)
root@ubuntu-02:~# docker -v
Docker version 17.05.0-ce, build 89658be
本地环境变量配置如下:
6.进入代码目录,执行命令“mvn clean package docker:build”项目编译成功。
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ dockerdemo ---
[INFO] Building jar: C:\Users\Sean\Downloads\demo\demo\target\dockerdemo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ dockerdemo ---
[INFO]
[INFO] --- docker-maven-plugin:0.4.12:build (default-cli) @ dockerdemo ---
[INFO] Copying C:\Users\Sean\Downloads\demo\demo\target\dockerdemo-0.0.1-SNAPSHOT.jar -> C:\Users\Sean\Downloads\demo\demo\target\docker\dockerdemo-0.0.1-SNAPSHOT.jar
[INFO] Building image microservice-eureka-demo
Step 1/3 : FROM java
---> d23bdf5b1b1b
Step 2/3 : ADD /dockerdemo-0.0.1-SNAPSHOT.jar //
---> e1b3c3de711c
Removing intermediate container a9e57b1d9883
Step 3/3 : ENTRYPOINT java -jar /dockerdemo-0.0.1-SNAPSHOT.jar
---> Running in 7a3cdc1d112b
---> 1f800bf8cb24
Removing intermediate container 7a3cdc1d112b
Successfully built 1f800bf8cb24
Successfully tagged microservice-eureka-demo:latest
[INFO] Built microservice-eureka-demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 39.982 s
[INFO] Finished at: 2017-07-23T21:51:21+08:00
[INFO] Final Memory: 42M/393M
[INFO] ------------------------------------------------------------------------
7.ssh连接192.168.244.130,执行命令“docker images”,查看可用的镜像:
root@ubuntu-02:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
microservice-eureka-demo latest 1f800bf8cb24 23 minutes ago 658MB
8.执行命令“docker run -p 8080:8080 microservice-eureka-demo:latest”,使用镜像microservice-eureka-demo:latest启动docker容器。
root@ubuntu-02:~# docker run -p 8080:8080 microservice-eureka-demo:latest
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.4.RELEASE)
2017-07-23 14:19:21.766 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on 656842f890d3 with PID 1 (/dockerdemo-0.0.1-SNAPSHOT.jar started by root in /)
2017-07-23 14:19:21.792 INFO 1 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2017-07-23 14:19:22.402 INFO 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2e4553: startup date [Sun Jul 23 14:19:22 UTC 2017]; root of context hierarchy
2017-07-23 14:19:31.950 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-23 14:19:32.015 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-07-23 14:19:32.032 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-07-23 14:19:32.467 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-23 14:19:32.467 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 10082 ms
2017-07-23 14:19:33.101 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-23 14:19:33.141 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-23 14:19:33.148 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-23 14:19:33.161 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-23 14:19:33.161 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-23 14:19:34.708 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2e4553: startup date [Sun Jul 23 14:19:22 UTC 2017]; root of context hierarchy
2017-07-23 14:19:35.105 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/phone]}" onto public java.lang.String com.example.demo.controller.Application.index()
2017-07-23 14:19:35.127 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-07-23 14:19:35.138 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-07-23 14:19:35.277 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-07-23 14:19:35.285 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-23 14:19:35.466 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-07-23 14:19:36.180 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-07-23 14:19:36.449 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-23 14:19:36.477 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 17.363 seconds (JVM running for 19.474)
9.执行命令“docker ps”,查看启动的docker容器。
root@ubuntu-02:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08099bd4b8e1 microservice-eureka-demo:latest "java -jar /docker..." 27 seconds ago Up 26 seconds 0.0.0.0:8080->8080/tcp upbeat_borg
10.执行命令“docker exec -it 08099bd4b8e1 bin/bash”,进入docker容器。
root@ubuntu-02:~# docker exec -it 08099bd4b8e1 bin/bash
root@08099bd4b8e1:/#
11.执行命令“ps -ef”,可用查看到docker容器里面启动的进程,即是pom.xml配置文件中配置的插件docker-maven-plugin的entryPoint,java -jar /dockerdemo-0.0.1-SNAPSHOT.jar。
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
root@08099bd4b8e1:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 4 14:20 ? 00:00:17 java -jar /dockerdemo-0.0.1-SNAPSHOT.jar
root 34 0 0 14:24 ? 00:00:00 bin/bash
root 41 34 0 14:26 ? 00:00:00 ps -ef
root@08099bd4b8e1:/#
12.至此,使用docker插件构建docker镜像并运行docker容器完成,访问路径“http://192.168.244.130:8080/p...”,获得controller结果:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。