我的 Dockerfile 中有以下命令序列;请注意 就地 由 sed 进行的替换:
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
没有。
事实证明,此解决方法解决了 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 ) 中可能有一个/相同的文件,这解释了为什么第二个选项有效。
第一个版本中的 COPY
会覆盖 /usr/src/app/node_modules/serve-swagger-editor/node_modules/swagger-editor/spec-files/
文件与主机构建目录中的 ./dir/file.yaml
。这个之前没有受到 sed
命令的影响,因为它位于容器之外。
因此,您可以做的是先将文件COPY
到容器内,然后使用RUN
命令修改它。或者在运行docker build
之前修改它。
关于Dockerfile COPY 获取文件的陈旧副本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36589996/