zoukankan      html  css  js  c++  java
  • idea Maven 插件 docker-maven-plugin 打包docker镜像上传到远程仓库

    参考资料 https://my.oschina.net/u/3764794/blog/2995648

    原理:

    docker-maven-plugin 调用一个 docker 的api,进行打包镜像,tag标签,push到远程仓库。

    远程仓库的密码,在本地maven 的setting.xml里配置一个server ,idea根据id可以获取到远程仓库的账号,密码

    一、docker主机开启docker 远程访问API

    Ubuntu:

    vi /lib/systemd/system/docker.service
    

    Centos7

    vi /usr/lib/systemd/system/docker.service 
    
    找到ExecStart
    ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

    修改完后 重置重启

    systemctl daemon-reload
    systemctl restart docker

    防火墙开放端口:2375

    验证

    http://ip:2375/images/json  或 curl http:/ip:2375/info

    二、在docker主机配置远程仓库地址,以便非https 也能访问

    vi  /etc/docker/daemon.json
    {
    "registry-mirrors":  [
    "https://dockerhub.azk8s.cn",
    "https://reg-mirror.qiniu.com"
    ],
        "insecure-registries": ["registry1的IP地址:端口号","registry2的IP地址:端口号"]
    }

    修改完后 重置重启

    systemctl daemon-reload
    systemctl restart docker

    三、maven 配置 远程仓库的账号密码

    编辑 maven settings.xml文件

     <server>  
            <id>local_docker</id>  
             <username>lulu</username>
            <password>123456</password>
     </server> 

    四、pom 配置

    <properties>
        <project.name>bluetoothled</project.name>
    <!-- 私有仓库配置,需要settings.xml文件配合serverId对应的仓库服务 账号密码 -->
        <docker.serverId>local_docker</docker.serverId>
        <docker.registry>192.168.182.100:5000</docker.registry>
         <docker.host>http://192.168.182.100:2375</docker.host>
    </properties>
        <build>
            <plugins>
                <!-- Srping Boot 打包工具 打包成可执行的jar  -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
                <!--打包后 复制jar包到指定文件目录-->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <!-- here the phase you need -->
                            <phase>validate</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <!-- 复制资源后的输出目录 -->
                                <outputDirectory>target</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>src/main/docker</directory>
                                        <filtering>true</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <!-- docker插件 -->
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>1.2.0</version>
                    <configuration>
                        <!-- 私有仓库配置,需要settings.xml文件配合serverId对应的服务地址 -->
                        <serverId>${docker.serverId}</serverId>
                        <!-- docker私服地址 -->
                        <registryUrl>${docker.registry}</registryUrl>
                        <!-- 指定docker server的地址,该配置不需要本机安装docker -->
                        <dockerHost>${docker.host}</dockerHost>
                        <imageName>${project.name}/${project.artifactId}:${project.version}</imageName>
                        <imageTags>
                            <imageTag>${project.version}</imageTag>
                        </imageTags>
                        <!-- docker的构建目录(构建上下文),包含Dockerfile  -->
                        <dockerDirectory>target</dockerDirectory>
                        <resources>
                            <resource>
                                <targetPath>/</targetPath>
                                <directory>${project.build.directory}</directory>
                                <include>${project.build.finalName}.jar</include>
                            </resource>
                        </resources>
                    </configuration>
                    <executions>
                        <!-- package之前清除上一次构建的image -->
                        <execution>
                            <id>remove-image</id>
                            <phase>package</phase>
                            <goals>
                                <goal>removeImage</goal>
                            </goals>
                            <configuration>
                                <imageName>
                                   ${project.name}/${project.artifactId}
                                </imageName>
                                <imageTags>
                                    <imageTag>${project.version}</imageTag>
                                    <imageTag>latest</imageTag>
                                </imageTags>
                            </configuration>
                        </execution>
                        <execution>
                            <id>remove-tag-image</id>
                            <phase>package</phase>
                            <goals>
                                <goal>removeImage</goal>
                            </goals>
                            <configuration>
                                <imageName>
                                    ${docker.registry}/${project.name}/${project.artifactId}
                                </imageName>
                                <imageTags>
                                    <imageTag>${project.version}</imageTag>
                                    <imageTag>latest</imageTag>
                                </imageTags>
                            </configuration>
                        </execution>
                        <!-- 将docker:build绑定到package这个phase  -->
                        <execution>
                            <id>build-image</id>
                            <phase>package</phase>
                            <goals>
                                <goal>build</goal>
                            </goals>
                            <configuration>
                                <!-- imageName中若不指定tag,则会打上latest -->
                                <imageName>${project.name}/${project.artifactId}:${project.version}</imageName>
                                <!-- 可以使用<imageTags>标签打其他额外的tag -->
                            </configuration>
                        </execution>
                        <!-- 将docker:tag绑定到package这个phase  -->
                        <execution>
                            <id>tag-image</id>
                            <phase>package</phase>
                            <goals>
                                <goal>tag</goal>
                            </goals>
                            <configuration>
                                <!-- docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]  -->
                                <!-- images与IMAGE[:TAG]对应,必须在build阶段已经构建了 -->
                                <image>${project.name}/${project.artifactId}:${project.version}</image>
                                <!-- newName与tag命令的第二个参数对应 -->
                                <newName>
                                    ${docker.registry}/${project.name}/${project.artifactId}:${project.version}
                                </newName>
                            </configuration>
                        </execution>
                        <execution>
                            <id>tag-image-latest</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>tag</goal>
                            </goals>
                            <configuration>
                                <!-- docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]  -->
                                <!-- images与IMAGE[:TAG]对应,必须在build阶段已经构建了 -->
                                <image>${project.name}/${project.artifactId}:${project.version}</image>
                                <!-- newName与tag命令的第二个参数对应 -->
                                <newName>
                                    ${docker.registry}/${project.name}/${project.artifactId}:latest
                                </newName>
                            </configuration>
                        </execution>
                        <!-- 将docker:push绑定到deploy这个phase  -->
                        <execution>
                            <id>push-image</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>push</goal>
                            </goals>
                            <configuration>
                                <imageName>
                                    ${docker.registry}/${project.name}/${project.artifactId}:${project.version}
                                </imageName>
                            </configuration>
                        </execution>
                        <execution>
                            <id>push-image-latest</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>push</goal>
                            </goals>
                            <configuration>
                                <imageName>
                                    ${docker.registry}/${project.name}/${project.artifactId}:latest
                                </imageName>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
            </plugins>
        </build>

    docker-maven-plugin 详细说明请参考https://my.oschina.net/u/3764794/blog/2995648

    五、在idea中命令控制台中使用
    创建镜像
    mvn clean package docker:build
    推送镜像到Registry
    mvn clean package docker:build -DpushImage
    推送指定tag的镜像到Registry
    mvn clean package docker:build -DpushImageTag
    执行 build、tag、push 操作
    mvn deploy

    如果想跳过 docker 某个过程时,只需要:
    -DskipDockerBuild 跳过 build 镜像
    -DskipDockerTag 跳过 tag 镜像
    -DskipDockerPush 跳过 push 镜像
    -DskipDocker 跳过整个阶段

  • 相关阅读:
    linuxshell中"2>&1"含义
    Java中正数与负数操作>>、>>>的区别
    jsp el表达式
    struct2常用标签
    shell正则表达式
    IPV6学起来很费力?你看看,也就这样简单吧!
    STP、RSTP、MSTP合集思维导图
    HCIE之路--ISIS思维导图
    佛祖保佑,永不宕机! 永无BUG!
    震惊!ARP安全竟然还可以这样配置?
  • 原文地址:https://www.cnblogs.com/lucoo/p/11889282.html
Copyright © 2011-2022 走看看