Dockerfile COPY需要一个陈旧的文件副本?
我在我的Dockerfile中有以下命令序列; 注意由sedreplace:
RUN sed -i "s/replace this/with that/g" ./dir/file.yaml COPY ./dir/file.yaml /usr/src/app/node_modules/serve-swagger-editor/node_modules/swagger-editor/spec-files/
这运行几乎是好的,那就是将file.yaml
复制到目标位置,但是:
-
./dir/file.yaml
包含sed所做的更改, -
/usr/src/.../spec-files/file.yaml
不。
事实certificate,这个解决方法修复了COPY问题:
RUN sed -i "s/replace this/with that/g" ./dir/file.yaml RUN cp ./dir/file.yaml /usr/src/app/node_modules/serve-swagger-editor/node_modules/swagger-editor/spec-files/
有人会解释这种奇怪的行为吗?
RUN
在容器中执行命令,所以sed
被应用于其中的./dir/file.yaml
文件。 您的WORKDIR/dir/file.yaml
( WORKDIR )中可能有相同的文件,这解释了为什么第二个选项WORKDIR/dir/file.yaml
。
您的第一个版本中的COPY
将覆盖宿主生成目录中的./dir/file.yaml
的/usr/src/app/node_modules/serve-swagger-editor/node_modules/swagger-editor/spec-files/
。 这个之前没有受到sed
命令的影响,因为它在容器之外。
所以你可以做的是首先在容器中COPY
文件,然后使用你的RUN
命令来修改它。 或者在运行docker build
之前修改它。