java Docker - maven 在运行时连接到特定的存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28078524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:58:32  来源:igfitidea点击:

Docker - maven connect to specific repository in runtime

javaxmlmavendocker

提问by bsferreira

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.

我有一个需要在运行时连接到存储库的图像,但它接缝 Docker 容器无法读取位于主机中的 ~/.m2/settings.xml 文件。

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

有什么办法可以让 docker 现在在 maven 配置文件所在的位置吗?还是实际将maven配置文件导入到容器中?

回答by Mark O'Connor

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:

另请参阅以下示例,其中存储库在容器中运行并通过链接访问:

回答by bsferreira

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:

settings.xml 已添加到项目构建文件夹中,DockerFile 如下所示:

...
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):

在运行脚本文件中,我们添加了存储库(这将写入 /etc/hosts):

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

and now the application can resolve runtime dependencies!

现在应用程序可以解决运行时依赖关系!

回答by Walter A

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.

确保您没有在 /home/me 的子目录中启动 docker。或者,当您在 /home/me/myproject 中启动 docker 时,创建一个目录 /home/me/myproject/shareddir 并将 settings.xml 复制到该目录。