今天小编给大家分享一下docker怎么对已经启动的容器添加目录映射的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
在自己安装sharding-proxy时,使用docker run -v
的方式启动时,只挂载了/opt/shardingsphere-proxy/conf
和/opt/shardingsphere-proxy/ext-lib
这两个目录,忘记挂载/opt/shardingsphere-proxy/logs
日志目录,导致每次查看都得要进入容器内部去查看,很是麻烦,于是考虑如何在不删除容器重新启动部署的情况下进行新目录的映射与挂载
[root@iZ2zedqr9yeos47fg4uor5Z proxy-a]# docker info | grep 'Root' Docker Root Dir: /var/lib/docker [root@iZ2zedqr9yeos47fg4uor5Z proxy-a]#
[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
[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
systemctl stop docker
说明:想要将容器中的/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 } }, ...... }
说明:Binds
中添加内容"/atguigu/server/proxy-a/logs:/opt/shardingsphere-proxy/logs"
{ "Binds": [ ......, "/atguigu/server/proxy-a/logs:/opt/shardingsphere-proxy/logs" ], ...... }
systemctl start docker
docker start 容器id
目录成功映射 出来,查看日志就不用每次很麻烦的进入容器内部查看了
[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]#
config.v2.json和hostconfig.json文件修改之前,需要关闭docker与容器!如果先修改了这两个文件,再关闭docker,这两个文件中的内容会被重置掉,导致新增的目录映射失效 (自己在这里折腾了很久才发现的!)
config.v2.json和hostconfig.json文件vi命令打开后,并非格式化后的json,这里可以通过:%!python3 -m json.tool命令将其进行格式化,方便查看。
以上就是“docker怎么对已经启动的容器添加目录映射”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。