This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9379|Reply: 0

The file or folder does not exist when the Docker volume is mounted

[Copy link]
Posted on 11/8/2021 9:20:35 AM | | |
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 existsFolder B that doesn't existCreate folder B in contanier first, and then copy all the files in folder A into B
Non-empty folder A existsNon-empty folder B existsFirst, 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 existsNon-empty folder B existsThe 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 existNon-empty folder B existsCreate folder A on host, and the contents of container Chinese folder B are emptied
Folders A/B/C that do not existNon-empty folder B existsCreate 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 emptyThe folder does not exist/exists but is empty/exists and is not emptycontainer Chinese piece is covered (emptied)
The folder exists and is not emptyThe folder does not exist/exists but is empty/exists and is not emptycontainer 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.jsDocuments that already exist congfigB.jsError, 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.jsPresent file congfigB.jscontainer Chinese item configB.js remain the same, but the file contents are overwritten by the congfigA.js contents
Present file configA.jsFiles that do not exist congfigB.jsA 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 existDocuments that already existProhibited acts
Documents that existFiles that don't exist / files that already existAdd/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)




Previous:Elasticsearch uses elasticdump to back up and migrate data
Next:Docker POI error exception solution
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com