一、找到 Docker 默认安装位置
[root@host-192-168-0-1 ~]# docker info
Client:Version: 26.1.0Context: defaultDebug Mode: falseServer:Containers: 31Running: 31Paused: 0Stopped: 0Images: 128Server Version: 26.1.0Storage Driver: overlay2Backing Filesystem: xfsSupports d_type: trueUsing metacopy: falseNative Overlay Diff: trueuserxattr: falseLogging Driver: json-fileCgroup Driver: cgroupfsCgroup Version: 1Plugins:Volume: localNetwork: bridge host ipvlan macvlan null overlayLog: awslogs fluentd gcplogs gelf journald json-file local splunk syslogSwarm: inactiveRuntimes: io.containerd.runc.v2 runcDefault Runtime: runcInit Binary: docker-initcontainerd version: 926c9586fe4a6236699318391cd44976a98e31f1runc version: v1.1.12-0-g51d5e94init version: de40ad0Security Options:seccompProfile: builtinKernel Version: 4.19.90-52.22.v2207.ky10.x86_64Operating System: Kylin Linux Advanced Server V10 (Lance)OSType: linuxArchitecture: x86_64CPUs: 8Total Memory: 30.1GiBName: host-192-173-32-120ID: 5e427e0b-c772-4045-90a2-59c5cba6a03bDocker Root Dir: /var/lib/dockerDebug Mode: falseExperimental: falseInsecure Registries:127.0.0.0/8Registry Mirrors:https://dockerproxy.com/https://registry.docker-cn.com/Live Restore Enabled: falseProduct License: Community Engine
其中 Docker Root Dir: /var/lib/docker
就是安装目录
二、创建或修改 daemon.json 配置文件
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://dockerproxy.com","https://registry.docker-cn.com"
],
"data-root": "/opt/data/docker" # 填写你想迁移过去的文件夹路径
}
注意:json文件中参数名前面的空格一定要用制表符,不要用4个空格,否则后面docker服务启动会失败!
三、将默认安装路径下的文件复制到将要迁移的文件夹下
systemctl stop docker# 这里要用 /opt/data 而不是 /opt/data/docker 是因为复制过去会自动生成一个 docker 文件夹
cp -ar /var/lib/docker /opt/data
注意:拷贝数据时,要拷贝权限关系,直接 cp -r
可能导致部分容器无法启动,显示权限不足等
四、重启服务
systemctl daemon-reload && systemctl restart docker
五、验证结果,删除原目录
[root@host-192-168-0-1 ~]# docker info | grep RootDocker Root Dir: /opt/data/docker
rm -rf /var/lib/docker