Home » Java » Docker – maven connect to specific repository in runtime

Docker – maven connect to specific repository in runtime

Posted by: admin December 28, 2021 Leave a comment


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?


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:


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!


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.