I just can’t understand what the VOLUME ["/data"] instruction does in the Dockerfile, at first I thought it was analogous to the -v option and that you could write like this: VOLUME ["С:/test:/data"] , but it turned out to be wrong . So what is the VOLUME directory for the directory and where is it stored on the host machine (windows)? And what is the best and proper way to connect the data directory, for example, the database to the container? Using the -v option?

2 answers 2

VOLUME can be very useful when the process of building the container starts some process that fills the folder with the initial set of files (initializes). For example, it could be a database folder. Further, in docker-compose.yaml or in docker run docker volume create you create a vulum and there will already be files that were placed in the folder specified in the VOLUME declaration immediately prior to this very declaration.

Those. VOLUME - means to take the contents of the folder (inside the container) and place it in the vulum, which is attached to the final image.

It is important to understand that using the Docker image of the folder as a start when launching it is more likely a crutch (hack, trick). In this case, the contents of the image from the image will not be expanded. Use commands

 Usage: docker volume COMMAND Manage volumes Options: --help Print usage Commands: create Create a volume inspect Display detailed information on one or more volumes ls List volumes prune Remove all unused volumes rm Remove one or more volumes 

    VOLUME says that when starting the container, you need to create a directory on the host, filled with data from the image, and mount it at the specified point inside the container (/ data):

    • Container starts
    • Before starting, an empty directory is created in the wilds of the docker installation
    • The contents of / data from the image are copied to the directory.
    • The directory is mounted in the container along the path / data

    It is stored somewhere inside the Docker installation (I don’t know where exactly it is on Windows). I will not be afraid of my own value judgment - this is an absolutely useless directive that bears more harm than good (because, for example, it remains to live after the death of the container, consuming unnecessary space, and detecting thousands of unnecessary volume at some point is a common thing) . If you need to remove data from a container for storage (for example, to save a database between container runs), then it is really better to use -v . The main difference between -v and VOLUME in the Dockerfile is that in -v you specify the end directory that you need to forward into the container and manage it yourself, and VOLUME does almost the same thing, only this directory is not clear where it is controlled by the docker (usually, on the contrary, it is not controlled at all), and the management of all this good is worth a fair amount of nerves.