2

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]
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
cppcoder
  • 22,227
  • 6
  • 56
  • 81

3 Answers3

8

Actually, you can pass MAVEN_OPTS as a build argument but you first must declare it in the Dockerfile like this (also, you can use the exec form of run):

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

Then your docker build would look like this:

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" .
clyon
  • 151
  • 1
  • 3
4

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

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
1

You need to update the maven settings file '~/.m2/settings.xml' to add the proxy configuration.

<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>

Take a look at the following https://github.com/alirizasaral/Maven-with-Proxy/ . You can do something very similar where you add to your image a template maven settings.xml and you can do an envsubst step in the Dockerfile where you replace proxy value placeholders with the ones passed as build args.

This is better than hardcoding the proxy values inside the setting.xml as you might want to build the image with a different proxy.

yamenk
  • 46,736
  • 10
  • 93
  • 87