问题描述

一般来说,都是创建容器时进行目录挂载,对于已经创建好的容器如果忘了挂载目录但是又想挂载目录

解决办法

  1. 提交这个容器为新的镜像,然后创建这个新镜像的容器同时进行目录挂载(此方式较简单)
  2. 修改这个容器的配置文件

实现过程

提交新镜像再创建容器

查看下面两篇博客即可实现修改已创建容器的挂载目录

  1. Docker提交运行时容器为镜像 ,将要挂载目录的已创建容器提交为本地新镜像
  2. Docker创建容器时目录挂载 ,创建容器时使用新镜像的镜像id顺便挂载目录

修改配置文件

停止Docker

1
systemctl stop docker

配置文件路径

/var/lib/docker/containers/[容器id]

修改配置文件hostconfig.json,在”Binds”节点中增加挂载目录的关系,前面的/home/hData/是宿主机目录,后面的/cData是容器目录,如果是多个用英文逗号隔开

单个目录例子:

1
"Binds":["/home/hData/:/cData"]

多个目录例子:

1
"Binds":["/home/hData:/cData","/home/hData2:/cData2"]

修改配置文件config.v2.json,在”MountPoints” 节点中增加挂载目录

单个目录例子:(第一段代码是原始的,第二段代码是格式化过的,格式化后更好修改)

1
"MountPoints":{"/cData":{"Source":"/home/hData","Destination":"/cData","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/home/hData/","Target":"/cData"},"SkipMountpointCreation":false}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"MountPoints": {
"/cData": {
"Source": "/home/hData",
"Destination": "/cData",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/home/hData/",
"Target": "/cData"
},
"SkipMountpointCreation": false
}
}

多个目录例子:(第一段代码是原始的,第二段代码是格式化过的,格式化后更好修改)

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

启动Docker

1
systemctl start docker

启动刚刚修改的容器

1
docker start 容器id