閱讀本文前,希望你已經(jīng)對(duì)Volumes和Bind mounts有了初步的了解,具體可以參考以下文章:
- Docker數(shù)據(jù)持久化之Volumes
- Docker數(shù)據(jù)持久化之Bind mounts
tmpfs mounts
Volumes和Bind mounts模式使我們能夠在宿主機(jī)和容器間共享文件從而我們能夠?qū)?shù)據(jù)持久化到宿主機(jī)上,以避免寫入容器存儲(chǔ)層帶來的容器停止后數(shù)據(jù)的丟失的問題。
如果你使用linux運(yùn)行Docker,那么避免寫入數(shù)據(jù)到容器存儲(chǔ)層還有一個(gè)方案:tmpfs mounts。
tmpfs mounts,顧名思義,是一種非持久化的數(shù)據(jù)存儲(chǔ)。它僅僅將數(shù)據(jù)保存在宿主機(jī)的內(nèi)存中,一旦容器停止運(yùn)行,tmpfs mounts會(huì)被移除,從而造成數(shù)據(jù)丟失。
tmpfs mounts的使用
我們可以在運(yùn)行容器時(shí)通過指定--tmpfs
參數(shù)或--mount
參數(shù)來使用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
參數(shù)無法指定任何其他的可選項(xiàng),并且不能用于Swarm Service。
使用docker container inspect tmptest
命令,然后查看Mounts
部分可以看到:
tmpfs mounts 可選選項(xiàng)

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