SpringBoot中使用Maven插件,上傳docker鏡像

開啓docker遠程端口

我上一篇裏面寫了,這裏暴露的路徑:html

18.16.202.95:2375

簡單構建

配置pom.xml文件

在properties中增長一行指定遠程主機的位置java

<properties>
    <java.version>1.8</java.version>
    <dockerHost>http://18.16.202.95:2375</dockerHost>
  </properties>

增長一個plugingit

<plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>0.4.12</version>
        <configuration>
          <imageName>com.huishi.demo</imageName>
          <!--          <imageName>${project.build.finalName}</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>

或者就用一個整個,dockerHost配置設置在插件裏面:github

<plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>0.4.12</version>
        <configuration>
          <imageName>com.huishi.demo</imageName>
          <!--          <imageName>${project.build.finalName}</imageName>-->
          <baseImage>java</baseImage>
          <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
          <dockerHost>http://18.16.202.95:2375</dockerHost>
          <resources>
            <resource>
              <targetPath>/</targetPath>
              <directory>${project.build.directory}</directory>
              <include>${project.build.finalName}.jar</include>
            </resource>
          </resources>
        </configuration>
      </plugin>

這種方式也是能夠的。web

configuration配置說明:docker

  • imageName:用於指定鏡像的完整名稱,其中{docker.registry}爲註冊中心地址,{project.groupId}爲倉庫名稱,{project.artifactId}爲鏡像名稱,${project.version}爲鏡像標籤名.
  • dockerDirectory:用於指定Dockerfile文件所在的目錄.
  • resources.resource.directory:用於指定須要複製的根目錄,${project.build.directory}表示target目錄.
  • resources.resource.include:用於指定須要複製的文件,${project.build.finalName}.jar表示生成的jar包.

configuration中也能夠添加apache

<forceTags>true</forceTags>

用於每次新的構建上覆蓋鏡像tags,若是若是已經存在該tag,那麼舊的鏡像將會設爲nonetomcat

構建鏡像:

mvn -DskipTests clean package docker:build
mvn clean package docker:build -Dmaven.test.skip=true

查看:bash

[root@slaver2 /]# docker images
REPOSITORY                                 TAG                 IMAGE ID            CREATED              SIZE
com.huishi.demo                            latest              860fe1ab3334        About a minute ago   662MB

啓動鏡像:

[root@slaver2 /]# docker run -p 18080:8080 com.huishi.demo

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-09-12 12:08:58.997  INFO 1 --- [           main] com.huishi.demo.DemoApplication          : Starting DemoApplication v0.0.1-SNAPSHOT on 70b1ca956ab4 with PID 1 (/demo-0.0.1-SNAPSHOT.jar started by root in /)
2019-09-12 12:08:59.001  INFO 1 --- [           main] com.huishi.demo.DemoApplication          : No active profile set, falling back to default profiles: default
2019-09-12 12:09:02.355  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-09-12 12:09:02.439  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-12 12:09:02.439  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-12 12:09:02.681  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-12 12:09:02.682  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3526 ms
2019-09-12 12:09:03.527  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-12 12:09:04.218  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-12 12:09:04.236  INFO 1 --- [           main] com.huishi.demo.DemoApplication          : Started DemoApplication in 6.877 seconds (JVM running for 8.01)
2019-09-12 12:09:27.255  INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-09-12 12:09:27.256  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-09-12 12:09:27.292  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 36 ms

訪問測試:

curl -XGET http://18.16.202.95:18080
Hello Docker World

使用Dockerfile進行構建

/src/main/docker目錄下,創建文件Dockerfileapp

FROM java:8
VOLUME /tmp
ADD demo-0.0.1.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

使用-Djava.security.egd=file:/dev/./urandom加快隨機數產生過程。

修改pom.xml配置:

<plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>0.4.12</version>
        <configuration>
          <imageName>hongdada/${project.groupId}.${project.artifactId}</imageName>
          <dockerHost>http://18.16.202.95:2375</dockerHost>
          <!-- 指定Dockerfile所在的路徑 -->
          <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
          <forceTags>true</forceTags>
          <imageTags>
            <imageTag>${project.version}</imageTag>
            <imageTag>latest</imageTag>
          </imageTags>
          <resources>
            <resource>
              <targetPath>/</targetPath>
              <directory>${project.build.directory}</directory>
              <include>${project.build.finalName}.jar</include>
            </resource>
          </resources>
        </configuration>
      </plugin>

其餘步驟同樣。這樣便可使用Dockerfile進行構建Docker鏡像啦。

異常

這裏要注意的是,imageName必須符合正則[a-z0-9-_.],docker的imageName只能是小寫字母,數字,點號,下劃線

${project.build.finalName}產出物名稱,缺省爲${project.artifactId}-${project.version}當前版本

若是要使用SpringBootimageName要設置爲${project.build.finalName},注意修改version,通常默認爲0.0.1-SNAPSHOT,注意大寫字母

參考:

使用Maven插件構建Docker鏡像

Docker系列教程12-使用Maven插件構建Docker鏡像

使用Docker部署SpringBoot

maven docker 插件集成的幾個小坑

docker與Spring boot的集成:docker-maven-plugin使用

相關文章
相關標籤/搜索