Docker - maven connect to specific repository in runtime

16,632

Solution 1

Regarding the feedback, we've came up with the following solution.

The settings.xml was added to the project build folder and the DockerFile looks like this:

...
RUN apt-get update
RUN apt-get install -y maven 
ADD /path/to/settings.xml /root/.m2/settings.xml
...

and in the run script file we added the repository (this will write on /etc/hosts):

...
docker run --add-host=myrepo.mycompany.com:<ip> ...
...

and now the application can resolve runtime dependencies!

Solution 2

You can do this by creating a volume mapping

docker run ... -v /path/on/host/settings.xml:/home/me/.m2/settings.xml ....

see also the following example where the repository is run within a container and accessed via a link:

Share:
16,632
bsferreira
Author by

bsferreira

Updated on June 16, 2022

Comments

  • bsferreira
    bsferreira almost 2 years

    I've an image which needs to connect to a repository in runtime, but it seams that Docker container is not able to read ~/.m2/settings.xml file located in host machine.

    Is there any way to let docker now where maven configuration file is located? Or actually import the maven configuration file to the container?

  • bsferreira
    bsferreira over 9 years
    I'm not sure if I explained myself well. When I said host machine, I mean the machine where docker seats on. So, I've tried -v /home/user/.m2/settings.xml, but I get the same response (not finding the jar in the repo). Do you have any other clue? do I need to install maven in the container?
  • Mark O'Connor
    Mark O'Connor over 9 years
    @RodreNathan Now I'm confused... If you're trying to run Maven, then yes it must be installed in the container.
  • bsferreira
    bsferreira over 9 years
    Yes, I've installed it and made some configurations, which I'll post for the record and help someone with the same problem.
  • Mark O'Connor
    Mark O'Connor over 9 years
    Check out a similar answer. Uses the official Maven image (no need to build one). Additionally instead of an add-host, uses a Nexus image running in a linked container. See: stackoverflow.com/questions/27767264/…
  • Carlos
    Carlos about 8 years
    You can also declare a VOLUME in order to be able to set or change the settings file at "docker run" time.