首頁 運維 Docker docker重啟後資料是否會遺失

docker重啟後資料是否會遺失

Mar 26, 2020 pm 02:11 PM
docker

docker重啟後資料是否會遺失

大家在使用Docker部署web應用程式或mysql資料庫時,會發現當容器重新啟動後,容器運行過程中產生的日誌或資料庫資料都會被清空。

解決方法:

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

更多相關教學課程,請追蹤PHP中文網docker教學欄位。

以上是docker重啟後資料是否會遺失的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

docker怎麼退出容器 docker怎麼退出容器 Apr 15, 2025 pm 12:15 PM

退出 Docker 容器的四種方法:容器終端中使用 Ctrl D 快捷鍵容器終端中輸入 exit 命令宿主機終端中使用 docker stop <container_name> 命令宿主機終端中使用 docker kill <container_name> 命令(強制退出)

docker內的文件怎麼拷貝到外面 docker內的文件怎麼拷貝到外面 Apr 15, 2025 pm 12:12 PM

Docker 中將文件拷貝到外部主機的方法:使用 docker cp 命令:執行 docker cp [選項] <容器路徑> <主機路徑>。使用數據卷:在主機上創建目錄,在創建容器時使用 -v 參數掛載該目錄到容器內,實現文件雙向同步。

docker容器名稱怎麼查 docker容器名稱怎麼查 Apr 15, 2025 pm 12:21 PM

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

docker怎麼啟動mysql docker怎麼啟動mysql Apr 15, 2025 pm 12:09 PM

在 Docker 中啟動 MySQL 的過程包含以下步驟:拉取 MySQL 鏡像創建並啟動容器,設置根用戶密碼並映射端口驗證連接創建數據庫和用戶授予對數據庫的所有權限

docker怎麼重啟 docker怎麼重啟 Apr 15, 2025 pm 12:06 PM

重啟 Docker 容器的方法:獲取容器 ID(docker ps);停止容器(docker stop <container_id>);啟動容器(docker start <container_id>);驗證重啟成功(docker ps)。其他方法:Docker Compose(docker-compose restart)或 Docker API(參考 Docker 文檔)。

docker怎麼更新鏡像 docker怎麼更新鏡像 Apr 15, 2025 pm 12:03 PM

更新 Docker 鏡像的步驟如下:拉取最新鏡像標記新鏡像為特定標籤刪除舊鏡像(可選)重新啟動容器(如果需要)

docker怎麼創建容器 docker怎麼創建容器 Apr 15, 2025 pm 12:18 PM

在 Docker 中創建容器: 1. 拉取鏡像: docker pull [鏡像名] 2. 創建容器: docker run [選項] [鏡像名] [命令] 3. 啟動容器: docker start [容器名]

怎麼看docker進程 怎麼看docker進程 Apr 15, 2025 am 11:48 AM

Docker 進程查看方法:1. Docker CLI 命令:docker ps;2. Systemd CLI 命令:systemctl status docker;3. Docker Compose CLI 命令:docker-compose ps;4. Process Explorer(Windows);5. /proc 目錄(Linux)。

See all articles