简体   繁体   English

docker 中的体积差异运行和 dockerfile 中的复制

[英]Difference in Volumes in docker run and COPY in dockerfile

If I do something like如果我做类似的事情

docker run -v /opt/datadir:/var/lib/mysql image

I am mapping some location inside the container to a location in the host.我将容器内的某个位置映射到主机中的某个位置。

How is this different to the command COPY used when writing a Dockerfile?这与编写 Dockerfile 时使用的命令COPY有何不同?

The major difference is seen in case we edit any of the file present inside that location.如果我们编辑该位置内的任何文件,则可以看到主要区别。

Suppose the directory /opt/datadir contains a file temp.txt假设目录/opt/datadir包含一个文件temp.txt

In case of bind mount, if you try to edit the file temp.txt from the host machine, the changes will be reflected inside the container and vice-versa.在绑定挂载的情况下,如果您尝试从主机编辑文件temp.txt ,更改将反映在容器内,反之亦然。

When we create COPY command in Dockerfile, it copies the content to the filesystem of the container.当我们在 Dockerfile 中创建COPY命令时,它会将内容复制到容器的文件系统中。 Hence any changes done inside the container are DOES NOT affect the files present on the host machine.因此,在容器内完成的任何更改都不会影响主机上存在的文件。 In this case, if you want changes done on the host machine to be reflected inside the container, then you need to build a docker image and run a new container using the updated image.在这种情况下,如果您希望在主机上所做的更改反映在容器内,那么您需要构建一个 docker 映像并使用更新后的映像运行一个新容器。

When to use what?什么时候用什么?

For scenarios where the resource needs frequent updates, use bind mounts.对于资源需要频繁更新的场景,请使用绑定挂载。

Eg: We want to provide our web server a configuration file that might change frequently.例如:我们想为我们的 web 服务器提供一个可能经常更改的配置文件。

In case the resource is independent of host filesystem, use COPY command inside dockerfile.如果资源独立于主机文件系统,请在 dockerfile 中使用COPY命令。

Eg: .tar , .zip , .war files or any file that requires no or very few updates inside the container.例如: .tar.zip.war文件或任何在容器内不需要或很少更新的文件。

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

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