天天看點

通過maven插件建構docker鏡像

通過maven插件建構docker鏡像:
    1、在pom.xml 中添加maven的docker插件。
         <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.4.13</version>
            <configuration>
                <imageName>jingdata/microservice-discovery-eureka:0.0.1</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>
        
  2、建構docker鏡像:  mvn  clean   package   docker:build
  3、執行docker images 檢視剛建構的鏡像
  4、啟動鏡像:  docker  run -d -p  8761:8761  jingdata/microservice-discovery-eureka:0.0.1
  5、通路測試:  128.0.0.1:8761 
             

插件讀取dockerfile進行建構:

1、在src/main目錄下,建立檔案夾docker,在檔案夾中建立一個Dockerfile檔案。
      FROM java:8
      VOLUME /tmp
      ADD  microservice-discovery-erueka-0.0.1-SNAPSHOT.jar   app.jar 
      RUN  bash   -c  'touch /app.jar'
      EXPOSE  9000
      ENTRYPOINT  ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
      修改pom.xml檔案:
       <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.4.13</version>
            <configuration>
                <imageName>jingdata/microservice-discovery-eureka:0.0.2</imageName>
                <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
        
  2.将插件綁定在沒某個phase執行
       <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.4.13</version>
            <executions>
                <execution>
                    <id>build-image</id>
                    <phase>package</phase>
                    <goals>
                        <goal>build</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <imageName>jingdata/microservice-discovery-eureka:0.0.3</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>      
  
  3.推送鏡像
    (1)、修改maven的全局配置檔案settings.xml,在其中添加docker hub的使用者資訊。
          <server>
              <id>docker-hub</id>
              <username>docker-hub的使用者名</username>
              <password>docker-hub的密碼</password>
              <configuration>
                <email>docker-hub的郵箱</email>
              </configuration>
          </server> 
    (2)、修改pom.xml檔案
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.4.13</version>
            <configuration>
                <imageName>jingdata/microservice-discovery-eureka:0.0.4</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>
                
                <!-- 與maven配置檔案settings中配置的server id一緻,用于推送鏡像--->
                <serverId>docker-hub</serverId>
            </configuration>
        </plugin>
   (3)、添加pushImage标示,推送鏡像
          mvn clean package docker:build  -DpushImage
          
   (4)、推送到私有倉庫位址:
          <imageName>localhost:5000/jingdata/microservice-discovery-eureka:0.0.4</imageName>