Dockerfile 中有一个 VOLUME 命令,可以把挂载一个目录到主机目录,但是在主机目录下的名字是随机的,除非用户用 -v 指定,但是如果在 Dockerfile 中不用 VOLUME,只在运行时用 -v 指定也能达到效果,那么就有一个问题,VOLUME 在 Dockerfile 中到底有多少用,在 Host 下创建了几个用户一般找不到的文件到底有多少使用价值,大部分情况下用户都是要手动使用 -v 重新指定挂载目录,那 VOLUME 命令就显得很鸡肋。
VOLUME 是否有什么比较重要的用处我没有理解呢?
I also have some questions about this, so I found some information and expressed my opinion.
Found the official userguide: https://docs.docker.com/engine/userguide...
I learned something from this: VOLUME is not just a statement, it will reload the specified path. I also discovered this through the inspect container.
This is VOLUME specified in the Dockerfile, and -v is not specified. View the Mounts information of the container:
This is based on the previous one, specifying -v to view the Mounts information of the container:
Then you go to the
/var/lib/docker/volumes/b3e2dcacd3f9f40b43ccd5773d45ca74f0f49b02d3da17749cb378ff9f59bb67/_data
directory and take a look, and it will be roughly clear.You can understand VOLUME as copying the folder of the specified volume from the image to the local
/var/lib/docker/volumes/xxxxxxxxx/文件夹
, and then mounting the local folder into the container.Essentially, it is just equivalent to mounting a local folder.
Continue to add, because VOLUME actually creates a new folder locally and mounts it, so there are actually three situations for folders inside the container:
1. Neither VOLUME nor -v is specified. This is a normal folder. .
2. VOLUME is specified without -v. This kind of folder can be shared between different containers, but cannot be modified locally.
3. Folders with -v specified can be shared between different containers and can be modified locally.
Then list a situation that needs to be shared between different containers and does not need to be modified locally.
First of all, we first understand how to obtain dynamic data in the container:
1. Provide locally, mount to the container
2. Provide remotely, download from remote
3. Generate and provide, generate inside the container
The latter two commands do not need to be modified locally, but the dynamic data they generate may need to be shared.
Download commands, such as git clone, pull the code directly from the git server without mounting the local folder.
To generate commands, such as jekyll (static website generator), you may mount a code folder, and then the static web page files generated in the build directory need to be provided to the Apache server, then you need to specify the build directory as VOLUME.
VOLUME
Commands are mainly useful in development environments:When editing code, edit it directly on the host machine, and then run it synchronously in docker. There is no need to start and shut down repeatedly. This can maximize the performance of the development environment.
Specify for
VOLUME
我更喜欢使用docker-compose
:docker-compose can bundle and run multiple services. The example above is to run web services and db services separately, which is very suitable for building complex environments.