2025 centos系统安装docker并迁移docker,docker安装到指定的目录以及文件迁移,docker迁移文件

需求:centos7系统,由于硬盘有限,需要把docker安装到指定的目录下,怎么安装?

1. 安装Docker引擎

步骤1.1:卸载旧版本(如有)

bash 复制代码
sudo yum remove -y docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine

步骤1.2:安装依赖工具

bash 复制代码
sudo yum install -y yum-utils epel-release wget device-mapper-persistent-data lvm2

步骤1.3:添加Docker官方仓库

bash 复制代码
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

步骤1.4:安装Docker

bash 复制代码
#yum list docker-ce --showduplicates | sort -r #查看yum仓库中可以安装的docker版本
#yum -y install docker-ce-18.06.1.ce-3.el7 #安装固定版本
sudo yum install -y docker-ce docker-ce-cli containerd.io  #安装docker最新版

2. 修改Docker数据存储目录

步骤2.1:停止Docker服务

bash 复制代码
sudo systemctl start docker
sudo systemctl stop docker

步骤2.2:创建自定义目录

bash 复制代码
sudo mkdir -p /custom/docker

步骤2.3:迁移旧数据

如果已存在旧数据,迁移到新目录:

bash 复制代码
sudo rsync -aqxP /var/lib/docker/ /custom/docker

步骤2.4:修改Docker配置文件

bash 复制代码
##只需要添加此行  "data-root": "/custom/docker",

cat > /etc/docker/daemon.json << EOF
{
  "data-root": "/custom/docker",
  "registry-mirrors": ["https://docker.registry.cyou","https://docker-cf.registry.cyou","https://dockercf.jsdelivr.fyi","https://docker.jsdelivr.fyi","https://dockertest.jsdelivr.fyi","https://mirror.aliyuncs.com","https://dockerproxy.com","https://mirror.baidubce.com","https://docker.m.daocloud.io","https://docker.nju.edu.cn","https://docker.mirrors.sjtug.sjtu.edu.cn","https://docker.mirrors.ustc.edu.cn","https://mirror.iscas.ac.cn","https://docker.rainbond.cc"],
  "insecure-registries": ["0.0.0.0/0"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
EOF

步骤2.5:处理SELinux(如启用)

如果系统启用了SELinux,需更新安全上下文:

bash 复制代码
sudo yum install -y policycoreutils-python-utils  # 安装必要工具
sudo semanage fcontext -a -t container_var_lib_t "/custom/docker(/.*)?"
sudo restorecon -Rv /custom/docker

步骤2.6:重启Docker服务

bash 复制代码
sudo systemctl restart docker
sudo systemctl enable docker

3. 验证安装和配置

步骤3.1:检查Docker是否运行

bash 复制代码
sudo systemctl status docker

步骤3.2:查看Docker存储目录

bash 复制代码
docker info | grep "Docker Root Dir"

步骤3.3:测试运行容器

bash 复制代码
sudo docker run hello-world

4. 验证自定义目录的使用

bash 复制代码
ls /custom/docker
du -sh /custom/docker/

步骤4.2:清理测试容器

bash 复制代码
docker rm $(docker ps -aq)

5. 删除原来docker文件

bash 复制代码
ll /var/lib/docker
rm -fr /var/lib/docker

常见问题

问题1:权限不足

确保自定义目录的所有权和权限正确:

sudo chown -R root:root /custom/docker

sudo chmod 711 /custom/docker

问题2:SELinux限制

若出现权限错误,确保已执行步骤2.5的SELinux配置。

总结

通过以上步骤,Docker已安装到CentOS系统,且数据存储目录修改为 /custom/docker。验证时需确保:

Docker服务状态为 active (running)。

docker info 显示正确的存储路径。

自定义目录中生成容器和镜像数据。

相关推荐
IT瑞先生16 小时前
docker-compose下快速部署实操——持续更新...
运维·docker·容器
爱码少年1 天前
Docker遍历镜像生成导出镜像命令六法
docker·shell
BullSmall1 天前
Sonarqube9.9 docker 镜像启动后 conf 下的配置文件没有了
docker·容器·eureka
ruofu331 天前
wsl端是py312,但是项目是py311, 如何下载py311的依赖包(wheels)
python·docker
spider_xcxc1 天前
Docker 实战:容器化多服务应用,Dockerfile 与 Compose 配置全解析
docker·云原生·eureka·虚拟化·容器化
晚风吹长发1 天前
Docker使用——Docker容器及相关命令
linux·运维·服务器·docker·容器·架构
ShiXZ2131 天前
Docker Compose 安装与配置指南
运维·docker·容器
是潮汕的灿灿展吖1 天前
Centos离线部署nfs操作
linux·运维·centos
BullSmall1 天前
Anolis OS 8.10 Docker 部署 SonarQube 9.9 完整教程
运维·docker·容器
AAA@峥1 天前
CentOS7 搭建 ELK 企业级日志集群:从部署到日志可视化完整实战
运维·elk·centos