edit-icon download-icon

How to increase data disks for Container Service Docker?

Last Updated: Jan 15, 2018

Docker data is stored on disks by using the union file system. If the number of containers or images needing to be run on the machines is continuously increased, the disk size might not meet the requirements. In this situation, increase the data disks to expand the storage space for Docker data directory.

Docker data directory

For Docker, the container and image data is stored in the /var/lib/docker directory by default. You can check the currently occupied disk size of this directory by running the du command.

For example:

  1. du -h --max-depth=0 /var/lib/docker
  2. 7.9G /var/lib/docker

Change Docker data disk

Many Docker images are big. Therefore, several images might occupy large disk space, which leads to insufficient disk space. By increasing the data disks for the Docker data directory, the requirements of increasing images or containers continuously can be met.

Purchase ECS data disk and mount to machines needing expansion

  1. Log on to the Elastic Compute Service (ECS) console to purchase the cloud disk with corresponding configurations.

  2. Click Instances in the left-side navigation pane.

  3. Select the region and then click the instance name or click Manage at the right of the instance.

  4. Click Instance Disks in the left-side navigation pane.

  5. Click Attach Disk in the upper-right corner.

  6. Select the purchased disk and record the mount point /dev/xvd* or /dev/vd*.

    Determine the mount point by running the cd command. The mount point of the I/O optimized instance is /dev/vd*.

Log on to the machine and format the mounted disk

  1. Run ls -l /dev/xvd* or ls -l /dev/vd* on the machine to view the disk ID, which is consistent with your recorded one.

  2. Partition the disk by running the fdisk command. Then, format the disk by using mkfs.ext4. For more information, see Linux _ Format and mount a data disk.

    For example:

    1. root@iZbp16h1ijt5er5wempg4sZ:~# ls -l /dev/vd*
    2. brw-rw---- 1 root disk 253, 0 Jan 5 17:44 /dev/vda
    3. brw-rw---- 1 root disk 253, 1 Jan 5 17:44 /dev/vda1
    4. brw-rw---- 1 root disk 253, 16 Jan 5 17:55 /dev/vdb
    5. root@iZbp16h1ijt5er5wempg4sZ:~# fdisk -S 56 /dev/vdb
    6. Welcome to fdisk (util-linux 2.27.1).
    7. Changes will remain in memory only, until you decide to write them.
    8. Be careful before using the write command.
    9. Device does not contain a recognized partition table.
    10. Created a new DOS disklabel with disk identifier 0x44e128c4.
    11. Command (m for help): n
    12. Partition type
    13. p primary (0 primary, 0 extended, 4 free)
    14. e extended (container for logical partitions)
    15. Select (default p): p
    16. Partition number (1-4, default 1): 1
    17. First sector (2048-41943039, default 2048):
    18. Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039):
    19. Created a new partition 1 of type 'Linux' and of size 20 GiB.
    20. Command (m for help): wq
    21. The partition table has been altered.
    22. Calling ioctl() to re-read partition table.
    23. Syncing disks.
    24. root@iZbp16h1ijt5er5wempg4sZ:~# ll /dev/vd*
    25. brw-rw---- 1 root disk 253, 0 Jan 5 17:44 /dev/vda
    26. brw-rw---- 1 root disk 253, 1 Jan 5 17:44 /dev/vda1
    27. brw-rw---- 1 root disk 253, 16 Jan 5 17:58 /dev/vdb
    28. brw-rw---- 1 root disk 253, 17 Jan 5 17:58 /dev/vdb1 ## Add partition
    29. root@iZbp16h1ijt5er5wempg4sZ:~# mkfs.ext4 /dev/vdb1 ## Format
    30. mke2fs 1.42.13 (17-May-2015)
    31. Creating filesystem with 5242624 4k blocks and 1310720 inodes
    32. Filesystem UUID: cef1625c-7533-4308-bc44-511580e3edc8
    33. Superblock backups stored on blocks:
    34. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    35. 4096000
    36. Allocating group tables: done
    37. Writing inode tables: done
    38. Creating journal (32768 blocks): done
    39. Writing superblocks and filesystem accounting information: done

Move Docker data to a new disk

  1. Stop Docker daemon first to guarantee the data integrity in the process of moving Docker data. Run the command service docker stop to stop the Docker daemon.

  2. Move the data in the Docker default data directory to a backup directory. For example, if the backup directory is /var/lib/docker_data, run the command mv /var/lib/docker /var/lib/docker_data.

  3. Mount the newly formatted disk to the /var/lib/docker directory. Run the command echo "/dev/vdb1 /var/lib/docker ext4 defaults 0 0" >>/etc/fstab && mkdir /var/lib/docker && mount -a.

  4. Move the backed up Docker data to the new disk. Run the command mv /var/lib/docker_data/* /var/lib/docker/.

Start Docker daemon and check data location

  1. Start Docker daemon and run the command service docker start.

  2. Run the command df. You can see /var/lib/docker is mounted to the new disk.

    1. root@iZbp16h1ijt5er5wempg4sZ:/# df -h
    2. Filesystem Size Used Avail Use% Mounted on
    3. udev 2.0G 0 2.0G 0% /dev
    4. tmpfs 396M 7.1M 389M 2% /run
    5. /dev/vda1 40G 2.7G 35G 8% /
    6. tmpfs 2.0G 476K 2.0G 1% /dev/shm
    7. tmpfs 5.0M 0 5.0M 0% /run/lock
    8. tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
    9. tmpfs 396M 0 396M 0% /run/user/0
    10. /dev/vdb1 20G 2.1G 17G 12% /var/lib/docker ## This directory is mounted to the new disk.
    11. ....
  3. Run the command docker ps to check if containers are lost. Restart the related containers as required, for example, the containers without configuring the restart:always label.

Thank you! We've received your feedback.