今天小编给大家分享一下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.json
和
hostconfig.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命令将其进行格式化,方便查看。