Linux修改docker默认存储目录(/var/lib)
1.使用ssh脚本方式
bash
#!/bin/bash
# 设置新目录路径
NEW_DATA_ROOT="/new/path/to/docker"
# 停止Docker服务
echo "Stopping Docker service..."
sudo systemctl stop docker
# 备份现有数据
echo "Backing up existing Docker data..."
sudo cp -rp /var/lib/docker /var/lib/docker-old
# 创建新的存储目录
echo "Creating new storage directory..."
sudo mkdir -p "$NEW_DATA_ROOT"
# 编辑Docker守护进程配置文件
echo "Configuring new data root in daemon.json..."
sudo tee /etc/docker/daemon.json <<EOF
{
"data-root": "$NEW_DATA_ROOT"
}
EOF
# 重新启动Docker服务
echo "Restarting Docker service..."
sudo systemctl start docker
# 验证更改
echo "Verifying changes..."
docker info |grep Dir
echo "Docker data root has been successfully changed to $NEW_DATA_ROOT."
2.命令方式
1.停止docker
sudo systemctl stop docker
2.迁移docker安装目录进行备份
mv docker/ /datadrive/software/
3.创建文件夹:
mkdir -p /datadrive/software/docker-config/docker
4.新增daemon.json文件
bash
sudo tee /etc/docker/daemon.json <<EOF
{
"data-root": "/datadrive/software/docker-config/docker"
}
EOF
5.启动docker
sudo systemctl start docker
6.查看docker文件的存放路径
docker info |grep Dir