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成功了。如下:
这里写图片描述

上一篇 使用nexus管理docker镜像

猜你喜欢

转载自blog.csdn.net/u011943534/article/details/81334900