1. 程式人生 > 其它 >使用docker-maven-plugin外掛構建映象並上傳到倉庫

使用docker-maven-plugin外掛構建映象並上傳到倉庫

技術標籤:docker

1、首先需要修改docker的配置,docker預設未啟用遠端訪問的,所以我們需要開啟docker的遠端訪問。

vim /lib/systemd/system/docker.service
在ExecStart後面新增-Htcp://0.0.0.0:2375-Hunix://var/run/docker.sock
#重啟docker服務
systemctl daemon-reload
systemctl restart docker

pom.xml中配置docker-maven-plugin外掛

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.macro.mall</groupId>
    <artifactId>mall-admin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mall-admin</name>
    <description>mall-admin project for mall</description>

    <parent>
        <groupId>com.macro.mall</groupId>
        <artifactId>mall</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.macro.mall</groupId>
            <artifactId>mall-mbg</artifactId>
        </dependency>
        <dependency>
            <groupId>com.macro.mall</groupId>
            <artifactId>mall-security</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
        </dependency>
        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
        </dependency>
    </dependencies>
    <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.2.2</version>
                <configuration>
                    <imageName>${project.artifactId}:${project.version}<!--映象名稱-->
                    </imageName>
                    <baseImage>java:8</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>
                    <dockerHost>http://192.168.119.141:2375</dockerHost>
                </configuration>
                <!--配置maven package和docker builder進行繫結-->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>tag-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>tag</goal>
                        </goals>
                        <configuration>
                            <image>${project.artifactId}:${project.version}</image>
                            <newName>${project.artifactId}:${project.version}</newName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>push-image</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <imageName>${project.artifactId}:${project.version}</imageName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>