查找到资料可以对docker容器磁盘大小进行设置,已知方法有两种,基于xfs文件系统的和devicemapper设置的,对比了一下,基于xfs方式的更适合,下面开始操作:

        由于我系统是ubuntu18的。默认文件系统是ext4,所以我先把一块磁盘转为xfs:

cat /etc/fstab 查看文件系统格式

umount -v /home 取消磁盘挂载  或者 umount -v /dev/sdb

mkfs.xfs -f /dev/sdb 格式化为xfs

mount -o pquota,prjquota /dev/sdb /data/ 挂载到/data/目录

这里挂载配额类型如下:

  • 根据用户(uquota/usrquota/quota)
  • 根据组(gquota/grpquota) ;
  • 根据目录(pquota/prjquota)(不能与grpquota同时设定) 

然后参看cat /etc/fstab

# /etc/fstab: static file system information.
# 
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sdb2 during installation
UUID=8f3ddd57-8ff0-4246-9ebe-2e3fe67af716 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sdb1 during installation
UUID=6876-8F90  /boot/efi       vfat    umask=0077      0       1
# swap was on /dev/sdb3 during installation
UUID=7d5607fe-48f5-466f-b26b-e4b15245bde5 none            swap    sw              0       0

我这里有点问题,显示还是原来的/home目录,而且还是ext4,不知道为什么,有大佬知道的请教一下,用df -T命令查看是正常的

后来知道了,更改/etc/fstab才会重启自动挂载,不然只是临时挂载。

用blkid 查看挂载磁盘的UUID,然后修改/etc/fstab如下:

# /etc/fstab: static file system information.
# 
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sdb2 during installation
UUID=8f3ddd57-8ff0-4246-9ebe-2e3fe67af716 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sdb1 during installation
UUID=6876-8F90  /boot/efi       vfat    umask=0077      0       1
# swap was on /dev/sdb3 during installation
UUID=7d5607fe-48f5-466f-b26b-e4b15245bde5 none            swap    sw              0       0
UUID=bb75e51b-643d-48b3-a79e-1f43b02c3e49 /data           xfs      defaults,prjquota     0    0
~# df -T
Filesystem     Type      1K-blocks    Used  Available Use% Mounted on
udev           devtmpfs  131618204       0  131618204   0% /dev
tmpfs          tmpfs      26333868    1412   26332456   1% /run
/dev/sda2      ext4      767830784 4758668  723994948   1% /
tmpfs          tmpfs     131669324       0  131669324   0% /dev/shm
tmpfs          tmpfs          5120       0       5120   0% /run/lock
tmpfs          tmpfs     131669324       0  131669324   0% /sys/fs/cgroup
/dev/sda1      vfat         194348     256     194092   1% /boot/efi
tmpfs          tmpfs      26333864       0   26333864   0% /run/user/0
/dev/sdb       xfs      1951194260 2053256 1949141004   1% /data

mount|grep data查看挂载信息

root@ubuntu:~# mount|grep data
/dev/sdb on /data type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,prjquota)

设置docker存储目录到/data/docker/有两种方式

1,把原来docker存储目录/var/lib/docker软链接到新建的/data/docker/目录下

mkdir -p /data/docker/
cd /var/lib/
mv docker docker.bak
ln -s /data/docker/  .

2,停止docker服务
# systemctl stop docker.service
修改/etc/docker/daemon.json文件值,文件不存在需手动创建
# vim /etc/docker/daemon.json
新增以下信息:

{
"data-root": "/data/docker"
}
启动docker服务# systemctl start docker.service

#docker info 验证Docker Root Dir的值 是否已修改为/opt/docker/data
 

在启动容器时设置参数--storage-opt size=20G 就可以设置容器磁盘大小了:

docker run -idt --storage-opt size=20G ubuntu

用df -T查看:

df -T
Filesystem     Type      1K-blocks    Used  Available Use% Mounted on
udev           devtmpfs  131618204       0  131618204   0% /dev
tmpfs          tmpfs      26333868    1412   26332456   1% /run
/dev/sda2      ext4      767830784 4758668  723994948   1% /
tmpfs          tmpfs     131669324       0  131669324   0% /dev/shm
tmpfs          tmpfs          5120       0       5120   0% /run/lock
tmpfs          tmpfs     131669324       0  131669324   0% /sys/fs/cgroup
/dev/sda1      vfat         194348     256     194092   1% /boot/efi
tmpfs          tmpfs      26333864       0   26333864   0% /run/user/0
/dev/sdb       xfs      1951194260 2053256 1949141004   1% /data
overlay        overlay    20971520       8   20971512   1% /data/docker/overlay2/97a2804d81b

显示已经成功了。

可以通过命令xfs_quota设置来为用户和目录分配配额,也可以通过命令来查看配额信息:


xfs_quota -x -c 'limit bsoft=10M bhard=10M dsfwe' /data xfs_quota -x -c 'report' /data/