CentOS7修改Docker容器和镜像默认存储位置

本文被帮助文档:

  • 链接1
  • 链接2

CentOS7修改Docker容器和镜像默认存储位置

通常,当你开始使用docker时,我们并不太关心Docker 用于存储镜像和容器的默认目录。当使用Docker进行更多实验时,Docker开始占用大量的空间时,你不得不按下紧急按钮。所以现在是时候把故障排除放在如何改变docker的默认存储位置了

在试图改变Docker的默认存储位置时,我们必须知道一些重要的信息。

docker 当前使用的默认存储位置
docker 当前使用的存储驱动程序
镜像和容器要存放的新存储空间

Docker 默认的位置在/var/lib/docker,当前所有的镜像、容器都存储在这儿。如果你有任何在运行的容器,停止这些容器,并确保没有容器在运行,然后运行以下命令,确定当前Docker使用的存储驱动。

1、查看docker当前信息

注:此处需要注意docker的两个信息:

  • Storage Driver: overlay2 当前Docker使用的存储驱动
  • Docker Root Dir: /var/lib/docker 当前docker使用的数据目录

docker info

[root@localhost ~]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 18.09.6
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-514.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 976.5MiB
Name: localhost.localdomain
ID: 3EGY:424H:ER2C:CBQ7:KUD5:SFRJ:TVDK:XV4Y:AFYJ:SOOL:7OM6:65K5
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

2、停止docker服务

systemctl stop docker

3、修改docker服务启动文件

[root@localhost ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --graph=/mnt/docker

注意:

  • 原配置保持不变,添加--graph=/mnt/docker,/mnt/docker是新的存储位置

  • 以下两个文件均可修改docker配置:

/etc/systemd/system/multi-user.target.wants/docker.service
/usr/lib/systemd/system/docker.service

4、将原先目录下所有的文件和目录拷贝到新目录下

[root@localhost mnt]# cp -rf /var/lib/docker/* /mnt/docker/

[root@localhost docker]# pwd
/mnt/docker
[root@localhost docker]# ls
builder  buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes

5、重新加载配置并启动

[root@localhost docker]#  systemctl daemon-reload
[root@localhost docker]# systemctl start docker

6、检查docker信息

[root@localhost docker]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 18.09.6
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-514.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 976.5MiB
Name: localhost.localdomain
ID: 3EGY:424H:ER2C:CBQ7:KUD5:SFRJ:TVDK:XV4Y:AFYJ:SOOL:7OM6:65K5
Docker Root Dir: /mnt/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

注:

查看==Docker Root Dir: /mnt/docker==信息可看到,docker服务存储位置已修改

你可能感兴趣的:(CentOS7修改Docker容器和镜像默认存储位置)