docker-修改容器掛載目錄的3種方法小結(jié)
方式一:修改配置文件(需停止docker服務(wù))
1、停止docker服務(wù)
systemctl stop docker.service(關(guān)鍵,修改之前必須停止docker服務(wù))
2、vim /var/lib/docker/containers/container-ID/config.v2.json
修改配置文件中的目錄位置,然后保存退出
"MountPoints":{"/home":{"Source":"/docker","Destination":"/home","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"http://docker/","Target":"/home"}}}
3、啟動(dòng)docker服務(wù)
systemctl start docker.service
4、啟動(dòng)docker容器
docker start <container-name/ID>
方式二:提交現(xiàn)有容器為新鏡像,然后重新運(yùn)行它
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5a3422adeead ubuntu:14.04 "/bin/bash" About a minute ago Exited (0) About a minute ago agitated_newton $ docker commit 5a3422adeead newimagename $ docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash
然后停止舊容器,并使用這個(gè)新容器,如果由于某種原因需要新容器使用舊名稱(chēng),請(qǐng)?jiān)趧h除舊容器后使用docker rename。
方式三:export容器為鏡像,然后import為新鏡像
$docker container export -o ./myimage.docker 容器ID $docker import ./myimage.docker newimagename $docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash
然后停止舊容器,并使用這個(gè)新容器,如果由于某種原因需要新容器使用舊名稱(chēng),請(qǐng)?jiān)趧h除舊容器后使用docker rename。
補(bǔ)充知識(shí):Docker如何重啟后數(shù)據(jù)不丟失,教你掛載數(shù)據(jù)卷Volume
大家在使用Docker部署web應(yīng)用或者mysql數(shù)據(jù)庫(kù)時(shí),會(huì)發(fā)現(xiàn)當(dāng)容器重啟后,容器運(yùn)行過(guò)程中產(chǎn)生的日志或者數(shù)據(jù)庫(kù)數(shù)據(jù)都會(huì)被清空,那么我們?nèi)绾伪4孢@些數(shù)據(jù)呢?
這就需要了解docker如何掛載宿主機(jī)磁盤(pán)目錄,用來(lái)永久存儲(chǔ)數(shù)據(jù)。
1. 創(chuàng)建容器時(shí)執(zhí)行Docker Volume
使用 docker run 命令,可以運(yùn)行一個(gè) Docker容器,使用鏡像ubuntu/nginx,掛載本地目錄/tmp/source到容器目錄/tmp/destination
docker run -itd --volume /tmp/source:/tmp/destination --name test ubuntu/nginx bash
基于ubuntu/nginx鏡像創(chuàng)建了一個(gè)Docker容器。
指定容器的名稱(chēng)為test,由 ––name 選項(xiàng)指定。
Docker Volume 由 ––volume (可以簡(jiǎn)寫(xiě)為-v)選項(xiàng)指定,主機(jī)的 /tmp/source 目錄與容器中的 /tmp/destination 目錄一一對(duì)應(yīng)。
2. 查看Docker Volume
使用 docker inspect 命令,可以查看 Docker容器 的詳細(xì)信息:
docker inspect --format='{{json .Mounts}}'test | python -m json.tool[{“Destination”: “/tmp/destination”,“Mode”: “”,“Propagation”: “”,“RW”: true,“Source”: “/tmp/source”,“Type”: “bind”}]
使用 ––format 選項(xiàng),可以選擇性查看需要的容器信息。 .Mount 為容器的 Docker Volume 信息。
python -m json.tool 可以將輸出的json字符串格式化顯示。
Source 表示主機(jī)上的目錄,即 /tmp/source 。
Destination 為容器中的目錄,即 /tmp/destination。
3. 本機(jī)文件可以同步到容器
在本機(jī)/tmp/source目錄中新建hello.txt文件
touch /tmp/source/hello.txtls /tmp/source/hello.txt
hello.txt文件在容器/tmp/destination/目錄中可見(jiàn)
使用 docker exec 命令,可以在容器中執(zhí)行命令。
docker exectest ls /tmp/destination/hello.txt
所以在宿主機(jī)對(duì)目錄 /tmp/source/ 的修改,可以同步到容器目錄 /tmp/destination/ 中。
4. 容器文件可以同步到宿主機(jī)
在容器/tmp/destination目錄中新建world.txt文件
docker exec test touch /tmp/destination/world.txtdocker exec test ls /tmp/destination/hello.txtworld.txt
world.txt文件在宿主機(jī)/tmp/source/目錄中可見(jiàn)
ls /tmp/source/hello.txt world.txt
以上這篇docker-修改容器掛載目錄的3種方法小結(jié)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
docker啟動(dòng)nginx及常見(jiàn)nginx配置方式
這篇文章主要介紹了docker啟動(dòng)nginx及常見(jiàn)nginx配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Gogs+Jenkins+Docker 自動(dòng)化部署.NetCore的方法步驟
這篇文章主要介紹了Gogs+Jenkins+Docker 自動(dòng)化部署.NetCore,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06使用Golang玩轉(zhuǎn)Docker API的實(shí)踐
這篇文章主要介紹了使用Golang玩轉(zhuǎn)Docker API的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04docker中的環(huán)境變量使用與常見(jiàn)問(wèn)題解決方案
這篇文章主要給大家介紹了關(guān)于docker中環(huán)境變量使用與常見(jiàn)問(wèn)題解決方案的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用docker具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03docker部署管理工具portainer-ce的實(shí)現(xiàn)
Portainer是一個(gè)可視化的容器鏡像的圖形管理工具,利用Portainer可以輕松構(gòu)建,管理和維護(hù)Docker環(huán)境,本文主要介紹了docker部署管理工具portainer-ce的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08