TMPFS 安装座
卷和绑定挂载允许您共享文件 在主机和容器之间,这样即使在 容器已停止。
如果您在 Linux 上运行 Docker,则还有第三个选项:tmpfs mounts。 当您使用 tmpfs 挂载创建容器时,容器可以创建 文件。
与 volumes 和 bind 挂载相反,tmpfs 挂载是临时的,并且只是 保留在主机内存中。当容器停止时,tmpfs 挂载为 removed 和写入其中的文件将不会被保留。
tmpfs 挂载最适合用于不希望数据持久化的情况 在主机上或容器中。这可能是为了安全 原因或为了保护容器的性能,当您的应用程序 需要写入大量非持久化 state 数据。
重要
Docker 中的 tmpfs 挂载直接映射到 Linux 内核中的 tmpfs。因此, 临时数据可以写入交换文件,从而持久化到 文件系统。
挂载到现有数据上
如果您将 tmpfs 挂载到容器中的文件或
目录存在,预先存在的文件会被挂载遮挡。这是
类似于如果您要将文件保存到/mnt
在 Linux 主机上,然后
将 USB 驱动器挂载到/mnt
.的内容/mnt
会被
USB 驱动器的内容,直到 USB 驱动器被卸载。
对于容器,没有直接的方法可以移除挂载来显示 再次被遮挡的文件。最好的选择是在 坐骑。
tmpfs 挂载的限制
- 与卷和绑定挂载不同,您不能在容器之间共享 tmpfs 挂载。
- 仅当在 Linux 上运行 Docker 时,此功能才可用。
- 在 tmpfs 上设置权限可能会导致它们在容器重启后重置。在某些情况下,设置 uid/gid 可以作为一种解决方法。
语法
要使用docker run
命令中,您可以使用--mount
或--tmpfs
旗。
$ docker run --mount type=tmpfs,dst=<mount-path>
$ docker run --tmpfs <mount-path>
通常--mount
是首选。主要区别在于--mount
flag 更明确,并支持所有可用选项。
这--tmpfs
flag 不能与 Swarm 服务一起使用。您必须使用--mount
.
--mount 的选项
这--mount
flag 由多个键值对组成,以逗号分隔
每个 API 都由<key>=<value>
元。键的顺序不是
重要。
$ docker run --mount type=tmpfs,dst=<mount-path>[,<key>=<value>...]
的有效选项--mount type=tmpfs
包括:
选择 | 描述 |
---|---|
destination ,dst ,target | tmpfs 挂载的大小(以字节为单位)。如果未设置,则 tmpfs 卷的默认最大大小为主机总 RAM 的 50%。 |
tmpfs-size | tmpfs 挂载的大小(以字节为单位)。如果未设置,则 tmpfs 卷的默认最大大小为主机总 RAM 的 50%。 |
tmpfs-mode | tmpfs 的文件模式(以八进制为单位)。例如700 或0770 .默认为1777 或 world-writable。 |
$ docker run --mount type=tmpfs,dst=/app,tmpfs-size=21474836480,tmpfs-mode=1770
--tmpfs 的选项
这--tmpfs
flag 不允许您指定任何选项。
在容器中使用 tmpfs 挂载
要使用tmpfs
挂载到容器中,请使用--tmpfs
标志,或使用--mount
flag 替换为type=tmpfs
和destination
选项。没有source
为tmpfs
坐骑。以下示例创建一个tmpfs
挂载于/app
在 Nginx 容器中。第一个示例使用--mount
flag 和
second 使用--tmpfs
旗。
$ docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app \
nginx:latest
$ docker run -d \
-it \
--name tmptest \
--tmpfs /app \
nginx:latest
验证挂载是否为tmpfs
mountMounts
部分
这docker inspect
输出:
$ docker inspect tmptest --format '{{ json .Mounts }}'
[{"Type":"tmpfs","Source":"","Destination":"/app","Mode":"","RW":true,"Propagation":""}]
停止并移除容器:
$ docker stop tmptest
$ docker rm tmptest