Zei*_*zar 6 java maven docker dockerfile docker-build
我正在尝试构建一个 Java 应用程序并使用 docker 制作一个包。这个构建需要一个 maven 存储库,我不想包含在图像中,因为它非常大。我想尝试使用卷并将我的本地 Maven 存储库挂载到映像中的 Maven 存储库。我使用apt-get install -y maven是为了让 maven 可用,但我.m2在图像中找不到目录$HOME。
我用ls -la $HOME,ls -la和ls -la /root找到maven home,但那里没有.m2目录。
编辑 1:
我有这些行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
Run Code Online (Sandbox Code Playgroud)
它将本地存储库配置放在图像的 maven 配置文件中,将我的本地 Maven 存储库挂载到图像中的目录,最后执行构建。正如我在 Maven 构建日志中看到的,它使用的是我期望的本地存储库路径:
[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
Run Code Online (Sandbox Code Playgroud)
但仍然无法检测依赖项。
我终于找到了在 docker 中安装我的本地 maven 存储库的解决方案。我改变了我的解决方案;我将它安装在run阶段而不是build阶段。这是我的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
Run Code Online (Sandbox Code Playgroud)
首先,我使用上面的构建图像Dockerfile:
sudo docker build -t imageName:imageTag .
Run Code Online (Sandbox Code Playgroud)
然后,我运行一个容器,如下所示:
sudo docker run -d -v /home/zeinab/.m2/:/root/.m2/ --name containerName imageName:imageTag
Run Code Online (Sandbox Code Playgroud)
您找不到该~/.m2目录,因为它仅在需要时创建,即当您将库存储在本地存储库中或添加配置文件时。
您可以~/.m2自己创建目录并settings.xml在内部创建自己的目录。您可以在那里定义本地存储库的位置:
<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>/path/to/local/repo/</localRepository>
...
</settings>
Run Code Online (Sandbox Code Playgroud)
阅读文档以获取更多详细信息。
| 归档时间: |
|
| 查看次数: |
15401 次 |
| 最近记录: |