修改docker镜像、容器默认存放位置

1.修改需求

因为Docker默认是存放在系统盘中/var/lib/docker的,当用的时间比较久后,产生的镜像及容器越来越多之后,可能会导致你的系统盘满了,这时我们需要将Docker的镜像及容器指向另外一个路径

2.修改步骤

2.1docker镜像、容器默认存放于/var/lib/docker

[root@docker02 ~]# cd /var/lib/docker
[root@docker02 docker]# ls
builder  buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes

2.2修改docker镜像、容器存放位置

1.编辑配置文件 /etc/docker/daemon.json
加入以下一行
"graph":"/data/docker"        #目录可自行修改

2.新建目录
[root@docker02 ~]# mkdir -p /data/docker 

3.修改后的配置文件
[root@docker02 ~]# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://gqk8w9va.mirror.aliyuncs.com"],
  "graph":"/data/docker"
}

2.3重启docker

[root@docker02 ~]# systemctl restart docker

2.4拷贝原镜像和容器到新目录

1.修改完docker镜像、容器存放位置后,原先的镜像和容器查看不存在
[root@docker02 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@docker02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2.拷贝原镜像和容器到新目录
[root@docker02 ~]# \cp -rp /var/lib/docker/* /data/docker/

3重启docker
[root@docker02 ~]# systemctl restart docker

4.查看修改
[root@docker02 ~]# docker info|grep Docker
Docker Root Dir: /data/docker

5.查看镜像和容器,已恢复
[root@docker02 ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
jumpserver/jms_all   1.4.8               e9274ba449e8        3 months ago        1.31GB
[root@docker02 ~]# docker ps -a
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS                                        NAMES
2789b0d4a200        jumpserver/jms_all:1.4.8   "entrypoint.sh"     About an hour ago   Up 49 seconds       0.0.0.0:80->80/tcp, 0.0.0.0:2222->2222/tcp   jms_all

6.测试镜像和容器都无问题后可以删除/var/lib/docker
[root@docker02 ~]# rm -rf /var/lib/docker
泡泡吐肥皂o © gitbook.pptfz.top 2021 all right reserved,powered by Gitbook文件修订时间: 秃笔南波湾!!!

results matching ""

    No results matching ""