不能在docker maven容器中写入〜/ .m2

为什么在构build过程中maven3 docker镜像中的/root/.m2文件不会写入持久化?

一个简单的dockerfile:

FROM maven:3-jdk-8 RUN touch /root/.m2/testfilehere && ls -a /root/.m2 RUN ls -a /root/.m2 CMD ["bash"] 

生成以下输出。

 $ docker build -t test --no-cache . Sending build context to Docker daemon 2.048 kB Step 1 : FROM maven:3-jdk-8 ---> 42e3884987fb Step 2 : RUN touch /root/.m2/testfilehere && ls -a /root/.m2 ---> Running in 1c1dc5e9f082 . .. testfilehere ---> 3da352119c4d Removing intermediate container 1c1dc5e9f082 Step 3 : RUN ls -a /root/.m2 ---> Running in df506db8c1dd . .. ---> d07cc155b20e Removing intermediate container df506db8c1dd Step 4 : RUN stat /root/.m2/testfilehere ---> Running in af44f30aafe5 stat: cannot stat '/root/.m2/testfilehere': No such file or directory The command '/bin/sh -c stat /root/.m2/testfilehere' returned a non-zero code: 1 

当中间容器存在时,在第一个命令中创build的文件不见了。

此外,这不会发生在Ubuntu的形象,只是maven。

编辑:使用ADD hostfile /root/.m2/containerfile确实工作作为解决方法,但不是我想要的。

这是因为/root/.m2被定义为图像中的VOLUME 。 当一个容器运行一个卷时,卷存储不是UnionFS的一部分 – 所以它的数据不存储在容器的可写层中:

数据卷是绕过联盟文件系统的一个或多个容器内的特定目录。

第一个“ RUN命令在卷中创build一个文件,但它位于具有自己卷的中间容器中 。 该文件没有保存到图像层,因为它在一个卷中。

第二个RUN命令运行在具有自己的卷的新的中间容器中。 从基本映像中没有卷的内容,因此新容器中的卷是空的。

如果您想用数据预填充卷,您需要在Dockerfile中执行此操作。

Maven Docker镜像有一个入口点

 ENTRYPOINT ["/usr/local/bin/mvn-entrypoint.sh"] 

在容器启动时,入口点将文件从/ usr / share / maven / ref复制到$ {MAVEN_CONFIG}中,然后擦除文件

您可以通过以下链接查看启动时执行的脚本

https://github.com/carlossg/docker-maven/blob/33eeccbb0ce15440f5ccebcd87040c6be2bf9e91/jdk-8/mvn-entrypoint.sh

在Docker Maven中有一个这样的文档: https : //github.com/carlossg/docker-maven#packaging-a-local-repository-with-the-image

 COPY settings.xml /usr/share/maven/ref/ 

运行Docker镜像后, settings.xml将出现在/root/.m2