2016-08-24 59 views
1

我想弄清楚如何使用fabric8 docker-maven-plugin構建一個spring boot docker鏡像。該文檔包含位和字節,我顯然缺少一些東西。有沒有人有一個完整的pom.xml的例子呢?fabric8 springboot完整示例

回答

2

如果你不必使用那個插件,我推薦spotify的docker-maven-plugin。安裝完成後,您可以執行來構建泊塢窗圖像。

pom.xml看起來是這樣的:

... 
<properties> 
    <docker.image.prefix>springio</docker.image.prefix> 
</properties> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>com.spotify</groupId> 
      <artifactId>docker-maven-plugin</artifactId> 
      <version>0.4.11</version> 
      <configuration> 
       <imageName>${docker.image.prefix}/${project.artifactId}</imageName> 
       <dockerDirectory>src/main/docker</dockerDirectory> 
       <buildArgs> 
        <finalName>${project.build.finalName}.jar</finalName> 
       </buildArgs> 
       <resources> 
        <resource> 
         <targetPath>/</targetPath> 
         <directory>${project.build.directory}</directory> 
         <include>${project.build.finalName}.jar</include> 
        </resource> 
       </resources> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 
... 

Dockerfilesrc/main/docker看起來是這樣的:

FROM openjdk:8u102-jre 
ARG finalName 
ADD $finalName /my-app.jar 
ENTRYPOINT ["java","-jar","/my-app.jar"] 

參考文獻:

https://spring.io/guides/gs/spring-boot-docker

https://github.com/spotify/docker-maven-plugin

1

如果您只想快速入門,那麼fabric8 fabric8-maven-plugin文檔很難挖掘到,所以這裏有一個快速示例,您可以獲取構建Docker映像所需的所有內容。

首先,請確保docker位於您的路徑上,並且Docker守護進程正在運行。運行docker ps並確保這樣的迴應:

CONTAINER ID  IMAGE    COMMAND    CREATED    STATUS    PORTS    NAMES 

添加到您的pom.xml與

<build> 
    <plugins> 
     <plugin> 
      <groupId>io.fabric8</groupId> 
      <artifactId>fabric8-maven-plugin</artifactId> 
      <configuration> 
       <verbose>true</verbose> 
       <images> 
        <image> 
         <!-- Replace your-project-name --> 
         <name>your-project-name:${project.version}</name> 

         <build> 

          <!-- This is the same FROM used in a Dockerfile --> 
          <from>vixns/java8</from> 

          <!-- This is the same ENTRYPOINT used in a Dockerfile --> 
          <entryPoint> 
           <exec> 
            <arg>java</arg> 
            <!-- This extra argument is so Tomcat can start faster due to lack of entropy --> 
            <arg>-Djava.security.egd=file:/dev/./urandom</arg> 
            <arg>-jar</arg> 
            <!-- /maven/ is the default dir that the plugin copies your artifact to --> 
            <arg>/maven/${project.artifactId}.${project.packaging}</arg> 
           </exec> 
          </entryPoint> 

          <assembly> 
           <!-- This is a predefined assembly.xml that will only copy your final artifact to the Docker image --> 
           <descriptorRef>artifact</descriptorRef> 
          </assembly> 
         </build> 
        </image> 
       </images> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

注意運行:如果你想使用mvn docker:start你需要額外的設置命令。您不必使用它,如果您願意,可以使用標準docker命令。

0

Here is one of使用fabric8的工作示例。該項目使用Spring Boot Docker鏡像,然後將其鏈接到mongodb。

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
     <plugin> 
      <groupId>io.fabric8</groupId> 
      <artifactId>docker-maven-plugin</artifactId> 
      <version>0.20.0</version> 
      <configuration> 
       <!--<dockerHost>tcp://REMOTE_IP:2375</dockerHost> --> 
       <images> 
        <image> 
         <name>springboot-mongo-dockerimage:${project.version}</name> 
         <alias>springboot-mongo-dockerimage</alias> 
         <build> 
          <dockerFileDir>${project.basedir}</dockerFileDir> 

         </build> 
         <run> 
          <namingStrategy>alias</namingStrategy> 
          <dependsOn> 
           <container>mongo</container> 
          </dependsOn> 

          <links> 
           <link>mongo</link> 
          </links> 
          <ports> 
           <port>8080:8080</port> 
          </ports> 
         </run> 
        </image> 
       </images> 
      </configuration> 

      <executions> 
       <execution> 
        <id>start</id> 
        <phase>pre-integration-test</phase> 
        <goals> 
         <goal>stop</goal> 
         <goal>build</goal> 
         <goal>start</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

您可以follow this link的分步說明

然而,而不是建立從fabric8 Maven插件圖片,你可以使用Dockerfile我覺得這更方便的使用,這就是爲什麼你會發現

<build> 
    <dockerFileDir>${project.basedir}</dockerFileDir> 
</build> 

DokcerFile

FROM java:8 
VOLUME /tmp 
ADD target/Boot-0.0.1-SNAPSHOT.jar app.jar 
EXPOSE 8080 
RUN bash -c 'touch /app.jar' 
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://mongo/test", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] 

如果要將圖像推送到Docker中心註冊表,則可以使用this link