..."."/> ...".">
Home > Article > Operation and Maintenance > Can docker mount files?
Docker can mount files; file mounting refers to mounting the specified file of the host to the file in the docker container. The file location is managed by the user. You can use the run command to mount the host file. To the file in the container, the syntax is "docker run --name
...".
The operating environment of this tutorial: linux7.3 system, docker version 19.03, Dell G3 computer.
File mounting is to mount the specified file of the host to the file in the Docker container. The file location is managed by the user
Example
docker run --name <容器名> -p 80:80 -v <宿主机文件绝对路径:容器内文件绝对路径> -d <仓库名:版本标签> # docker run --name nginx-contain -p 80:80 -v /docker_conf/nginx/html/index.html:/usr/share/nginx/html/index.html -d nginx # 宿主机文件挂载到容器内文件
ps:
1. The host file will overwrite the file in the container
Extended knowledge
Docker mounting is to use this The machine files or folders overwrite the files or folders inside the container.
Function:
I use cp command to realize data transfer, this method is more troublesome;
We mount through the container directory, which can achieve two-way communication between the host and the container directory. Automatic data synchronization can easily realize code upload, configuration modification, log synchronization and other needs.
Note: When docker mounts the configuration file, be careful not to write the wrong content of the configuration file to be mounted, otherwise the container cannot be started after the container is created.
Folder mounting Loading the folder on the
host will definitely overwrite the folder in the container:
Allows non-existing folders or existing empty folders to be mounted Loading the container, the corresponding folders in the container will be cleared
Mounting non-empty folders into the container will overwrite the original folders in the container
File mounting
The biggest difference between file mounting and folder mounting is:
docker prohibits mounting files that do not exist on the host to files that already exist in the container
File mounting will not have any impact on other files in the same folder
Mounting existing files into the container will overwrite the corresponding files in the container. If the file does not exist, it will be created
Summary
Files on the host will definitely overwrite files in the container
host container mount result
Non-existent files Already existing files Prohibited behavior
Exists Files that do not exist/existing files, new/overwritten (if the directory does not exist, the directory will be created)
Application scenarios
From the above analysis, it can be seen that the folder is mounted with The file is overwritten in units of 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 mount point in the container. At this time, they can be directly seen in the folder on the corresponding host.
File mounting will only overwrite a single file and will not affect other files in the same folder in the container. It is often used to mount configuration files to dynamically modify the default configuration at runtime.
Recommended learning: "docker video tutorial"
The above is the detailed content of Can docker mount files?. For more information, please follow other related articles on the PHP Chinese website!