阅读本文前,希望你已经对Volumes和Bind mounts有了初步的了解,具体可以参考以下文章:
tmpfs mounts
Volumes和Bind mounts模式使我们能够在宿主机和容器间共享文件从而我们能够将数据持久化到宿主机上,以避免写入容器存储层带来的容器停止后数据的丢失的问题。
如果你使用linux运行Docker,那么避免写入数据到容器存储层还有一个方案:tmpfs mounts。
tmpfs mounts,顾名思义,是一种非持久化的数据存储。它仅仅将数据保存在宿主机的内存中,一旦容器停止运行,tmpfs mounts会被移除,从而造成数据丢失。
tmpfs mounts的使用
我们可以在运行容器时通过指定--tmpfs
参数或--mount
参数来使用tmpfs mounts:
$ docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app \
nginx:latest
$ docker run -d \
-it \
--name tmptest \
--tmpfs /app \
nginx:latest
使用
--tmpfs
参数无法指定任何其他的可选项,并且不能用于Swarm Service。
使用docker container inspect tmptest
命令,然后查看Mounts
部分可以看到:
"Tmpfs": {
"/app": ""
},
tmpfs mounts 可选选项
Option | Description |
---|---|
tmpfs-size | 挂载的tmpfs的字节数,默认不受限制 |
tmpfs-mode | tmpfs的文件模式,例如700或1700.默认值为1777,这意味着任何用户都有写入权限 |
一个例子:
docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app,tmpfs-mode=1770 \
nginx:latest
tmpfs mounts使用场景
请参考这篇文章:Docker数据存储总结
网友评论