在这里插入图片描述
将项目打包成jar

  1. FROM java:8
  2. VOLUME /tmp
  3. ADD elk-web-1.0-SNAPSHOT.jar elk.jar
  4. EXPOSE 8080
  5. 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作为 ENTRYPOINT

  1. [root@VM_0_15_centos elk]# docker build -t elk .
  2. Sending build context to Docker daemon 14.43 MB
  3. Step 1/5 : FROM java:8
  4. Trying to pull repository docker.io/library/java ...
  5. 8: Pulling from docker.io/library/java
  6. 5040bd298390: Pull complete
  7. fce5728aad85: Pull complete
  8. 76610ec20bf5: Pull complete
  9. 60170fec2151: Pull complete
  10. e98f73de8f0d: Pull complete
  11. 11f7af24ed9c: Pull complete
  12. 49e2d6393f32: Pull complete
  13. bb9cdec9c7f3: Pull complete
  14. Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
  15. Status: Downloaded newer image for docker.io/java:8
  16. ---> d23bdf5b1b1b
  17. Step 2/5 : VOLUME /tmp
  18. ---> Running in 0aec2dc2f98c
  19. ---> a52e844f25d4
  20. Removing intermediate container 0aec2dc2f98c
  21. Step 3/5 : ADD elk-web-1.0-SNAPSHOT.jar elk.jar
  22. ---> 3ba2f4fdddda
  23. Removing intermediate container 860a0f748a23
  24. Step 4/5 : EXPOSE 8080
  25. ---> Running in 1d3331cc2be6
  26. ---> e9ac33d26ce0
  27. Removing intermediate container 1d3331cc2be6
  28. Step 5/5 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /elk.jar
  29. ---> Running in d354f8ee2af5
  30. ---> 8937e1ade6c7
  31. Removing intermediate container d354f8ee2af5
  32. Successfully built 8937e1ade6c7
  1. docker run -di --name 容器名称 -p 8080:8080 镜像名称

其中-d表示后台运行容器,这也就自然地解决的Spring Boot不支持后台运行应用程序的问题。
-p 8080:8080表示将容器内部的8080端口映射到宿主机器的8080端口,这样就可以通过宿主机器直接访问应
用。
–name 给容器取一个容易记住的名字方便日后管理。

  1. [root@VM_0_15_centos elk]# docker run -di --name myspringboot -p 8080:8080 8937e1ade6c7
  2. 04d6b2c347950a10c95a039c94a3e51d717e516dd8c3c742e3197687dfcf5523
  3. [root@VM_0_15_centos elk]# docker ps -a
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 04d6b2c34795 8937e1ade6c7 "java -Djava.secur..." 8 seconds ago Up 7 seconds 0.0.0.0:8080->8080/tcp myspringboot
  6. [root@VM_0_15_centos elk]#

docker logs -f –tail=100 容器名称

  1. [root@VM_0_15_centos elk]# docker logs -f --tail=100 04d6b2c34795
  2. . ____ _ __ _ _
  3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  6. ' |____| .__|_| |_|_| |_\__, | / / / /
  7. =========|_|==============|___/=/_/_/_/
  8. :: Spring Boot :: (v1.5.4.RELEASE)
  9. 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 /)
  10. 2019-12-29 07:42:58.999 INFO 1 --- [ main] c.b.ElkExampleSpringBootApplication : No active profile set, falling back to default profiles: default
  11. 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
  12. 2019-12-29 07:43:03.652 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
  13. 2019-12-29 07:43:03.699 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  14. 2019-12-29 07:43:03.714 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
  15. 2019-12-29 07:43:04.012 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  16. 2019-12-29 07:43:04.012 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4772 ms
  17. 2019-12-29 07:43:04.449 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
  18. 2019-12-29 07:43:04.470 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
  19. 2019-12-29 07:43:04.470 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  20. 2019-12-29 07:43:04.471 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
  21. 2019-12-29 07:43:04.471 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
  22. 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
  23. 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()
  24. 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()
  25. 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)
  26. 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)
  27. 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]
  28. 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]
  29. 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]
  30. 2019-12-29 07:43:06.387 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  31. 2019-12-29 07:43:06.537 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
  32. 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文件

  1. vim /usr/lib/systemd/system/docker.service

修改以ExecStart开头的行(centos 7):添加

  1. -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \

在这里插入图片描述
修改后保存文件,然后重启docker

  1. systemctl daemon-reload
  2. service docker restart

重启之后测试远程连接是否正常,这里的2375是之前配置的端口

  1. curl http://localhost:2375/version

看到返回信息基本上就没有问题了

  1. [root@VM_0_15_centos elk]# curl http://localhost:2375/version
  2. {"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"}

然后开启端口,或者关闭防火墙,二者选其一即可

  1. firewall-cmd --zone=public --add-port=2375/tcp --permanent
  1. chkconfig iptables off

然后打开浏览器测试将之前的localhost修改为你的ip
在这里插入图片描述

首先下载docker插件,idea2019自带了docker插件。如果没有插件可以选择安装docker插件
在这里插入图片描述然后配置docker地址,在你的File | Settings | Build, Execution, Deployment | Docker
在这里插入图片描述配置完成链接之后,出现了框中的内容即可.
在这里插入图片描述
链接成功之后会列出容器和镜像!

配置阿里云镜像加速器:
在这里插入图片描述

在我们持续集成过程中,项目工程一般使用 Maven 编译打包,然后生成镜像,通过镜像上线,能够大大提供上线效率,同时能够快速动态扩容,快速回滚,着实很方便。docker-maven-plugin 插件就是为了帮助我们在Maven工程中,通过简单的配置,自动生成镜像并推送到仓库中。

pom.xml:

  1. <build>
  2. <finalName>${project.artifactId}</finalName>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-maven-plugin</artifactId>
  7. <configuration>
  8. <fork>true</fork>
  9. </configuration>
  10. </plugin>
  11. <!-- 跳过单元测试 -->
  12. <plugin>
  13. <groupId>org.apache.maven.plugins</groupId>
  14. <artifactId>maven-surefire-plugin</artifactId>
  15. <configuration>
  16. <skipTests>true</skipTests>
  17. </configuration>
  18. </plugin>
  19. <!--使用docker-maven-plugin插件-->
  20. <plugin>
  21. <groupId>com.spotify</groupId>
  22. <artifactId>docker-maven-plugin</artifactId>
  23. <version>1.0.0</version>
  24. <!--将插件绑定在某个phase执行-->
  25. <executions>
  26. <execution>
  27. <id>build-image</id>
  28. <!--用户只需执行mvn package ,就会自动执行mvn docker:build-->
  29. <phase>package</phase>
  30. <goals>
  31. <goal>build</goal>
  32. </goals>
  33. </execution>
  34. </executions>
  35. <configuration>
  36. <!--指定生成的镜像名-->
  37. <imageName>bruceliu/${project.artifactId}</imageName>
  38. <!--指定标签-->
  39. <imageTags>
  40. <imageTag>latest</imageTag>
  41. </imageTags>
  42. <!--指定基础镜像jdk1.8-->
  43. <baseImage>java</baseImage>
  44. <!--镜像制作人本人信息-->
  45. <maintainer>bruceliu@email.com</maintainer>
  46. <!--切换到ROOT目录-->
  47. <workdir>/ROOT</workdir>
  48. <cmd>["java", "-version"]</cmd>
  49. <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
  50. <!--指定远程 docker api地址-->
  51. <dockerHost>http://122.51.50.249:2375</dockerHost>
  52. <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
  53. <resources>
  54. <resource>
  55. <targetPath>/</targetPath>
  56. <!--jar 包所在的路径 此处配置的 即对应 target 目录-->
  57. <directory>${project.build.directory}</directory>
  58. <!--用于指定需要复制的文件 需要包含的 jar包 ,这里对应的是 Dockerfile中添加的文件名 -->
  59. <include>${project.build.finalName}.jar</include>
  60. </resource>
  61. </resources>
  62. </configuration>
  63. </plugin>
  64. </plugins>
  65. </build>

执行Maven打包命令:
在这里插入图片描述

  1. 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
  2. [INFO] Scanning for projects...
  3. [WARNING]
  4. [WARNING] Some problems were encountered while building the effective model for com.bruceliu.elk.demo:elk-web:jar:1.0-SNAPSHOT
  5. [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
  6. [WARNING]
  7. [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
  8. [WARNING]
  9. [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
  10. [WARNING]
  11. [INFO]
  12. [INFO] -------------------< com.bruceliu.elk.demo:elk-web >--------------------
  13. [INFO] Building elk-web 1.0-SNAPSHOT
  14. [INFO] --------------------------------[ jar ]---------------------------------
  15. [INFO]
  16. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ elk-web ---
  17. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  18. [INFO] Copying 1 resource
  19. [INFO] Copying 0 resource
  20. [INFO]
  21. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ elk-web ---
  22. [INFO] Changes detected - recompiling the module!
  23. [INFO] Compiling 2 source files to E:\workspace2017\elk-web\target\classes
  24. [INFO]
  25. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ elk-web ---
  26. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  27. [INFO] skip non existing resourceDirectory E:\workspace2017\elk-web\src\test\resources
  28. [INFO]
  29. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ elk-web ---
  30. [INFO] Nothing to compile - all classes are up to date
  31. [INFO]
  32. [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ elk-web ---
  33. [INFO] Tests are skipped.
  34. [INFO]
  35. [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ elk-web ---
  36. [INFO] Building jar: E:\workspace2017\elk-web\target\elk-web.jar
  37. [INFO]
  38. [INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ elk-web ---
  39. [INFO]
  40. [INFO] --- docker-maven-plugin:1.0.0:build (build-image) @ elk-web ---
  41. [INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
  42. [INFO] Copying E:\workspace2017\elk-web\target\elk-web.jar -> E:\workspace2017\elk-web\target\docker\elk-web.jar
  43. [INFO] Building image bruceliu/elk-web
  44. Step 1/6 : FROM java
  45. ---> d23bdf5b1b1b
  46. Step 2/6 : MAINTAINER bruceliu@email.com
  47. ---> Running in 787e4786fbd4
  48. ---> 4d4519f52fda
  49. Removing intermediate container 787e4786fbd4
  50. Step 3/6 : WORKDIR /ROOT
  51. ---> f40dcbc9a9eb
  52. Removing intermediate container 7fa6bbc9d1df
  53. Step 4/6 : ADD /elk-web.jar //
  54. ---> c7f1107ae3d4
  55. Removing intermediate container f370558f1a38
  56. Step 5/6 : ENTRYPOINT java -jar /elk-web.jar
  57. ---> Running in e4480ced0829
  58. ---> b634ca5fa5ad
  59. Removing intermediate container e4480ced0829
  60. Step 6/6 : CMD java -version
  61. ---> Running in cc6a064ef921
  62. ---> cf9a5d50326b
  63. Removing intermediate container cc6a064ef921
  64. Successfully built cf9a5d50326b
  65. [INFO] Built bruceliu/elk-web
  66. [INFO] Tagging bruceliu/elk-web with latest
  67. [INFO] ------------------------------------------------------------------------
  68. [INFO] BUILD SUCCESS
  69. [INFO] ------------------------------------------------------------------------
  70. [INFO] Total time: 10.329 s
  71. [INFO] Finished at: 2019-12-29T22:44:06+08:00
  72. [INFO] ------------------------------------------------------------------------
  73. Process finished with exit code 0

版权声明:本文为npeng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/npeng/p/14267007.html