当前位置:   article > 正文

当docker中容器运行时,如何将目录和宿主机进行挂载_已运行的容器如何进行挂载

已运行的容器如何进行挂载

容器已运行,但还想挂载文件

容器已经运行起来了,突然想给容器的目录进行挂载,通常是在运行容器时加上 -v 命令进行挂载。运行起来的容器想挂载文件夹可以通过修改容器在宿主机的配置文件进行解决。

在配置文件中加入新的挂载

1.查看容器存放目录

  1. [root@linuxchen ~]# docker info | grep 'Root'
  2. Docker Root Dir: /var/lib/docker

2.进入该目录

  1. [root@linuxchen ~]# cd /var/lib/docker
  2. [root@linuxchen docker]# ls
  3. buildkit containers engine-id image network
  4. overlay2 plugins runtimes swarm tmp volumes

3.文件夹containers就包含的是各个容器的配置文件的目录

  1. [root@linuxchen docker]# cd containers/
  2. [root@linuxchen containers]# ls
  3. 01cd5154b4e4b36f3c70774cb22c9f61f6058b90383f423f7c8bb5b7d3e14d3b 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2
  4. 0552b78c6b7eec42e1ef26767054bbcc273ce1ce45a669846bc180ef1047aa1c 9d23423647373b08215f3778ebca871baffd3375b6f5b7f23c226dab15a955c3
  5. 155d3b8a66bbdf5c7321cd90d9acbbc41d61f2bdce75dc0b86f42fc002bce87b bb2567b9c2ddf9d9c7b5ad35c3a6fa20ca429e7e074c36db7c3ded50058e178c
  6. 3f1e1ca864b88e616e13359b2d919487b05103cc54a6d28280494d68fef0ba6c bcd680044d1c8602f54f679a759306827b44cf62ded902596899a931f29790cc
  7. 6441463ea608447fc484937eceec7e55db1ac65c57c9c7e4e9ab6316d7504c0f c957b6f352686df7aaa03ae14357dce9c496803e15f660ab204f3d1654c63221
  8. 6e2afdb382e6e78ebfae28cd35fca65004822906be8c6befe88d9b8b8417376a d997473f71241688421081319df0dd6bbca888fc072e3713311c269fe07f6051
  9. 900e4b6fa19cff7fb2444fec93e2f041e226e48fdae86fe7095d232936cc5824 f67880be8e92c2262290244f7ed6b889cc3361a49adb0ce26c6f91f8aeab69f9
  10. 9301eedc7ea843d39da502e4c1a37e1c88b8401e21893a8b5001f6b9ebccf76c fecaaaf35867f448b298f05a1f1da3a289533fb6be422edd49e7431f6828e09f
  11. 993633e3936dd583cd0bfb64716ac32b6b7a305b037411bb91d20ea5c38046b8 ffb4fa8fbeda9724d72c6d118090ed9b6d8564fa6fbc56bb93e47d6e7cededbf

4.查看你的容器id,容器id就是配置目录的前缀名,cd进该容器的配置文件夹(粘贴上容器id,使用tab补全即可)

  1. [root@linuxchen containers]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 9a26f7183290 nginx "/docker-entrypoint.…" 2 days ago Up 35 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
  4. [root@linuxchen containers]# cd 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2/
  5. [root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# ll
  6. total 404
  7. -rw-r----- 1 root root 376531 Nov 19 16:02 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2-json.log
  8. drwx------ 2 root root 4096 Nov 17 13:05 checkpoints
  9. -rw------- 1 root root 3519 Nov 19 15:33 config.v2.json
  10. -rw------- 1 root root 1585 Nov 19 15:33 hostconfig.json
  11. -rw-r--r-- 1 root root 13 Nov 19 15:33 hostname
  12. -rw-r--r-- 1 root root 174 Nov 19 15:33 hosts
  13. drwx--x--- 2 root root 4096 Nov 17 13:05 mounts
  14. -rw-r--r-- 1 root root 149 Nov 19 15:33 resolv.conf
  15. -rw-r--r-- 1 root root 71 Nov 19 15:33 resolv.conf.hash

5.配置config.v2.json和hostconfig.json

挂载需求:1.将宿主机的文件夹 /root/music 和容器中的文件夹 /usr/share/nginx/html/music进行挂载
                  2.将宿主机的文件夹/root/images 和容器中的文件夹 /usr/share/nginx/html/images进行挂载 

5.0 !!! 修改配置文件前一定要关掉容器并且停掉docker服务,否则配置过的内容会被冲刷掉

                 在关闭docker时,假如跟我一样出现了docker.socket并未关闭,一定要使用命令systemctl stop docker.socket 将其关闭再去关闭docker

  1. [root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# docker stop 9a26f7183290
  2. 9a26f7183290
  3. [root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# systemctl stop docker
  4. Warning: Stopping docker.service, but it can still be activated by:
  5. docker.socket
  6. [root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# systemctl stop docker.socket
  7. [root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# systemctl stop docker

注:要修改的两个配置文件都是json文件,如果使用vi或者vim打开后的文件内容不是格式化的JSON格式,可在vim中使用命令:%!python3 -m json.tool 来格式化文件内容,以方便查看和修改。

5.1 配置config.v2.json

config.v2.json文件中找到MountPoints,配置如下,只需更改为你的目录即可。要注意逗号的问题,符合json格式。

  1. "MountPoints": {
  2. "/usr/share/nginx/html/images": {
  3. "Source": "/root/images",
  4. "Destination": "/usr/share/nginx/html/images",
  5. "RW": true,
  6. "Name": "",
  7. "Driver": "",
  8. "Type": "bind",
  9. "Propagation": "rprivate",
  10. "Spec": {
  11. "Type": "bind",
  12. "Source": "/root/images",
  13. "Target": "/usr/share/nginx/html/images"
  14. },
  15. "SkipMountpointCreation": false
  16. },
  17. "/usr/share/nginx/html/music": {
  18. "Source": "/root/music",
  19. "Destination": "/usr/share/nginx/html/music",
  20. "RW": true,
  21. "Name": "",
  22. "Driver": "",
  23. "Type": "bind",
  24. "Propagation": "rprivate",
  25. "Spec": {
  26. "Type": "bind",
  27. "Source": "/root/music",
  28. "Target": "/usr/share/nginx/html/music"
  29. },
  30. "SkipMountpointCreation": false
  31. }
  32. },

5.2 配置hostconfig.json

  1. "Binds": [
  2. "/root/music:/usr/share/nginx/html/music",
  3. "/root/images:/usr/share/nginx/html/images"
  4. ],

6.重启docker 重启容器

  1. [root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# systemctl start docker
  2. [root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# docker start 9a26f7183290
  3. 9a26f7183290

7.查看文件挂载情况

    宿主机

  1. [root@linuxchen music]# ls
  2. jay2.mp3 jay.mp3 withoutyou.mp3 wozaideng-suxingjie.mp3 zood.mp3

    容器

  1. root@9a26f7183290:/usr/share/nginx/html/music# ls
  2. jay.mp3 jay2.mp3 withoutyou.mp3 wozaideng-suxingjie.mp3 zood.mp3

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/349428
推荐阅读
相关标签
  

闽ICP备14008679号