一、找到 Docker 默认安装位置
[root@host-192-168-0-1 ~]# docker info
Client:
Version: 26.1.0
Context: default
Debug Mode: false
Server:
Containers: 31
Running: 31
Paused: 0
Stopped: 0
Images: 128
Server Version: 26.1.0
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 926c9586fe4a6236699318391cd44976a98e31f1
runc version: v1.1.12-0-g51d5e94
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 4.19.90-52.22.v2207.ky10.x86_64
Operating System: Kylin Linux Advanced Server V10 (Lance)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 30.1GiB
Name: host-192-173-32-120
ID: 5e427e0b-c772-4045-90a2-59c5cba6a03b
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://dockerproxy.com/
https://registry.docker-cn.com/
Live Restore Enabled: false
Product 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 Root
Docker Root Dir: /opt/data/docker
rm -rf /var/lib/docker