在构建期间,为什么在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

中间容器存在时,在第一个命令创建的文件将消失。

另外,这不会在ubuntu镜像中发生,只是行家。

编辑:使用ADD hostfile /root/.m2/containerfile确实可以解决,但不是我想要的。

最佳答案

这是因为/root/.m2在图像中被定义为VOLUME。当容器与卷一起运行时,卷存储is not part of the UnionFS-因此其数据未存储在容器的可写层中:



第一个RUN命令在卷中创建一个文件,但是该文件位于具有自己的卷的中间容器中。该文件在一个卷中,因此未保存到图像层。

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

如果您想用数据预填充卷,则需要在Dockerfile中进行操作,如您所见。

关于bash - 无法写入Docker Maven容器中的〜/.m2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39637899/

10-13 08:54
查看更多