閱讀本文前,希望你已經對Volumes和Bind mounts有了初步的了解,具體可以參考以下文章:
- Docker數據持久化之Volumes
- Docker數據持久化之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 mounts 可選選項

一個例子:
docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app,tmpfs-mode=1770 \
nginx:latest
tmpfs mounts使用場景
請參考這篇文章:Docker數據存儲總結
參考文章
https://docs.docker.com/storage/tmpfs/
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接