【问题标题】:Maven cannot connect to network inside dockerMaven无法连接到docker内部的网络
【发布时间】:2017-08-26 05:07:34
【问题描述】:

我正在尝试克隆一个 git 项目并在 docker 内执行 mvn package。但是 maven 无法连接到网络来下载依赖项。这是Dockerfile

FROM java:8
FROM maven
ADD id_rsa /root/.ssh/id_rsa
ADD known_hosts /root/.ssh/known_hosts
RUN git clone git@myhub.mygithub.com:project/myapp.git
WORKDIR myapp
RUN mvn package

这是maven构建命令:

sudo docker build --build-arg http_proxy=http://proxy.in.my.com:80  
--build-arg https_proxy=http://proxy.in.my.com:80  
--build-arg ftp_proxy=http://proxy.in.my.com:80  
--build-arg no_proxy=localhost,127.0.0.1,.us.my.com,.my.com   
-t myapp .  

mvn package 期间出现以下错误:

Downloading: https://repo.maven.apache.org/maven2/org/jacoco/jacoco-maven-plugin/0.7.6.201602180812/jacoco-maven-plugin-0.7.6.201602180812.pom  

[ERROR] Plugin org.jacoco:jacoco-maven-plugin:0.7.6.201602180812 or one of its dependencies could not be resolved:   
Failed to read artifact descriptor for org.jacoco:jacoco-maven-plugin:jar:0.7.6.201602180812: Could not transfer artifact org.jacoco:jacoco-maven-plugin:pom:0.7.6.201602180812 from/to central (https://repo.maven.apache.org/maven2): Network is unreachable (connect failed) -> [Help 1]

【问题讨论】:

    标签: maven docker


    【解决方案1】:

    实际上,您可以将 MAVEN_OPTS 作为构建参数传递,但您首先必须像这样在 Dockerfile 中声明它(也可以使用 run 的 exec 形式):

    FROM maven
    ARG MAVEN_OPTS
    RUN ["mvn", "package"]
    

    那么您的 docker 构建将如下所示:

    docker build -t my_image --build-arg http_proxy=http://proxy:3128 --build-arg https_proxy=http://proxy:3128 --build-arg MAVEN_OPTS="-Dhttp.proxyHost=proxy -Dhttp.proxyPort=3128 -Dhttps.proxyHost=proxy -Dhttps.proxyPort=3128" .
    

    【讨论】:

    • 你救了我。谢谢!
    • 非常感谢!此答案应标记为正确答案!
    【解决方案2】:

    问题是您传递了构建参数,但没有在 Dockerfile 中的任何地方使用它们。传递参数与传递环境变量不同。

    所以更新你的 dockerfile。此外,您还有两个 FROM,它们是有效的,因为现在是多阶段构建,但您只需要 maven。

    您可以通过两种方式构建文件

    FROM maven
    ARG http_proxy
    ENV http_proxy=${http_proxy}
    RUN git clone git@myhub.mygithub.com:project/myapp.git
    

    这将为完整映像设置环境,并且当您运行映像时,代理将已设置为容器。如果你只需要这个来做 git clone 然后使用下面的方法

    FROM maven
    ARG http_proxy
    RUN http_proxy=${http_proxy} git clone git@myhub.mygithub.com:project/myapp.git
    

    这只会设置克隆的参数,并且您的图像在运行时不会使用代理。

    Edit-1

    Maven 似乎不尊重 http_proxy。所以你需要在 maven config 中自己指定代理。配置位于 maven 映像内的 /usr/share/maven/conf/settings.xml

    有一个代理部分默认注释

       |-->
      <proxies>
        <!-- proxy
         | Specification for one proxy, to be used in connecting to the network.
         |
        <proxy>
          <id>optional</id>
          <active>true</active>
          <protocol>http</protocol>
          <username>proxyuser</username>
          <password>proxypass</password>
          <host>proxy.host.net</host>
          <port>80</port>
          <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
        </proxy>
        -->
      </proxies>
    

    取消注释并在您的主机目录中创建配置文件。使用 Dockerfile 中的 COPY 命令复制文件。现在maven也应该使用代理了

    【讨论】:

    • 我曾尝试直接在 Dockerfile 中提供代理,如下所示:ENV http_proxy 'proxy'。即使这样也行不通。我需要 maven 的代理
    • 哦,好的,让我编辑答案。 maven 需要特殊设置
    • 我应该将文件复制到哪里?
    • 我明白了。我将它复制到 /root/.m2/settings.xml 并且它有效
    【解决方案3】:

    您需要更新 maven 设置文件 '~/.m2/settings.xml' 以添加代理配置。

    <proxies>
     <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>$PROXY_PROTOCOL</protocol>
      <username>$PROXY_USER</username>
      <password>$PROXY_PASS</password>
      <host>$PROXY_HOST</host>
      <port>$PROXY_PORT</port>
      <nonProxyHosts>$NO_PROXY</nonProxyHosts>
     </proxy>
    </proxies>
    

    看看下面的https://github.com/alirizasaral/Maven-with-Proxy/。您可以执行非常类似的操作,将模板 maven settings.xml 添加到图像中,并且可以在 Dockerfile 中执行 envsubst 步骤,将代理值占位符替换为作为构建参数传递的占位符。

    这比在 setting.xml 中硬编码代理值要好,因为您可能希望使用不同的代理构建图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多