docker-compose build 然后 因为网络原因build 剧慢。
我自己也有梯子,奈何 build 使用maven 构建包的时候不走梯子。
解决思路,让容器内使用梯子。
1.让容器使用宿主机的网络,让梯子的端口映射到容器内。
这是docker-compose.yaml的内容,network 参数要搭配 version: "3.4"使用
version: "3.4"
services:
client:
build:
context: ../docker/ops-playground-image
network: host
2.让maven打包使用代理 这是build 的Dockerfile 内容
FROM maven:3.8-eclipse-temurin-17 AS builder
env HTTP_PROXY "http://127.0.0.1:7890"
env HTTPS_PROXY "http://127.0.0.1:7890"
env FTP_PROXY "http://127.0.0.1:7890"
env MAVEN_OPTS="-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=7890"
这样使用mvn clean & install 的时候就走代理了
======================================================
改进方案,不用上面的方案
把 settings.xml 放到dockerfile 的同目录
<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/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<proxies>
<proxy>
<id>http-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>hostip</host>
<port>7890</port>
<!-- <username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>www.google.com|*.example.com</nonProxyHosts>
-->
</proxy>
<proxy>
<id>https-proxy</id>
<active>true</active>
<protocol>https</protocol>
<host>hostip</host>
<port>7890</port>
<!-- <username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>www.google.com|*.example.com</nonProxyHosts>
-->
</proxy>
</proxies>
<activeProfiles/>
<profiles>
<profile>
<repositories>
<repository>
<id>aliyunmaven</id>
<name>aliyunmaven</name>
<url>https://maven.aliyun.com/repository/public</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>MavenCentral</id>
<url>http://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>aliyunmavenApache</id>
<url>https://maven.aliyun.com/repository/apache-snapshots</url>
</repository>
</repositories>
</profile>
</profiles>
</settings>
改造dockerfile
RUN apt-get update&&apt-get install -y iproute2 &&\
rm /usr/share/maven/conf/settings.xml && \
cp /usr/share/maven/conf/settings2.xml /usr/share/maven/conf/settings.xml && \
echo "$(ip route show | awk '/default/ {print $3}') hostip">>/etc/hosts && \
mvn clean verify
```