docker如何對已經啟動的容器添加目錄映射(掛載目錄)

1. 背景

在自己安裝sharding-proxy時,使用docker run -v的方式啟動時,隻掛載瞭/opt/shardingsphere-proxy/conf/opt/shardingsphere-proxy/ext-lib這兩個目錄,忘記掛載/opt/shardingsphere-proxy/logs日志目錄,導致每次查看都得要進入容器內部去查看,很是麻煩,於是考慮如何在不刪除容器重新啟動部署的情況下進行新目錄的映射與掛載

2. 開始新增掛載目錄

1.查看容器存放目錄

[root@iZ2zedqr9yeos47fg4uor5Z proxy-a]# docker info | grep 'Root'
 Docker Root Dir: /var/lib/docker
[root@iZ2zedqr9yeos47fg4uor5Z proxy-a]#

2.查看要新增掛載的容器id

[root@iZ2zedqr9yeos47fg4uor5Z proxy-a]# docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED       STATUS          PORTS                               NAMES
6f5f3c195304   apache/shardingsphere-proxy:5.1.1   "/bin/sh -c '${LOCAL…"   4 days ago    Up 9 minutes    0.0.0.0:3321->3307/tcp              server-proxy-a

3.進入要新增文件夾掛載的目錄

[root@iZ2zedqr9yeos47fg4uor5Z proxy-a]# docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED       STATUS          PORTS                               NAMES
6f5f3c195304   apache/shardingsphere-proxy:5.1.1   "/bin/sh -c '${LOCAL…"   4 days ago    Up 11 minutes   0.0.0.0:3321->3307/tcp              server-proxy-a
[root@iZ2zedqr9yeos47fg4uor5Z proxy-a]# cd /var/lib/docker/containers/
[root@iZ2zedqr9yeos47fg4uor5Z containers]# ll
total 64
drwx------ 4 root root 4096 Nov  7 14:20 1d0c21204385f60fa8859b0ff199569988218e449155634470cfb14ff2ceebf9
drwx------ 4 root root 4096 Nov  7 14:20 21a5f8b6e68ee09f8f80381c536b38abec1b8dcb81c69f1c50caeb95e8ebb35f
drwx------ 4 root root 4096 Nov  7 14:20 28242c9fd16bd610880d42a773e078221a0d34b1c779935fc0c8609a1b63666a
drwx------ 4 root root 4096 Nov  7 14:22 2bc290c8e158229fdc5291a3ea53c5721f5becd39c8cb47cb2f826f6349ac778
drwx------ 4 root root 4096 Nov  7 14:20 2fc0e1b1031abe6b1871332b971ccdc40185b7441e94ba3c266f51d2d4728412
drwx------ 4 root root 4096 Nov  7 14:20 3124c345f129a3c542423c68061c6ded5b41a649b25991125c8e81da7b010e7e
drwx------ 4 root root 4096 Nov  7 14:20 4a1e699a3711b92918f711d7ab021a5ac62bab1b8353ed321337c6f94ac50533
drwx------ 4 root root 4096 Nov  7 14:20 4cdc6456250a922e60f3394b26064be0541f04a0c34825c6487e73ed7979705a
drwx------ 4 root root 4096 Nov  7 14:20 53d710c37c08b9c2ab0c84fea2059d067bbcbc38f22aa2e654e1c62758b1df44
drwx------ 4 root root 4096 Nov  7 14:41 6f5f3c1953044043672723cdf4f3725c500e77a54e07cd0a4759371c04c0cfa1
drwx------ 4 root root 4096 Nov  7 14:20 73cc941fd394d42b8cbdc8fd7a50fc098b5d56c0323e5b9a786703d21a102d33
drwx------ 4 root root 4096 Nov  7 14:35 8dc6124199b0ef6afbc9d2b542c5a46c8767e7daafd52bfedd53a83491e17db0
drwx------ 4 root root 4096 Nov  7 14:20 9c554d94d5eb6c2c6015994b52ae3f3c31d182dde189d86427840b4a1c5d6477
drwx------ 4 root root 4096 Nov  7 14:20 a073fcd724623a98168564707dccfb9562d94d89c0882ad2fd92798a2137be0d
drwx------ 4 root root 4096 Nov  7 14:21 b1191679d1ecea73a74f44ee74b33281fbcccc942307630db02f2cb9e86869d0
drwx------ 4 root root 4096 Nov  7 14:20 eb30519b4804f9ca3c708260e885f99d693332af4a162a956952b601332fed97
[root@iZ2zedqr9yeos47fg4uor5Z containers]# cd 6f5f3c1953044043672723cdf4f3725c500e77a54e07cd0a4759371c04c0cfa1/
[root@iZ2zedqr9yeos47fg4uor5Z 6f5f3c1953044043672723cdf4f3725c500e77a54e07cd0a4759371c04c0cfa1]# ll
total 488
-rw-r----- 1 root root 461990 Nov  7 14:41 6f5f3c1953044043672723cdf4f3725c500e77a54e07cd0a4759371c04c0cfa1-json.log
drwx------ 2 root root   4096 Nov  2 22:29 checkpoints
-rw------- 1 root root   4041 Nov  7 14:41 config.v2.json
-rw-r--r-- 1 root root   1705 Nov  7 14:41 hostconfig.json
-rw-r--r-- 1 root root     13 Nov  7 14:41 hostname
-rw-r--r-- 1 root root    174 Nov  7 14:41 hosts
drwx------ 2 root root   4096 Nov  2 22:29 mounts
-rw-r--r-- 1 root root    149 Nov  7 14:41 resolv.conf
-rw-r--r-- 1 root root     71 Nov  7 14:41 resolv.conf.hash
[root@iZ2zedqr9yeos47fg4uor5Z 6f5f3c1953044043672723cdf4f3725c500e77a54e07cd0a4759371c04c0cfa1]#

我們將要修改的文件就是config.v2.jsonhostconfig.json

4.關閉容器、關閉docker

systemctl stop docker

5.修改config.v2.json

說明:想要將容器中的/opt/shardingsphere-proxy/logs目錄映射到宿主機的/atguigu/server/proxy-a/logs目錄下。
config.v2.json文件中找到MountPoints,添加如下/opt/shardingsphere-proxy/logs的映射即可(這裡如果前面還有內容的話,註意逗號)

{
    ......
	
	"MountPoints": {
		......,
		"/opt/shardingsphere-proxy/logs": {
			"Source": "/atguigu/server/proxy-a/logs",
			"Destination": "/opt/shardingsphere-proxy/logs",
			"RW": true,
			"Name": "",
			"Driver": "",
			"Type": "bind",
			"Propagation": "rprivate",
			"Spec": {
				"Type": "bind",
				"Source": "/atguigu/server/proxy-a/logs",
				"Target": "/opt/shardingsphere-proxy/logs"
			},
			"SkipMountpointCreation": false
		}
	},
	
	......
	
}

6.修改hostconfig.json

說明:Binds中添加內容"/atguigu/server/proxy-a/logs:/opt/shardingsphere-proxy/logs"

{
	"Binds": [
		......,
		"/atguigu/server/proxy-a/logs:/opt/shardingsphere-proxy/logs"
	],
	......
}

7.重啟docker與容器

systemctl start docker
docker start 容器id

8.進行目錄驗證

目錄成功映射 出來,查看日志就不用每次很麻煩的進入容器內部查看瞭

[root@iZ2zedqr9yeos47fg4uor5Z logs]# pwd
/atguigu/server/proxy-a/logs
[root@iZ2zedqr9yeos47fg4uor5Z logs]# ll
total 36
-rw-r--r-- 1 root root 36421 Nov  7 14:41 stdout.log
-rw-r--r-- 1 root root     0 Nov  7 14:41 xa_tx1.log
-rw-r--r-- 1 root root     0 Nov  7 14:41 xa_tx.lck
[root@iZ2zedqr9yeos47fg4uor5Z logs]#

3. 註意

  • config.v2.json和hostconfig.json文件修改之前,需要關閉docker與容器!如果先修改瞭這兩個文件,再關閉docker,這兩個文件中的內容會被重置掉,導致新增的目錄映射失效 (自己在這裡折騰瞭很久才發現的!)
  • config.v2.json和hostconfig.json文件vi命令打開後,並非格式化後的json,這裡可以通過:%!python3 -m json.tool命令將其進行格式化,方便查看

總結

到此這篇關於docker如何對已經啟動的容器添加目錄映射(掛載目錄)的文章就介紹到這瞭,更多相關docker容器添加目錄映射內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: