idea Maven 插件 docker-maven-plugin 打包docker镜像上传到远程仓库
原理:
docker-maven-plugin 调用一个 docker 的api,进行打包镜像,tag标签,push到远程仓库。
远程仓库的密码,在本地maven 的setting.xml里配置一个server ,idea根据id可以获取到远程仓库的账号,密码
一、docker主机开启docker 远程访问API
Ubuntu:
|
1
|
vi /lib/systemd/system/docker.service |
Centos7
|
1
|
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
修改完后 重置重启
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>
