將項目打包成jarhtml
FROM java:8 VOLUME /tmp ADD elk-web-1.0-SNAPSHOT.jar elk.jar EXPOSE 8080 ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/elk.jar"]
FROM:表示基礎鏡像,即運行環境
VOLUME /tmp建立/tmp目錄並持久化到Docker數據文件夾,由於Spring Boot使用的內嵌Tomcat容器默認使用/tmp做爲工做目錄
ADD:拷貝文件而且重命名(ADD elk-web-1.0-SNAPSHOT.jar elk.jar 將應用jar包複製到/elk.jar)
EXPOSE:並非真正的發佈端口,這個只是容器部署人員與創建image的人員之間的交流,即創建image的人員告訴容器佈署人員容器應該映射哪一個端口給外界
ENTRYPOINT:容器啓動時運行的命令,至關於咱們在命令行中輸入java -jar xxxx.jar,爲了縮短 Tomcat 的啓動時間,添加java.security.egd的系統屬性指向/dev/urandom做爲 ENTRYPOINTjava
[root@VM_0_15_centos elk]# docker build -t elk . Sending build context to Docker daemon 14.43 MB Step 1/5 : FROM java:8 Trying to pull repository docker.io/library/java ... 8: Pulling from docker.io/library/java 5040bd298390: Pull complete fce5728aad85: Pull complete 76610ec20bf5: Pull complete 60170fec2151: Pull complete e98f73de8f0d: Pull complete 11f7af24ed9c: Pull complete 49e2d6393f32: Pull complete bb9cdec9c7f3: Pull complete Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d Status: Downloaded newer image for docker.io/java:8 ---> d23bdf5b1b1b Step 2/5 : VOLUME /tmp ---> Running in 0aec2dc2f98c ---> a52e844f25d4 Removing intermediate container 0aec2dc2f98c Step 3/5 : ADD elk-web-1.0-SNAPSHOT.jar elk.jar ---> 3ba2f4fdddda Removing intermediate container 860a0f748a23 Step 4/5 : EXPOSE 8080 ---> Running in 1d3331cc2be6 ---> e9ac33d26ce0 Removing intermediate container 1d3331cc2be6 Step 5/5 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /elk.jar ---> Running in d354f8ee2af5 ---> 8937e1ade6c7 Removing intermediate container d354f8ee2af5 Successfully built 8937e1ade6c7
docker run -di --name 容器名稱 -p 8080:8080 鏡像名稱
其中-d表示後臺運行容器,這也就天然地解決的Spring Boot不支持後臺運行應用程序的問題。
-p 8080:8080表示將容器內部的8080端口映射到宿主機器的8080端口,這樣就能夠經過宿主機器直接訪問應
用。
--name 給容器取一個容易記住的名字方便往後管理。linux
[root@VM_0_15_centos elk]# docker run -di --name myspringboot -p 8080:8080 8937e1ade6c7 04d6b2c347950a10c95a039c94a3e51d717e516dd8c3c742e3197687dfcf5523 [root@VM_0_15_centos elk]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 04d6b2c34795 8937e1ade6c7 "java -Djava.secur..." 8 seconds ago Up 7 seconds 0.0.0.0:8080->8080/tcp myspringboot [root@VM_0_15_centos elk]#
docker logs -f --tail=100 容器名稱git
[root@VM_0_15_centos elk]# docker logs -f --tail=100 04d6b2c34795 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.4.RELEASE) 2019-12-29 07:42:58.982 INFO 1 --- [ main] c.b.ElkExampleSpringBootApplication : Starting ElkExampleSpringBootApplication v1.0-SNAPSHOT on 04d6b2c34795 with PID 1 (/elk.jar started by root in /) 2019-12-29 07:42:58.999 INFO 1 --- [ main] c.b.ElkExampleSpringBootApplication : No active profile set, falling back to default profiles: default 2019-12-29 07:42:59.243 INFO 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2e4553: startup date [Sun Dec 29 07:42:59 UTC 2019]; root of context hierarchy 2019-12-29 07:43:03.652 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2019-12-29 07:43:03.699 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-12-29 07:43:03.714 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15 2019-12-29 07:43:04.012 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-12-29 07:43:04.012 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4772 ms 2019-12-29 07:43:04.449 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2019-12-29 07:43:04.470 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2019-12-29 07:43:04.470 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2019-12-29 07:43:04.471 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2019-12-29 07:43:04.471 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2019-12-29 07:43:05.534 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2e4553: startup date [Sun Dec 29 07:42:59 UTC 2019]; root of context hierarchy 2019-12-29 07:43:05.765 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/exception]}" onto public java.lang.String com.bruceliu.controller.ELKController.exception() 2019-12-29 07:43:05.766 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/elkdemo]}" onto public java.lang.String com.bruceliu.controller.ELKController.helloWorld() 2019-12-29 07:43:05.772 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) 2019-12-29 07:43:05.780 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) 2019-12-29 07:43:05.869 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-12-29 07:43:05.869 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-12-29 07:43:05.984 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] 2019-12-29 07:43:06.387 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2019-12-29 07:43:06.537 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2019-12-29 07:43:06.562 INFO 1 --- [ main] c.b.ElkExampleSpringBootApplication : Started ElkExampleSpringBootApplication in 8.771 seconds (JVM running for 9.832)
首先編輯咱們服務器上的docker文件web
vim /usr/lib/systemd/system/docker.service
修改以ExecStart開頭的行(centos 7):添加spring
-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \
修改後保存文件,而後重啓dockerdocker
systemctl daemon-reload service docker restart
重啓以後測試遠程鏈接是否正常,這裏的2375是以前配置的端口shell
curl http://localhost:2375/version
看到返回信息基本上就沒有問題了apache
[root@VM_0_15_centos elk]# curl http://localhost:2375/version {"Version":"1.13.1","ApiVersion":"1.26","MinAPIVersion":"1.12","GitCommit":"7f2769b/1.13.1","GoVersion":"go1.10.3","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-957.21.3.el7.x86_64","BuildTime":"2019-09-15T14:06:47.565778468+00:00","PkgVersion":"docker-1.13.1-103.git7f2769b.el7.centos.x86_64"}
而後開啓端口,或者關閉防火牆,兩者選其一便可vim
firewall-cmd --zone=public --add-port=2375/tcp --permanent
chkconfig iptables off
而後打開瀏覽器測試將以前的localhost修改成你的ip
首先下載docker插件,idea2019自帶了docker插件。若是沒有插件能夠選擇安裝docker插件
而後配置docker地址,在你的File | Settings | Build, Execution, Deployment | Docker
配置完成連接以後,出現了框中的內容便可.
連接成功以後會列出容器和鏡像!
配置阿里雲鏡像加速器:
在咱們持續集成過程當中,項目工程通常使用 Maven 編譯打包,而後生成鏡像,經過鏡像上線,可以大大提供上線效率,同時可以快速動態擴容,快速回滾,着實很方便。docker-maven-plugin 插件就是爲了幫助咱們在Maven工程中,經過簡單的配置,自動生成鏡像並推送到倉庫中。
pom.xml:
<build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> <!-- 跳過單元測試 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <!--使用docker-maven-plugin插件--> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <!--將插件綁定在某個phase執行--> <executions> <execution> <id>build-image</id> <!--用戶只需執行mvn package ,就會自動執行mvn docker:build--> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> <configuration> <!--指定生成的鏡像名--> <imageName>bruceliu/${project.artifactId}</imageName> <!--指定標籤--> <imageTags> <imageTag>latest</imageTag> </imageTags> <!--指定基礎鏡像jdk1.8--> <baseImage>java</baseImage> <!--鏡像製做人本人信息--> <maintainer>bruceliu@email.com</maintainer> <!--切換到ROOT目錄--> <workdir>/ROOT</workdir> <cmd>["java", "-version"]</cmd> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <!--指定遠程 docker api地址--> <dockerHost>http://122.51.50.249:2375</dockerHost> <!-- 這裏是複製 jar 包到 docker 容器指定目錄配置 --> <resources> <resource> <targetPath>/</targetPath> <!--jar 包所在的路徑 此處配置的 即對應 target 目錄--> <directory>${project.build.directory}</directory> <!--用於指定須要複製的文件 須要包含的 jar包 ,這裏對應的是 Dockerfile中添加的文件名 --> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>
執行Maven打包命令:
G:\softDevelopment\JDK8\bin\java -Dmaven.multiModuleProjectDirectory=E:\workspace2017\elk-web -Dmaven.home=E:\Maven20190910\apache-maven-3.6.1 -Dclassworlds.conf=E:\Maven20190910\apache-maven-3.6.1\bin\m2.conf "-javaagent:G:\idea2017\IntelliJ IDEA 2017.3.1\lib\idea_rt.jar=49260:G:\idea2017\IntelliJ IDEA 2017.3.1\bin" -Dfile.encoding=UTF-8 -classpath E:\Maven20190910\apache-maven-3.6.1\boot\plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version=2017.3.7 -s E:\Maven20190910\apache-maven-3.6.1\conf\settings.xml -Dmaven.repo.local=E:\Maven20190910\repository package [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.bruceliu.elk.demo:elk-web:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.springframework.boot:spring-boot-maven-plugin @ line 36, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] -------------------< com.bruceliu.elk.demo:elk-web >-------------------- [INFO] Building elk-web 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ elk-web --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ elk-web --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to E:\workspace2017\elk-web\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ elk-web --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory E:\workspace2017\elk-web\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ elk-web --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ elk-web --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ elk-web --- [INFO] Building jar: E:\workspace2017\elk-web\target\elk-web.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ elk-web --- [INFO] [INFO] --- docker-maven-plugin:1.0.0:build (build-image) @ elk-web --- [INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier] [INFO] Copying E:\workspace2017\elk-web\target\elk-web.jar -> E:\workspace2017\elk-web\target\docker\elk-web.jar [INFO] Building image bruceliu/elk-web Step 1/6 : FROM java ---> d23bdf5b1b1b Step 2/6 : MAINTAINER bruceliu@email.com ---> Running in 787e4786fbd4 ---> 4d4519f52fda Removing intermediate container 787e4786fbd4 Step 3/6 : WORKDIR /ROOT ---> f40dcbc9a9eb Removing intermediate container 7fa6bbc9d1df Step 4/6 : ADD /elk-web.jar // ---> c7f1107ae3d4 Removing intermediate container f370558f1a38 Step 5/6 : ENTRYPOINT java -jar /elk-web.jar ---> Running in e4480ced0829 ---> b634ca5fa5ad Removing intermediate container e4480ced0829 Step 6/6 : CMD java -version ---> Running in cc6a064ef921 ---> cf9a5d50326b Removing intermediate container cc6a064ef921 Successfully built cf9a5d50326b [INFO] Built bruceliu/elk-web [INFO] Tagging bruceliu/elk-web with latest [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.329 s [INFO] Finished at: 2019-12-29T22:44:06+08:00 [INFO] ------------------------------------------------------------------------ Process finished with exit code 0