将Maven存储库挂载到Docker [英] Mounting Maven Repository to Docker
问题描述
我正在尝试构建一个Java应用程序并使用docker制作一个软件包.这个构建需要一个Maven存储库,因为它很大,所以我不想包含在映像中.我想尝试使用卷并将本地maven存储库安装到映像中的maven存储库.为了使maven可用,我使用了 apt-get install -y maven
,但是在图像 $ HOME 中找不到目录
.m2
.代码>.
I am trying to build a Java application and make a package using docker. This builds needs a maven repository which I don't want to include in the image, since it's very large. I wanted to try using volumes and mount my local maven repository to the maven repository in the image. I used apt-get install -y maven
in order to have maven available, but I can't find the directory .m2
in the image $HOME
.
我用 ls -la $ HOME
, ls -la
和 ls -la/root
查找行家,但是有那里没有 .m2
目录.
I used ls -la $HOME
, ls -la
and ls -la /root
to find the maven home, but there is no .m2
directory there.
我在 Dockerfile
中有以下几行:
FROM ubuntu
MAINTAINER Zeinab Abbasimazar
# Install and configure required packages
RUN apt-get update; \
apt-get install -y --no-install-recommends apt-utils; \
apt-get install -y dialog; \
apt-get install -y wget unzip curl maven; \
mkdir $HOME/.m2/; \
ls -la /usr/share/maven/conf/; \
echo \
"<settings xmlns='http://maven.apache.org/SETTINGS/1.0.0\' \
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' \
xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd'> \
<localRepository>/root/.m2/repository</localRepository> \
<interactiveMode>true</interactiveMode> \
<usePluginRegistry>false</usePluginRegistry> \
<offline>false</offline> \
</settings>" \
> /usr/share/maven/conf/settings.xml
VOLUME ["/home/zeinab/.m2/", "/root/.m2/"]
# Build
RUN mvn -X clean install -pl components -P profile
将本地存储库配置放在映像的maven配置文件中,将本地maven存储库挂载到映像中的目录,最后执行构建.正如我在Maven构建日志中看到的那样,它正在使用我期望的本地存储库路径:
Which puts local repository configurations in image's maven configuration file, mounts my local maven repository to a directory in the image and finally performs the build. As I can see in the maven build log that it's using the local repository path I expected:
[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
[DEBUG] Reading user settings from /root/.m2/settings.xml
[DEBUG] Using local repository at /root/.m2/repository
但是仍然无法检测到依赖关系.
But still can't detect dependencies.
推荐答案
我终于找到了在docker中挂载本地maven存储库的解决方案.我改变了解决方案;我将其安装在 run
阶段,而不是 build
阶段.这是我的 Dockerfile
:
I finally found the solution for mounting my local maven repository in docker. I changed my solution; I am mounting it in the run
phase instead of build
phase. This is my Dockerfile
:
FROM ubuntu
MAINTAINER Zeinab Abbasimazar
ADD gwr $HOME
RUN apt-get update; \
apt-get install -y --no-install-recommends apt-utils; \
apt-get install -y wget unzip curl maven git; \
echo \
"<settings xmlns='http://maven.apache.org/SETTINGS/1.0.0\' \
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' \
xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd'> \
<localRepository>/root/.m2/repository</localRepository> \
<interactiveMode>true</interactiveMode> \
<usePluginRegistry>false</usePluginRegistry> \
<offline>false</offline> \
</settings>" \
> /usr/share/maven/conf/settings.xml; \
mkdir /root/.m2/; \
echo \
"<settings xmlns='http://maven.apache.org/SETTINGS/1.0.0\' \
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' \
xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd'> \
<localRepository>/root/.m2/repository</localRepository> \
<interactiveMode>true</interactiveMode> \
<usePluginRegistry>false</usePluginRegistry> \
<offline>false</offline> \
</settings>" \
> /root/.m2/settings.xml
WORKDIR .
CMD mvn -X clean install -pl components -P profile
首先,我使用上面的 Dockerfile
构建映像:
At first, I build the image using above Dockerfile
:
sudo docker build -t imageName:imageTag .
然后,我运行如下容器:
Then, I run a container as below:
sudo docker run -d -v /home/zeinab/.m2/:/root/.m2/ --name containerName imageName:imageTag
这篇关于将Maven存储库挂载到Docker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!