Operating system and storage
The operating system defines storage as a Volume, which is a logical abstraction of physical storage to provide an elastic partitioning mode for physical storage. In addition, the action to associate external storage with the operating system is defined as Mount.
Three mount modes in Docker
- Bind
To Bind an external HTML document to the root directory of the Nginx container, use the following command:
$ docker run -v ~/zioyi/html:/usr/share/nginx/html -p 81:80 -d --name nginx_bind nginx:latest
Copy the code
So let’s verify that
$ curl localhost:81
<html>
<title>Hi, Docker</title>
<h1>You mount me by Bind mode</h1>V
</html>
Copy the code
The disadvantage of this approach is that the mounted host directory (or file) is not protected and can be modified by any container.
- Volume
In Volume mode, we need to create a Volume using the docker Volume command. Instead, create a folder with the same name inside Docker’s /var/lib/docker/volumes/ folder to hold the data. Because this folder is within the scope of Docker’s control, Docker can control the container’s read and write permission to the Volume according to the mounting Settings.
#Create a volume
$ docker volume create nginx-volume
nginx-volume
$ docker run --mount type=volume,source=nginx-volume,destination=/usr/share/nginx/html -p 82:80 -d --name nginx_volume nginx:latest
Copy the code
The nginx-volume volume has been mounted to the nginx_volume container. You can see the following by running the inpsect command:
$ docker inspect nginx_volume {... "Mounts": [ { "Type": "volume", "Name": "nginx-volume", "Source": "/var/lib/docker/volumes/nginx-volume/_data", "Destination": "/usr/share/nginx/html", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ], ... }Copy the code
We go into the container and modify the HTML document in /usr/share/nginx/html
$ docker exec -it nginx_volume bash
root@d0df9a0eb3e5:/# echo "<html>
<title>Hi, Docker</title>
<h1> You mount me by Volume mode</h1>
</html>" > /usr/share/nginx/html/index.html
root@d0df9a0eb3e5:/# exit
exit
#Verify on the host
$ curl localhost:82
<html>
<title>Hi, Docker</title>
<h1> You mount me by Volume mode</h1>
</html>
Copy the code
When we delete the nginx_volum container, the volume is not deleted
$ docker stop nginx_volume && docker rm nginx_volume
$ docker run --mount type=volume,source=nginx-volume,destination=/usr/share/nginx/html -p 82:80 -d --name nginx_volume nginx:latest
$ curl localhost:81
<html>
<title>Hi, Docker</title>
<h1>You mount me by Volume mode</h1>
</html>
Copy the code
In addition, we can also set the container to read volumes only, but not write operations, when mounting. This method is usually used when the container reads configuration files:
$ docker run --mount type=volume,source=nginx-volume,destination=/usr/share/nginx/html,readonly -p 82:80 -d --name nginx_volume nginx:latest
$ docker exec -it nginx_volume bash
root@782b11b3cc43:/#
#When we corrected it again, The Times was wrong
root@782b11b3cc43:/# echo "hello" > /usr/share/nginx/html/index.html
bash: /usr/share/nginx/html/index.html: Read-only file system
Copy the code
- tmpfs
The TMPFS mount is temporary and remains only in the host memory. When the container is stopped, the TMPFS mount is removed and the written files are not persisted
$ docker run --mount type=tmpfs,destination=/usr/share/nginx/html -p 83:80 -d --name nginx_tmpfs nginx:latest
$ docker inspect nginx_tmpfs{... "Mounts": [ { "Type": "tmpfs", "Source": "", "Destination": "/usr/share/nginx/html", "Mode": "", "RW": true, "Propagation": "" } ], ... }Copy the code
Go to the nginx_tmpfs container and modify the data
$ docker exec -it nginx_tmpfs bash
root@68b03d8d3ec4:/# echo "<html>
<title>Hi, Docker</title>
<h1> You mount me by tmpfs mode</h1>
</html>" > /usr/share/nginx/html/index.html
root@68b03d8d3ec4: exit
exit
#validation
$ curl localhost:83
<html>
<title>Hi, Docker</title>
<h1> You mount me by tmpfs mode</h1>
</html>
Copy the code
TMPFS does not persist data generated in containers and is generally not used.
conclusion
- Compare Docker’s three mounting modes:
Bind | Volume | tmpfs | |
---|---|---|---|
Volume location | Any location can be specified | /var/lib/docker/volumes/… | Host memory |
Existing mount points are affected | Hide and replace with volume | Copy the original data to the volume | – |
Whether to support a single file | support | Not supported, only directories | – |
Access control | The value can be set to read-only, and the default value is read and write | The value can be set to read-only, and the default value is read and write | – |
portability | Weak portability, bound to host path | Portability, no need to specify the host directory | – |
Whether to support persistence | support | support | Does not support |
- About the Volume
Docker did not have Volume mode at the beginning. Docker initially thought that Volume was just a mapping between the disk storage of the external host machine and the internal container, but later found that it was not that simple: The location of storage is not limited to external hosts, the storage medium is not limited to physical disks, and the management of storage is not limited to mapping relationships. The Bind mode can not solve the problem of sharing storage between multiple hosts and the management of Bind mode. The core purpose of Volume is to improve Docker’s support capacity for different storage media, which can also reduce Docker’s workload. Storage includes not only physical storage mounted on the host, but also network storage. Docker abstracts storage startup (Sotroage Driver) to solve the problem of reading and writing network storage.