解决Docker根目录切换与数据迁移

我们知道docker安装之后默认的服务数据存放根路径为/var/lib/docker目录下,var目录默认使用的是根分区的磁盘空间;所以这是非常危险的事情;随着我们镜像、启动的容器实例开始增多的时候,磁盘所消耗的空间也会越来越大,所以我们必须要做数据迁移和修改docker服务的默认存储位置路径;有多种方式是可以修改docker默认存储目录路径的,但是最好是在docker安装完成后,第一时间便修改docker的默认存储位置路径为其他磁盘空间较大的目录(一般企业中为/data目录),规避迁移数据过程中所造成的风险。
  
1)首先我们准备一块空间充足的磁盘进行分区、格式化、挂载

[root@docker ~]# fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xad62b6a2.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +50G
Partition 1 of type Linux and of size 50 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@docker ~]# mkfs.ext4 /dev/sdb1 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13107200 blocks
655360 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2162163712
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@docker ~]# partprobe
[root@docker ~]# mkdir /data/docker/lib/  -pv 
mkdir: created directory ‘/data/docker’
mkdir: created directory ‘/data/docker/lib/’


[root@docker ~]# vim /etc/fstab
/dev/sdb1      /data/docker/lib/         ext4       defaults  0 0 
[root@docker ~]# df -h | grep "sdb1"
/dev/sdb1        50G   53M   47G   1% /data/docker/lib

2)迁移数据到新目录

[root@docker ~]# docker image ls 
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
192.168.126.155/test/httpd    v0.1                7d85cc3b2d80        2 weeks ago         154MB
httpd                         latest              7d85cc3b2d80        2 weeks ago         154MB
httpd                         v0.1                7d85cc3b2d80        2 weeks ago         154MB
[root@docker ~]# systemctl stop docker
[root@docker ~]# rsync -avz /var/lib/docker/  /data/docker/lib/
[root@docker ~]# ls /data/docker/lib/
builder   containers  lost+found  overlay2  runtimes  tmp    volumes
buildkit  image       network     plugins   swarm     trust
[root@docker ~]# 

3)创建docker的配置文件

[root@docker ~]# 
[root@docker ~]# mkdir -p /etc/systemd/system/docker.service.d/
[root@docker ~]# vim /etc/systemd/system/docker.service.d/devicemapper.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd  --graph=/data/docker/lib/

[root@docker ~]# systemctl daemon-reload 
[root@docker ~]# systemctl restart docker
[root@docker ~]# docker info  | grep "Dir"
 Docker Root Dir: /data/docker/lib
[root@docker ~]# 
[root@docker ~]# docker image ls 
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
192.168.126.155/test/httpd    v0.1                7d85cc3b2d80        2 weeks ago         154MB
httpd                         latest              7d85cc3b2d80        2 weeks ago         154MB
httpd                         v0.1                7d85cc3b2d80        2 weeks ago         154MB
[root@docker ~]# df -h |grep "sdb1"
/dev/sdb1        50G  2.8G   44G   6% /data/docker/lib
##迁移完成!!!

你可能感兴趣的:(Docker学习)