大网数据广告活动
服务器运维问答

What is the purpose of the VOLUME instruction in a Dockerfile?

· 52CE 我爱测

In the documentation it says that the VOLUME instruction creates a mout point but I created a image using FROM alpine RUN mkdir /myvol RUN echo "hello world" > /myvol/greeting and I was able to mount /myvol or any any other path …

问题原文

原帖链接 · Shane · CC BY-SA 3.0

In the documentation it says that the VOLUME instruction creates a mout point but I created a image using

FROM alpine
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting

and I was able to mount /myvol or any any other path on the containers filesytem using docker run -v vol:/myvol myimage and was able to see the data there in /var/lib/docker/vol/_data on the host machine.

What difference would adding VOLUME myvol to the Dockerfile make?

社区高分解答

原帖链接 · Cvetk0 · CC BY-SA 4.0

I've been struggling with understanding this quite a bit and had to do some actual testing as documentation was a bit to vague to me.

With VOLUME directive in the Dockerfile you explicitly declare a volume that container created from that image exposes even if it is not explicitly mounted when container is created at container creation time - e.g. docker run -v <volume>:/data <image name>.

Instead I can have a directive in the Dockerfile

FROM alpine

RUN mkdir /data && echo "Some data" > /data/mydata
VOLUME /data

Start the container from image built with above Dockerfile:

docker run -ti --rm --name volume-test voltest

Inspect the running container

docker container inspect volume-test

...
        "Mounts": [
            {
                "Type": "volume",
                "Name": "c4d070456dfa65540bd5c75b958930837bbf4277f4a4169b791679127f29a73a",
                "Source": "/var/snap/docker/common/var-lib-docker/volumes/c4d070456dfa65540bd5c75b958930837bbf4277f4a4169b791679127f29a73a/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ]
...

As you can see there is a volume mounted to /data directory of container. This anonymous volume was automatically created during container creation because of VOLUME directive in the Dockerfile and since container was started with --rm option it will be automatically removed when container is stopped (assuming nothing else will use it at that time). You can confirm this by using docker volume ls after stopping the container.

This allows usage of such ad-hoc volumes from other containers, for example mounting them by running:

docker run --rm -ti --name alpine-vol --volumes-from volume-test alpine sh

Check the /data directoy in the newly started container, it will contain original container's data written on the volume.

I definitely see use of this when data needs to be shared between containers but does not need to persist after original container has been removed (e.g. as part of sidecar pattern). If data persistence is required you can still explicitly mount a volume into the same directory.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-12-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。