6

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
  • 1,189
  • 5
  • 14
  • 27

3 Answers3

8

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:

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • 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? – bsferreira Jan 22 '15 at 10:15
  • 1
    @RodreNathan Now I'm confused... If you're trying to run Maven, then yes it must be installed in the container. – Mark O'Connor Jan 22 '15 at 20:18
  • Yes, I've installed it and made some configurations, which I'll post for the record and help someone with the same problem. – bsferreira Jan 24 '15 at 15:36
6

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!

bsferreira
  • 1,189
  • 5
  • 14
  • 27
  • 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: http://stackoverflow.com/questions/27767264/how-to-dockerize-maven-project-and-how-many-ways-to-accomplish-it/27768965#27768965 – Mark O'Connor Jan 25 '15 at 18:06
  • You can also declare a VOLUME in order to be able to set or change the settings file at "docker run" time. – Carlos Mar 04 '16 at 14:12
0

Make sure you don't start docker in a subdir of /home/me. Or, when you are starting docker in /home/me/myproject, make a directory /home/me/myproject/shareddir and copy settings.xml to this dir.

Walter A
  • 19,067
  • 2
  • 23
  • 43