Maven无法连接到Docker内部的网络 [英] Maven cannot connect to network inside docker
问题描述
我正在尝试克隆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]
问题是您正在传递构建args,但未在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
这只会设置克隆参数,并且图像在运行时将不会使用代理.
编辑1
行家似乎不尊重http_proxy.因此,您需要在maven配置中自行指定代理.该配置位于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还应该使用代理
I am trying to clone a git project and do mvn package
inside a docker. But maven is unable to connect to network to download dependencies. This is the 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
This is the maven build command:
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 .
I am getting the following error during 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]
The problem is you are passing build args but not using them anywhere in your Dockerfile. Passing a argument is not same as passing a Environment variable.
So update your dockerfile. Also you have two FROM
they are valid because of multi stage build now but you only need maven in this.
You can build your file two ways
FROM maven
ARG http_proxy
ENV http_proxy=${http_proxy}
RUN git clone git@myhub.mygithub.com:project/myapp.git
This will set the environment for complete image and when you run the image the proxy will be already set it container. If you only need this for doing git clone then use the below approach
FROM maven
ARG http_proxy
RUN http_proxy=${http_proxy} git clone git@myhub.mygithub.com:project/myapp.git
This will only set the argument for cloning and your image will not use proxy when it is run.
Edit-1
Maven it seems doesn't honor http_proxy. So you need to specify the proxy yourself in maven config. The config is located at /usr/share/maven/conf/settings.xml
inside maven image.
There is a section for proxies which is commented by default
|-->
<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>
Uncomment it and create the config file in your host directory. Copy the file using COPY
command in your Dockerfile. Now maven also should use the proxy
这篇关于Maven无法连接到Docker内部的网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!