继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Docker:使用 Maven 插件构建镜像

aluckdog
关注TA
已关注
手记 443
粉丝 68
获赞 393
  • 通过 Maven 的 Docker 插件可以构建 Docker 镜像

快速入门

  • 在 pom.xml 中添加 Docker 插件

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.4.13</version>
    <configuration>
        <imageName>linyuantongxue/docker-demo:0.0.1</imageName> // 指定镜像名称,linyuantongxue  是仓库名称(对应 DockerHub 用户名),docker-demo 是镜像名称(对应 DockerHub 仓库名),0.0.1 是标签名称(相当于版本号)        <baseImage>java</baseImage>     // 指定基础镜像,等同 FROM 指令        <entryPoint>["java","-jar","app.jar"]</entryPoint>       // 等同于 ENTRYPOINT 指令        <resources>
            <resource>
                <targetPath>/</targetPath>  
                <directory>${project.build.directory}</directory>   // 指定要复制的根目录,${project.build.directory} 表示 target 目录                <include>${project.build.finalName}.jar</include>   // 指定要复制的文件,${project.build.finalName}.jar 指打包后的 jar 文件            </resource>
        </resources>
    </configuration></plugin>
  • 执行以下命令构建 Docker 镜像

mvn clean package docker:build
  • 执行 docker images 查看刚才构建的镜像

读取 Dockerfile 文件

  • 读取 Dockerfile 文件就不必指定 baseImage 和 entrypoint

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.4.13</version>
    <configuration>
        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>     // 指定要读取的 Dockerfile 文件        <imageName>linyuantongxue/docker-demo:0.0.1</imageName>  // 指定镜像名称,linyuantongxue  是仓库名称(对应 DockerHub 用户名),docker-demo 是镜像名称(对应 DockerHub 仓库名),0.0.1 是标签名称(相当于版本号)        <resources>
            <resource>
                <targetPath>/</targetPath>  
                <directory>${project.build.directory}</directory>   // 指定要复制的根目录,${project.build.directory} 表示 target 目录                <include>${project.build.finalName}.jar</include>   // 指定要复制的文件,${project.build.finalName}.jar 指打包后的 jar 文件            </resource>
        </resources>
    </configuration></plugin>

将插件绑定在某个 phase 执行

  • 很多场景下有这样的需求,比如执行 mvn clean package 时插件就自动构建 Docker 镜像,要实现这点只需要将插件的 goal 绑定在某个 phase 即可

  • maven 命令格式是:mvn phase:goal,phase 绑定了目标的构建生命周期阶段,goal 配置的执行目标

  • 只需添加如下配置:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.4.13</version>
    // 在 maven 生命周期 package 中执行 build 构建目标    <executions>
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
    // $$$$$$$$$$$$$$$$华丽的分割线$$$$$$$$$$$$$$$$    <configuration>
        <imageName>linyuantongxue/docker-demo:0.0.1</imageName>
        <baseImage>java</baseImage>
        <entryPoint>["java","-jar","app.jar"]</entryPoint>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration></plugin>

推送镜像

  • 使用 Maven 插件也可以推送镜像到 Docker Hub

  • 修改 Maven 全局配置信息文件 settings.xml,配置 Docker Hub 用户信息

<servers>
    <server>
        <id>docker-hub</id>
        # DockerHub 该网站的用户名必须全部为小写才正确        <username>linyuantongxue</username>
        <password>765371578Ly</password>
        <configuration>
            <email>765371578@qq.com</email>
        </configuration>
    </server></servers>
  • 修改 pom 文件

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.4.13</version>
    <configuration>
        <imageName>linyuantongxue/docker-demo:0.0.1</imageName>
        <baseImage>java</baseImage>
        <entryPoint>["java","-jar","app.jar"]</entryPoint>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
        <!--与配置文件 setting.xml 中的 server.id 一致,用于推送镜像-->
        <serverId>docker-hub</serverId>
    </configuration></plugin>
  • 执行以下命令,添加 pushImage 标识,表示推送镜像

mvn clean package docker:build -DpushImage
  • 上面例子中通过 imageName 指定镜像名称和标签,也可以借助 imageTags 元素更为灵活的指定镜像名称和标签,这样就可以为同一个镜像指定两个标签

<configuration>
    <imageName>linyuantongxue/docker-demo</imageName>
    <imageTags>
        <imageTag>0.0.1</imageTag>
        <imageTag>latest</imageTag>
    </imageTags></configuration>
  • 也可在构建命令时使用 dockerImageTags 参数指定标签名称

mvn clean package:build -DpushImageTags -DdockerImageTags=latest -DdockerImageTags=another-tag
  • 若需要重复构建相同标签名称的镜像,可将 forceTags 设置为 true

<configuration>
    // .......    <forceTags>true</forceTags></configuration>



作者:林塬
链接:https://www.jianshu.com/p/c9d771a193b4

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP