记录 docker-compose build 的一次过程

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
     ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值