1. 程式人生 > >docker學習(六) 使用maven構建springboot docker映象並push到nexus

docker學習(六) 使用maven構建springboot docker映象並push到nexus

使用maven構建springboot docker映象並push到nexus

前面學習了使用gradle構建springboot docker映象,docker學習(三) gradle 使用docker外掛自動構建springboot工程,現在學習使用maven外掛構建,外掛採用 com.spotify docker-maven-pluginspringboot maven 構建docker映象工程github地址。 nexus的安裝可參考上一篇的介紹,maven環境的安裝不用介紹了,很簡單,可參閱資料安裝。

1 按照github地址構建一個springboot工程並配置外掛。pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<groupId>com.zqw</groupId> <artifactId>docker-maven</artifactId> <version>1.0</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId
>
<version>2.1.0.M1</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.1.1</version> <configuration> <imageName>192.168.1.4:7003/dockermaven:1.0</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> <serverId>docker</serverId> </configuration> </plugin> </plugins> </build> <!-- Add Spring repositories --> <!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>

下面是docker外掛的程式碼,imageName標籤指定映象名稱,前面的IP 埠與nexus一致,baseImage對應DockerFile的From entryPoint也與DockerFile的entryPoint一樣,指定啟動命令以及引數。

<plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.1.1</version>
                <configuration>
                    <imageName>192.168.1.4:7003/dockermaven:1.0</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>
                    <serverId>docker</serverId>
                </configuration>
            </plugin>

上面的配置中還有個serviceId標籤。對應maven settings.xml配置檔案的一個server

<server>
  <id>docker</id>
  <username>docker</username>
  <password>docker</password>
  <configuration>
    <email>[email protected]</email>
  </configuration>
</server>

其中id標籤對應上面的serviceId,username password email分別對應nexus倉庫對應的使用者名稱、密碼、email。
至此配置結束

2 執行命令 mvn clean package docker:build -DpushImage 。可以發現nexus中已經push成功了。如下:
這裡寫圖片描述