Dockerfile COPY 获取文件的陈旧副本?

标签 docker dockerfile

我的 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/

相关文章:

elasticsearch - 在Docker中运行Elasticsearch

docker - 在 docker 上运行的进程是否应该对外部操作系统可见?

mysql - 为什么dockerfile无法使用 “CMD/etc/init.d/mysql start ”启动数据库?

bash - 如何在 Dockerfile 中运行读取命令?

node.js - docker webpack编译文件运行时丢失

docker - 构建Dockerfile时linux源命令不起作用

docker - docker-compose没有在共享卷中获取文件系统更改

java - 约 10 秒后与 Airflow docker 容器断开连接

docker-compose 来自 shell 的 args

docker - 如何在 docker 中重启 tcserver 实例?