tmpfs 挂载
Volumes 和 bind mounts 允许你在主机和容器之间共享文件, 这样即使容器停止后,数据也能持久保存。
如果您在Linux上运行Docker,您还有第三种选择:tmpfs挂载。 当您使用tmpfs挂载创建容器时,容器可以在容器的可写层之外创建文件。
与卷和绑定挂载不同,tmpfs 挂载是临时的,并且仅保存在主机内存中。当容器停止时,tmpfs 挂载会被移除,写入其中的文件不会被持久化。
tmpfs挂载最适合用于不希望数据持久化在主机或容器内的情况。这可能是出于安全原因,或者是为了在应用程序需要写入大量非持久状态数据时保护容器的性能。
重要
Docker中的tmpfs挂载直接映射到Linux内核中的tmpfs。因此,临时数据可能会被写入交换文件,从而持久化到文件系统中。
在现有数据上挂载
如果你在容器中的一个目录中创建了一个tmpfs挂载,而该目录中已经存在文件或目录,那么这些预先存在的文件将被挂载所遮蔽。这类似于你在Linux主机的/mnt目录中保存文件,然后将USB驱动器挂载到/mnt目录。在USB驱动器卸载之前,/mnt目录的内容将被USB驱动器的内容所遮蔽。
使用容器时,没有直接的方法可以移除挂载以再次显示被隐藏的文件。您的最佳选择是在没有挂载的情况下重新创建容器。
tmpfs挂载的限制
- 与卷和绑定挂载不同,您不能在容器之间共享tmpfs挂载。
- 此功能仅在您在Linux上运行Docker时可用。
- 在tmpfs上设置权限可能会导致它们在容器重启后重置。在某些情况下,设置uid/gid可以作为解决方法。
语法
要使用docker run命令挂载一个tmpfs,你可以使用--mount或--tmpfs标志。
$ docker run --mount type=tmpfs,dst=<mount-path>
$ docker run --tmpfs <mount-path>
一般来说,--mount 是首选。主要区别在于 --mount 标志更加明确。另一方面,--tmpfs 更加简洁,并且提供了更多的灵活性,因为它允许你设置更多的挂载选项。
--tmpfs 标志不能与 swarm 服务一起使用。您必须使用 --mount。
--tmpfs 的选项
--tmpfs 标志由两个字段组成,用冒号字符 (:) 分隔。
$ docker run --tmpfs <mount-path>[:opts]
第一个字段是要挂载到tmpfs的容器路径。第二个字段是可选的,允许您设置挂载选项。--tmpfs的有效挂载选项包括:
| Option | Description |
|---|---|
ro | Creates a read-only tmpfs mount. |
rw | Creates a read-write tmpfs mount (default behavior). |
nosuid | Prevents setuid and setgid bits from being honored during execution. |
suid | Allows setuid and setgid bits to be honored during execution (default behavior). |
nodev | Device files can be created but are not functional (access results in an error). |
dev | Device files can be created and are fully functional. |
exec | Allows the execution of executable binaries in the mounted file system. |
noexec | Does not allow the execution of executable binaries in the mounted file system. |
sync | All I/O to the file system is done synchronously. |
async | All I/O to the file system is done asynchronously (default behavior). |
dirsync | Directory updates within the file system are done synchronously. |
atime | Updates file access time each time the file is accessed. |
noatime | Does not update file access times when the file is accessed. |
diratime | Updates directory access times each time the directory is accessed. |
nodiratime | Does not update directory access times when the directory is accessed. |
size | Specifies the size of the tmpfs mount, for example, size=64m. |
mode | Specifies the file mode (permissions) for the tmpfs mount (for example, mode=1777). |
uid | Specifies the user ID for the owner of the tmpfs mount (for example, uid=1000). |
gid | Specifies the group ID for the owner of the tmpfs mount (for example, gid=1000). |
nr_inodes | Specifies the maximum number of inodes for the tmpfs mount (for example, nr_inodes=400k). |
nr_blocks | Specifies the maximum number of blocks for the tmpfs mount (for example, nr_blocks=1024). |
$ docker run --tmpfs /data:noexec,size=1024,mode=1777
并非所有在Linux挂载命令中可用的tmpfs挂载功能都支持--tmpfs标志。如果您需要高级的tmpfs选项或功能,您可能需要使用特权容器或在Docker之外配置挂载。
注意
使用
--privileged运行容器会授予提升的权限,并可能使主机系统面临安全风险。仅在绝对必要且受信任的环境中使用此选项。
$ docker run --privileged -it debian sh
/# mount -t tmpfs -o <options> tmpfs /data
--mount 的选项
--mount 标志由多个键值对组成,用逗号分隔,每个键值对由一个 元组组成。键的顺序不重要。
$ docker run --mount type=tmpfs,dst=<mount-path>[,<key>=<value>...]
--mount type=tmpfs 的有效选项包括:
| Option | Description |
|---|---|
destination, dst, target | Size of the tmpfs mount in bytes. If unset, the default maximum size of a tmpfs volume is 50% of the host's total RAM. |
tmpfs-size | Size of the tmpfs mount in bytes. If unset, the default maximum size of a tmpfs volume is 50% of the host's total RAM. |
tmpfs-mode | File mode of the tmpfs in octal. For instance, 700 or 0770. Defaults to 1777 or world-writable. |
$ docker run --mount type=tmpfs,dst=/app,tmpfs-size=21474836480,tmpfs-mode=1770
在容器中使用tmpfs挂载
要在容器中使用tmpfs挂载,请使用--tmpfs标志,或者使用--mount标志并指定type=tmpfs和destination选项。tmpfs挂载没有source。以下示例在Nginx容器中的/app处创建了一个tmpfs挂载。第一个示例使用了--mount标志,第二个示例使用了--tmpfs标志。
$ docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app \
nginx:latest
通过查看docker inspect输出中的Mounts部分,验证挂载是否为tmpfs挂载:
$ docker inspect tmptest --format '{{ json .Mounts }}'
[{"Type":"tmpfs","Source":"","Destination":"/app","Mode":"","RW":true,"Propagation":""}]
$ docker run -d \
-it \
--name tmptest \
--tmpfs /app \
nginx:latest
通过查看docker inspect输出中的Mounts部分,验证挂载是否为tmpfs挂载:
$ docker inspect tmptest --format '{{ json .Mounts }}'
{"/app":""}
停止并移除容器:
$ docker stop tmptest
$ docker rm tmptest
下一步
- 了解 volumes
- 了解 bind mounts
- 了解 存储驱动程序