给正在运行的Docker容器动态绑定卷组

github的地址:https://github.com/pushiqiang/utils/tree/master/docker

git clone  https://github.com/pushiqiang/utils
[root@iZ1la3d1xbmukrZ ~]# ll
total 4
drwxr-xr-x 13 root root 4096 Mar  3 18:03 utils
[root@iZ1la3d1xbmukrZ ~]# cd utils/
[root@iZ1la3d1xbmukrZ utils]# ll
total 44
drwxr-xr-x 2 root root 4096 Mar  3 18:03 asyncio
drwxr-xr-x 2 root root 4096 Mar  3 18:03 cassandra
drwxr-xr-x 3 root root 4096 Mar  3 18:03 django_celery
drwxr-xr-x 2 root root 4096 Mar  3 18:03 docker
drwxr-xr-x 2 root root 4096 Mar  3 18:03 es_ik
drwxr-xr-x 2 root root 4096 Mar  3 18:03 logger
drwxr-xr-x 3 root root 4096 Mar  3 18:03 nginx_thumbnail
drwxr-xr-x 2 root root 4096 Mar  3 18:03 pagination
-rw-r--r-- 1 root root 3084 Mar  3 18:03 README.md
drwxr-xr-x 4 root root 4096 Mar  3 18:03 rest_framework_swagger
drwxr-xr-x 2 root root 4096 Mar  3 18:03 zerorpc

 然后要给脚本赋予权限

chmod +x dynamic_mount_docker_volume
 docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter

 注:如果不能下载,参考:https://github.com/jpetazzo/nsenter ,nsenter是一个允许根据容器名称进入容器的小工具。

[root@iZ1la3d1xbmukrZ docker]# ./dynamic_mount_docker_volume 955138b6c3ed /tmp/test /src
physics /tmp/test is not exist!
[root@iZ1la3d1xbmukrZ docker]# mkdir -p /tmp/test
[root@iZ1la3d1xbmukrZ docker]# ./dynamic_mount_docker_volume 955138b6c3ed /tmp/test /src
Error: No such object: 955138b6c3ed
[root@iZ1la3d1xbmukrZ docker]# docker ps -a
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS                        PORTS                                              NAMES
27e9dd648ef2        sonatype/nexus3                       "sh -c ${SONATYPE_DI…"   3 hours ago         Exited (137) 53 minutes ago                                                      nexus82
d0fe6f850e15        sonatype/nexus:oss                    "/bin/sh -c 'java   …"   4 hours ago         Up 4 hours                    0.0.0.0:8082->8081/tcp                             nexus-oss
2e9ae246f58f        sonatype/nexus:pro-2.14.16            "/bin/sh -c 'java   …"   4 hours ago         Exited (1) 4 hours ago                                                           nexus
7e83dc0d600a        wojiushixiaobai/jms_guacamole:1.5.6   "./entrypoint.sh"        8 days ago          Up 5 days                     127.0.0.1:8081->8080/tcp                           jms_guacamole
92d1dd497f2c        wojiushixiaobai/jms_koko:1.5.6        "./entrypoint.sh"        8 days ago          Up 5 days                     0.0.0.0:2222->2222/tcp, 127.0.0.1:5000->5000/tcp   jms_koko
[root@iZ1la3d1xbmukrZ docker]# ./dynamic_mount_docker_volume d0fe6f850e15 /tmp/test /src
dymainc mount physics /tmp/test on d0fe6f850e15 /src is success!

 

进入到容器中去查看

[root@iZ1la3d1xbmukrZ docker]# docker exec -it d0fe6f850e15 /bin/bash
[nexus@d0fe6f850e15 nexus]$ cd src
bash: cd: src: No such file or directory
[nexus@d0fe6f850e15 nexus]$ cd /src
[nexus@d0fe6f850e15 src]$ ls -l
total 0

 

进行测试,在宿主机中创建文件

[root@iZ1la3d1xbmukrZ ~]# cd /tmp/test
[root@iZ1la3d1xbmukrZ test]# ll
total 0
[root@iZ1la3d1xbmukrZ test]# touch 111.txt
[root@iZ1la3d1xbmukrZ test]#

在容器中查看

[nexus@d0fe6f850e15 src]$ ls -l
total 0
-rw-r--r-- 1 root root 0 Mar  3 12:11 111.txt
[nexus@d0fe6f850e15 src]$
posted @ 2020-03-03 20:26  天宇轩-王  阅读(665)  评论(0编辑  收藏  举报