向运行中的docker容器添加挂载磁盘
需求
容器跑了一段时间,空间不足,需要扩容。
传统方法需要commit成新的image然后重新run添加-v进行挂载
容器使用了很长时间,数据较多打包不方便,希望热添加。
实现
以下命令在root权限下执行
sudo -i
# 找到当前容器my_container的id
docker container inspect my_container | grep "Id"
# 转换配置文件
cd /var/lib/docker/containers/xxxxxxxx
jq . config.v2.json > tmp.json
# 停止docker服务否则改动失效
systemctl stop docker.service
# 根据格式添加MountPoint字段
jq -c . tmp.json > config.v2.json
# 重启docker生效
systemctl start docker.service
添加字段可以参考这个json片段,无需提前建立挂载点文件夹
"/home/ubuntu/data3": {
"Source": "/data3/waymo",
"Destination": "/home/ubuntu/data3",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/data3/waymo/",
"Target": "/home/ubuntu/data3"
},
"SkipMountpointCreation": false
}
参考
How to Add a Volume to an Existing Docker Container
Add a Volume to an Existing Docker Container | Baeldung on Linux