停止docker容器數據是否會丟失

停止docker容器,數據不會丟失。docker容器停止并退出,會處于終止(exited)狀態,其中的數據不會丟失,可以通過“docker ps -a”查看;只有刪除掉容器,數據才會隨著容器的刪除被清空。

停止docker容器數據是否會丟失

本教程操作環境:linux5.9.8系統、docker-1.13.1版、Dell G3電腦。

停止docker容器,數據不會丟失。

docker容器停止并退出,會處于終止(exited)狀態,等同于虛擬機關機,所以是不會有數據丟失的。

此時可以通過 docker ps -a 查看,還可以通過docker start 來啟動,只有刪除容器才會清除數據。

只有當docker rm之后,刪除容器才會清除數據。

創建容器,然后把容器刪除,數據隨著容器的刪除也被刪除

如何刪除容器不刪除數據,在創建容器的時候dockerrun-vhost_dir:containere_dir這樣可以解決你的問題!

擴展知識:當容器重啟后,容器運行過程中產生的日志或者數據庫數據都會被清空。

解決方法:

docker可以通過掛載宿主機磁盤目錄,來永久存儲數據。

1. 創建容器時執行Docker Volume

使用 docker run 命令,可以運行一個 Docker容器,使用鏡像ubuntu/nginx,掛載本地目錄/tmp/source到容器目錄/tmp/destination

docker?run?-itd?--volume?/tmp/source:/tmp/destination?--name?test?ubuntu/nginx?bash

基于ubuntu/nginx鏡像創建了一個Docker容器。

指定容器的名稱為test,由 ––name 選項指定。

Docker Volume 由 ––volume (可以簡寫為-v)選項指定,主機的 /tmp/source 目錄與容器中的 /tmp/destination 目錄一一對應。

2. 查看Docker Volume

使用 docker inspect 命令,可以查看 Docker容器 的詳細信息:

docker?inspect?--format=’{{json?.Mounts}}'test?|?python?-m?json.tool[{“Destination”:?“/tmp/destination”,“Mode”:?“”,“Propagation”:?“”,“RW”:?true,“Source”:?“/tmp/source”,“Type”:?“bind”}]

使用 ––format 選項,可以選擇性查看需要的容器信息。 .Mount 為容器的 Docker Volume 信息。

python -m json.tool 可以將輸出的json字符串格式化顯示。

Source 表示主機上的目錄,即 /tmp/source 。

Destination 為容器中的目錄,即 /tmp/destination。

3. 本機文件可以同步到容器

在本機/tmp/source目錄中新建hello.txt文件

touch?/tmp/source/hello.txtls?/tmp/source/hello.txt

hello.txt文件在容器/tmp/destination/目錄中可見

使用 docker exec 命令,可以在容器中執行命令。

docker?exectest?ls?/tmp/destination/hello.txt

所以在宿主機對目錄 /tmp/source/ 的修改,可以同步到容器目錄 /tmp/destination/ 中。

4. 容器文件可以同步到宿主機

在容器/tmp/destination目錄中新建world.txt文件

docker?exec?test?touch?/tmp/destination/world.txtdocker?exec?test?ls?/tmp/destination/hello.txtworld.txt

world.txt文件在宿主機/tmp/source/目錄中可見

ls?/tmp/source/hello.txt?world.txt

推薦學習:《docker視頻教程

以上就是停止

? 版權聲明
THE END
喜歡就支持一下吧
點贊6 分享