之所以如此操作,主要原因是,官版的镜像中默认的setting.xml已内置好,不容易修改,
重新二次打包,可以指定我们自己的setting.xml配置,配置自己的私服地址以及解决默认Maven仓库国内下载速度慢的问题
一、创建操作目录
$ cd ~
$ mkdir -p dockerfiles/maven
二、创建 settings.xml 和 Dockerfile
$ cd dockerfiles/maven
$ touch settings.xml Dockerfile
三、编辑 settings.xml 以下供参考
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/root/.m2</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>maven-public</id>
<username>anonymous</username>
</server>
<server>
<id>maven-inner</id>
<username>test</username>
<password>test</password>
</server>
<server>
<id>maven-releases</id>
<username>test</username>
<password>test</password>
</server>
<server>
<id>maven-snapshots</id>
<username>test</username>
<password>test</password>
</server>
<server>
<id>docker-registry</id>
<username>test</username>
<password>test</password>
</server>
</servers>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!--配置阿里云maven私有仓库(即配阿里私服)-->
<mirror>
<id>alimaven</id>
<mirrorOf>*</mirrorOf>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>
<!-- maven官方镜像 -->
<mirror>
<id>mirrorId</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name </name>
<url>https://repo1.maven.org/maven2/</url>
</mirror>
<!-- 阿里云镜像1 -->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://central.maven.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 阿里云镜像2 -->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/repositories/central/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>chenrise-outer</id>
<repositories>
<repository>
<id>maven-public</id>
<name>Chenrise Maven Http</name>
<url>http://maven.test.com/repository/maven-public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>chenrise-inner</id>
<repositories>
<repository>
<id>maven-work</id>
<name>Chenrise Maven Http</name>
<url>http://192.168.1.190:8081/repository/maven-public/</url>
<!-- <url>http://nexus.jljianxing.work/repository/maven-public/</url>-->
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>chenrise-inner</activeProfile>
<!-- <activeProfile>chenrise-inner</activeProfile>-->
</activeProfiles>
</settings>
编辑settings.xml,主要是对maven的repository与mirror进行配置,内容如下
四、编辑Dockerfile,
来源于 maven:3.6.0-jdk-8-alpine,把编写好的配置settings.xml复制到镜像内即可
FROM maven:3.6.0-jdk-8-alpine
COPY settings.xml /usr/share/maven/ref/
五、构建镜像
$ docker build -t maven:3.5-jdk-8 .
其中maven指的是自定义镜像中的 REPOSITORY
3.5-jdk-8指的是自定义镜像中的 TAG
六、查看镜像
$ docker image ls
七、将自定义的镜像推送到自己的harbor仓库
# 如服务器未登录过,需先使用以下命令登录后再推送
# $ docker login harbor.test.com
$ docker push harbor.test.com/jx-common/maven:3.5-jdk-8