idea Maven 插件 docker-maven-plugin 打包docker镜像上传到远程仓库的过程详解
原理:
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://www.jb51.net/article/224164.htm
五、在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 跳过整个阶段
到此这篇关于ideaMaven插件docker-maven-plugin打包docker镜像上传到远程仓库的文章就介绍到这了,更多相关ideaMaven插件docker-maven-plugin内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Springcloud+Mybatis使用多数据源的四种方式(小结)
这篇文章主要介绍了Springcloud+Mybatis使用多数据源的四种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09mybatis plus条件构造器queryWrapper、updateWrapper
这篇文章主要介绍了mybatis plus条件构造器queryWrapper、updateWrapper,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09
最新评论