2

I have to build a docker image and package java application using maven in the docker container but when I run the build process all is going fine but all maven dependencies downloading from maven remote repo.

That is my docker file:

FROM ubuntu_img
CMD ./mvnw -s .mvn/settings.xml --batch-mode clean package

How can I configure docker or maven for downloading dependencies from maven local repository located on my laptop?

John
  • 1,375
  • 4
  • 17
  • 40
  • Or you can package locally and add to container. – pmverma Oct 09 '19 at 07:52
  • @pmverma no I have to run the maven build in the docker container because all tests have to be executed in the docker environment. – John Oct 09 '19 at 07:56
  • You can also map local m2 repo to container m2 repo in which case it won't download from remote. – pmverma Oct 09 '19 at 08:02
  • With `docker` experimental features enabled and using `buildkit` you can mount a cache volume during the builds, check [this answer](https://stackoverflow.com/a/57789560/1423507). – masseyb Oct 09 '19 at 08:14

2 Answers2

3

At first, you need to attach the directory of your existing local Maven repository into the Docker container:

VOLUME ["/home/<user>/.m2", "/root/.m2"]

Then you need to tell Maven (inside your container) to use this directory as a local repository.

setting.xml

<settings ...>
    <localRepository>/root/.m2</localRepository>
    ...
</settings>
zappee
  • 20,148
  • 14
  • 73
  • 129
  • where I should write ```VOLUME ["/home//.m2", "/root/.m2"]``` in my docker file? – John Oct 09 '19 at 09:02
  • You need to add 'VOLUME ...' into the docker-compose.yml file. More info here: http://blog.code4hire.com/2018/06/define-named-volume-with-host-mount-in-the-docker-compose-file/ – zappee Oct 09 '19 at 09:07
  • More examples are here: https://stackoverflow.com/questions/42248198/how-to-mount-a-single-file-in-a-volume – zappee Oct 09 '19 at 09:12
  • Can I mount the volume in docker file on build image step without docker-compose.yml file? – John Oct 09 '19 at 11:29
  • This is a different question. I think that, the best you can do is to complete this question then you can search for it on the Internet or ask a new question. – zappee Oct 09 '19 at 11:35
  • Anyway, have you checked this documentation? https://dmp.fabric8.io – zappee Oct 10 '19 at 08:26
  • I'm new in docker and I need to solve the issue very quickly but I don't understand what is going on in the dockers. I tried to find a solution but I couldn't. And no I didn't see this documentation. – John Oct 10 '19 at 08:38
  • I don't understand why no simple solution for this simple issue mount local maven repo on the build stage in docker? I'm very disappointed. – John Oct 10 '19 at 08:41
0

use volume,like this:

VOLUME ["/home/test/.m2", "/root/.m2"]

Willing
  • 66
  • 7