简体   繁体   English

Dockerfile COPY 与“docker run ... cp”

[英]Dockerfile COPY vs "docker run ... cp"

We have a build pipeline that first does a docker build using a dockerfile.我们有一个构建管道,首先使用 dockerfile 构建 docker。 That dockerfile has a number of COPY commands. dockerfile 有许多 COPY 命令。 We also have a later step that does a docker run, with 'cp' command, as follows:我们还有一个后续步骤,使用“cp”命令执行 docker 运行,如下所示:

docker run --volume /hostDirA:/containerDirB --workdir /folderB dockerRepo:somebuildnum cp -R /hostDirC/. docker 运行 --volume /hostDirA:/containerDirB --workdir /folderB dockerRepo:somebuildnum cp -R /hostDirC/。 /containerDirB /containerDirB

First, before the main point, it is my understanding that the cp command is copying from one folder to another, both folders on the container.首先,在重点之前,我的理解是 cp 命令正在从一个文件夹复制到另一个文件夹,这两个文件夹都是容器上的。 Is that a correct understanding?这是正确的理解吗?

Second, why would a cp be done in this way in the docker run when COPY is already being done in the docker build via the dockerfile?其次,当通过 dockerfile 构建的 docker 已经完成 COPY 时,为什么要在 docker 运行中以这种方式完成 cp? Are there valid reasons why we wouldn't move this cp to be inside the dockerfile?是否有正当理由不将这个 cp 移到 dockerfile 内?

Without knowing what the files are for, we can only take wild guesses.在不知道这些文件的用途的情况下,我们只能胡乱猜测。

Volumes are used for data persistence, whereas COPY is used for data that is needed in running the process, but may be destroyed.卷用于数据持久性,而 COPY 用于运行进程所需但可能被破坏的数据。

One possible valid scenario for why data is copied into the volume instead of using the COPY command is that persistent data needs to be initialized and they don't want that initialization data to add bloat to the container image.为什么将数据复制到卷中而不是使用 COPY 命令的一种可能的有效场景是需要初始化持久数据,并且他们不希望该初始化数据将膨胀添加到容器映像中。 Like I said, it's just a wild guess.就像我说的,这只是一个疯狂的猜测。

Another guess is that the Dockerfile is shared between developers and the initialization data between groups may vary, so one set of developers might copy different data into the volume.另一种猜测是 Dockerfile 在开发人员之间共享,并且组之间的初始化数据可能会有所不同,因此一组开发人员可能会将不同的数据复制到卷中。

Whatever the data is, if you shut down and remove the container, data created via COPY just vanishes with the container, but data moved into the volume via cp on the host stays in the directory that was mounted.无论数据是什么,如果您关闭并删除容器,通过COPY创建的数据将随容器一起消失,但通过主机上的cp移动到卷中的数据仍保留在挂载的目录中。 That data may have changed while the container was running from what was originally placed in it, but it doesn't reset when you remove the container and spawn a new container from the image.当容器从最初放置在其中的内容运行时,该数据可能已更改,但当您移除容器并从映像中生成新容器时,它不会重置。

You should ask the developer what all the files are for, and whether the files need to persist or whether they can just be "ephemeral".您应该询问开发人员所有文件的用途,以及这些文件是否需要持久化或者它们是否可以只是“短暂的”。 This will probably answer your questions as to why they are copied the way they are.这可能会回答您的问题,即为什么要按原样复制它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM