跳转至

Docker 数据迁移

概要: 以RHEL 8.5平台为例,迁移Docker数据到新路径位置

创建时间: 2022.09.19 23:34:01

更新时间: 2023.08.01 00:02:02

确定Docker数据位置

Bash
docker info | grep "Docker Root Dir"
如果没有改动过,默认的位置在 /var/lib/docker

停掉Docker服务

Bash
systemctl stop docker

使用rsync迁移数据

rsync is a fast and versatile command-line utility for synchronizing files and directories between two locations over a remote shell, or from/to a remote Rsync daemon. It provides fast incremental file transfer by transferring only the differences between the source and the destination. Rsync can be used for mirroring data, incremental backups, copying files between systems, and as a replacement for scp , sftp , and cp commands.

确保 rsync 工具已经安装

Bash
sudo dnf install rsync -y
在目标位置创建新的Docker数据文件夹
Bash
mkdir lib-docker
迁移数据
Bash
rsync -avz /var/lib/docker/ ./lib-docker
然后时漫长的等待,直到迁移完毕
image.png

更新Docker配置

新建或编辑Docker配置文件

Bash
vim /etc/docker/daemon.json
在JSON文件中指定Docker数据的位置
JSON
1
2
3
{
  "data-root": "/mnt/sdb/lib-docker"
}

重启Docker服务

重启Docker服务

Bash
systemctl daemon-reload
systemctl restart docker

确认迁移成功

首先确认新的位置已被正确加载

Bash
docker info | grep "Docker Root Dir"
image.png
接下来检查Docker各个容器是否正常工作,最后移除旧的Docker数据
Bash
rm -rf /var/lib/docker/

参考