Background
docker volume allows us to dynamically mount some files (such as configuration files) to overwrite the original files in the image when starting a docker container, but what happens if we mount a folder or file that does not yet exist on the host to the container? LZ encountered such a problem in his work, so he practiced it himself and recorded the experimental results as follows:
folder mount
docker's behavior on folder mounting is uniform, as follows:
- If the folder does not exist, the folder is created first (or recursively if it is a multi-level folder)
- Overwrite the folder contents in container with the folder contents on host
The details are as follows:
The folder on host exists and is not empty
host | container | mount result | | Non-empty folder A exists | Folder B that doesn't exist | Create folder B in contanier first, and then copy all the files in folder A into B | | Non-empty folder A exists | Non-empty folder B exists | First, empty the original contents of container Chinese folder B, and then copy A Chinese copy to B |
Regardless of whether folder B in the container exists, A will completely overwrite B's contents
The folder on host exists, but is empty
host | container | mount result | | Empty folder A that exists | Non-empty folder B exists | The contents of container Chinese folder B are emptied |
The contents of the corresponding folder in the container are emptied
The host folder does not exist
host | container | mount result | | Folder A that doesn't exist | Non-empty folder B exists | Create folder A on host, and the contents of container Chinese folder B are emptied | | Folders A/B/C that do not exist | Non-empty folder B exists | Create folders A/B/C on host, and the contents of container Chinese folder B are emptied |
The contents of the corresponding folder in the container are emptied
summary
The host folder will definitely overwrite the container Chinese folder:
host | container | mount result | | Folder does not exist/folder exists but is empty | The folder does not exist/exists but is empty/exists and is not empty | container Chinese piece is covered (emptied) | | The folder exists and is not empty | The folder does not exist/exists but is empty/exists and is not empty | container Chinese folder contents are overwritten (the original content is emptied, and the contents of the folder on the host are overwritten) |
File mount
The biggest difference between file mounting and folder mounting is:
- docker prohibits mounting files that don't exist on the host to files that already exist in the container
- File mounting does not have any effect on other files under the same folder
In addition, its overwrite behavior is consistent with folder mounting, i.e.:
- Overwrite the contents of the file in the container with the contents of the file on host
The details are as follows:
host
host | container | mount result | | Files that do not exist configA.js | Documents that already exist congfigB.js | Error, Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type. It generates two empty directories configA.js and configB.js on host at the same time, but the container cannot start |
host
host | container | mount result | | Present file configA.js | Present file congfigB.js | container Chinese item configB.js remain the same, but the file contents are overwritten by the congfigA.js contents | | Present file configA.js | Files that do not exist congfigB.js | A new file configB.js in the container with the contents of the configA.js file remains unchanged configB.js all other files under the file |
summary
The file on the host will always overwrite the container Chinese folder
host | container | mount result | | Files that do not exist | Documents that already exist | Prohibited acts | | Documents that exist | Files that don't exist / files that already exist | Add/overwrite (creates a directory if it doesn't exist) |
conclusion
folder mount
- If you allow non-existent folders or empty folders to be mounted in the container, the corresponding folders in the container will be emptied
- Attaching a non-empty folder to a container will overwrite the original folder in the container
File mount
- It is forbidden to mount non-existent files on files that already exist in the container
- Existing files will be overwritten when they are attached to the container, and if the files do not exist, they will be created
Application scenarios
- From the above analysis, it can be seen that folder mounting is used to overwrite files in the entire folder, so it can be used when a large number of files need to be mounted into the container, in addition, if an empty folder or a non-existent folder is mounted, it is generally used in reverse: that is, after the container is started, some files (such as logs) may be generated under the folder of the mounting point in the container, and at this time, it can be directly seen in the folder on the corresponding host.
- File mounting is often used to mount configuration files to dynamically modify the default configuration at runtime because it only overwrites a single file without affecting other files in the same folder in the container.
Transferred from:The hyperlink login is visible.
(End) |