Dockerfile 中的 VOLUME 声明和 -v 作为 docker run 参数之间的区别 [英] Difference between VOLUME declaration in Dockerfile and -v as docker run parameter

查看:20
本文介绍了Dockerfile 中的 VOLUME 声明和 -v 作为 docker run 参数之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我理解以下之间的区别:

Dockerfile 中的VOLUME 命令(镜像构建层)

发出 docker run-v/xyz/bla 命令时的

-v 参数(容器构建层).

-v 参数对我来说很清楚,它只是将一个目录从主机公开到容器,反之亦然,但是 Dockerfile<中的 VOLUME 如何/code> 行为不同?

解决方案

-v参数和VOLUME关键字差不多.您可以使用 -v 来获得与 VOLUME 相同的行为.

docker run -v/data

音量/数据

而且 -v 还有更多用途,其中之一就是映射到卷的位置:

docker run -v data:/data # 命名卷docker run -v/var/data:/data # 主机挂载的卷,这就是你所说的 -v 使用,但是你可以看到还有更多的用途,

所以问题是:Dockerfile中的VOLUME有什么用?

容器文件系统是由层组成的,所以写在那里,比普通文件系统慢且有限(因为层数固定).

您在 Dockerfile 中声明 VOLUME 以表示容器将写入应用程序数据的位置.例如一个数据库容器,无论你在 docker run 中放入什么,它的数据都会进入一个卷中.

如果你为 JBoss 创建了一个 docker 容器,并且你想通过 libaio 使用快速文件系统访问,你需要将数据目录声明为 VOLUME 否则 JBoss 将崩溃启动.

总而言之,VOLUME 声明一个卷,无论你在 docker run 中做什么.事实上,在 docker run 中,您无法撤消 Dockerfile 中的 VOLUME 声明.

问候

Could some one help me to understand the difference between:

VOLUME command in Dockerfile (image building layer)

and

-v parameter when issuing docker run-v/xyz/bla command (container building layer).

-v parameter is for me clear, it simply exposes a directory from the host to the container and vice versa, but how does VOLUME in Dockerfile behave differently?

解决方案

The -v parameter and VOLUME keyword are almost the same. You can use -v to have the same behavior as VOLUME.

docker run -v /data

Same as

VOLUME /data

But also -v have more uses, one of them is where map to the volume:

docker run -v data:/data # Named volumes
docker run -v /var/data:/data # Host mounted volumes, this is what you refer to -v use, but as you can see there are more uses,

So the question is: what is the use of VOLUME in a Dockerfile?

The container filesystem is made of layers so writing there, is slower and limited (because the fixed number of layers) than the plain filesystem.

You declare VOLUME in your Dockerfile to denote where your container will write application data. For example a database container, its data will go in a volume regardless what you put in your docker run.

If you create a docker container for JBoss and you want to use fast filesystem access with libaio yo need to declare the data directory as a VOLUME or JBoss will crash on startup.

In summary VOLUME declares a volume regardless what you do in docker run. In fact in docker run you cannot undo a VOLUME declaration made in Dockerfile.

Regards

这篇关于Dockerfile 中的 VOLUME 声明和 -v 作为 docker run 参数之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆