What is the meaning of Volume in Dockerfile? is docker run -v the same effect?

  docker, question

DockerfileOne of themVOLUMECommand, you can mount a directory to the host directory, but the name under the host directory is random unless the user uses the-vSpecifies, but if theDockerfileNo needVOLUME, only used at runtime-vSpecifying can also achieve results, so there is a problem.VOLUMEInDockerfileHow much use is there inHostUnder the created several users generally can’t find the file exactly how much use value, in most cases the user is to use manually-vReassign the mount directory, thenVOLUMEOrders are very chicken ribs.

VOLUMEIs there any more important use I don’t understand?

I also have some doubts about this, so I looked for some information and expressed my views.

Found the official userguide:https://docs.docker.com/engine/userguide …

One thing I learned from this is that VOLUME is not just a declaration. it reloads the specified path. I also found this through the inspect container.

This is the Dockerfile specifies the VOLUME without specifying-V. To view the Mounts information of the container:

"Mounts": [
 {
 "Name": "b3e2dcacd3f9f40b43ccd5773d45ca74f0f49b02d3da17749cb378ff9f59bb67",
 "Source": "/var/lib/docker/volumes/b3e2dcacd3f9f40b43ccd5773d45ca74f0f49b02d3da17749cb378ff9f59bb67/_data",
 "Destination": "/etc",
 "Driver": "local",
 "Mode": "",
 "RW": true
 }
 ],

This is based on the previous one, and -v is specified to view the Mounts information of the container:

"Mounts": [
 {
 "Source": "/etc",
 "Destination": "/etc",
 "Mode": "",
 "RW": true
 }
 ],

Then you go/var/lib/docker/volumes/b3e2dcacd3f9f40b43ccd5773d45ca74f0f49b02d3da17749cb378ff9f59bb67/_dataTake a look at the catalogue and it will be clear.

You can understand VOLUME as copying the folder of the specified volume from the mirror to the local/var/lib/doc/volumes/xxxxxxxxx/folder, and then mount the local folder into the container.

In essence, it is equivalent to a local folder mount.

Continue to add, because VOLUME is actually mounted by creating a new folder locally, then there are actually three cases of folders inside the container:
1. No VOLUME or -v is specified. This is a normal folder.
2. The specified VOLUME does not specify-v. This folder can be shared between different containers, but cannot be modified locally.
3. A -v folder is specified. This folder can be shared among different containers and can be modified locally.

Let’s list a situation that needs to be shared between different containers and does not need to be modified locally.

First of all, let’s understand how to get dynamic data in the container:
1, locally provided, mounted to the container
2. Provide it remotely and download it remotely.
3. Generation Provision: Generated 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 pulling code directly from the git server, do not need to mount local folders.
For generating 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 Apache server, then you need to specify the build directory as VOLUME.