0


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

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

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

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

1.查看容器存放目录

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

2.进入该目录

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

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

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

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

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

[root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# docker stop 9a26f7183290
9a26f7183290
[root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket
[root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# systemctl stop docker.socket
[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格式。

    "MountPoints": {
        "/usr/share/nginx/html/images": {
            "Source": "/root/images",
            "Destination": "/usr/share/nginx/html/images",
            "RW": true,
            "Name": "",
            "Driver": "",
            "Type": "bind",
            "Propagation": "rprivate",
            "Spec": {
                "Type": "bind",
                "Source": "/root/images",
                "Target": "/usr/share/nginx/html/images"
            },
            "SkipMountpointCreation": false
        },
        "/usr/share/nginx/html/music": {
            "Source": "/root/music",
            "Destination": "/usr/share/nginx/html/music",
            "RW": true,
            "Name": "",
            "Driver": "",
            "Type": "bind",
            "Propagation": "rprivate",
            "Spec": {
                "Type": "bind",
                "Source": "/root/music",
                "Target": "/usr/share/nginx/html/music"
            },
            "SkipMountpointCreation": false
        }
    },

5.2 配置hostconfig.json

 "Binds": [
        "/root/music:/usr/share/nginx/html/music",
        "/root/images:/usr/share/nginx/html/images"
    ],

6.重启docker 重启容器

[root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# systemctl start docker
[root@linuxchen 9a26f71832905af40316ff2a17d709f22c658de0276677e6bd980f350e3d30a2]# docker start 9a26f7183290
9a26f7183290

7.查看文件挂载情况

宿主机
[root@linuxchen music]# ls
jay2.mp3  jay.mp3  withoutyou.mp3  wozaideng-suxingjie.mp3  zood.mp3
容器
root@9a26f7183290:/usr/share/nginx/html/music# ls
jay.mp3  jay2.mp3  withoutyou.mp3  wozaideng-suxingjie.mp3  zood.mp3
标签: docker 容器 云原生

本文转载自: https://blog.csdn.net/weixin_73497586/article/details/134491646
版权归原作者 东坡的雪堂 所有, 如有侵权,请联系我们删除。

“当docker中容器运行时,如何将目录和宿主机进行挂载”的评论:

还没有评论