This topic describes how to mount a data disk to the Docker data directory. The existing
disk capacity may be insufficient to support an increase in the number of containers
or images on a node. You can mount a data disk to the Docker data directory. This
allows you to expand the disk capacity and provide more storage space for containers
and images.
Docker data directory
Docker data is stored in disks based on a union file system (UnionFS). The default
container data and image data of Docker is stored in the
/var/lib/docker directory. You can run the
du command to view the disk size that is occupied by this directory.
# du -h --max-depth=0 /var/lib/docker
7.9G /var/lib/docker
Scenarios
A Docker image occupies a large amount of disk space. If you want to use multiple
Docker images or a large number of containers, you must mount a data disk to the Docker
data directory. This allows you to ensure that sufficient disk capacity is available.
Mount a data disk
To mount a disk to the Docker data directory, perform the following steps:
- Create a data disk and mount the disk to the node for which you want to expand the
disk capacity. For more information, see Create a disk.
- Log on to the Elastic Compute Service (ECS) console and create the required cloud disk.
- In the left-side navigation pane, click Instances.
- On the Instances page, click the ID of the ECS instance that you want to manage to
go to the Instance Details page.
- In the left-side navigation pane, click Disks.
- On the Disks page, click Attach Disk.
- In the dialog box that appears, select the created disk from the Target Disk drop-down list. Click OK.
- Click Attach to attach the disk to the specified ECS instance, and record the mount point
/dev/xvd*
or /dev/vd*
.
- Log on to the ECS instance and format the mounted disk.
- In the command-line interface (CLI), enter ls -l /dev/xvd* or ls -l /dev/vd* to check whether the returned mount point is the same as that you have recorded.
- In the CLI, enter fdisk to partition the disk, and then enter mkfs.ext4 to format the disk.
- Migrate the Docker data to the disk.
- To ensure that data can be migrated, In the CLI, enter service docker stop to stop Docker daemon, and enter service kubelet stop to stop kubelet.
- Migrate the Docker directory data to a backup directory, such as mv /var/lib/docker /var/lib/docker_data.
- Mount the formatted disk to the /var/lib/docker and /var/lib/kubelet directories. In the CLI, run the following command:
echo "/dev/xvdb1 /var/lib/container/ ext4 defaults 0 0" >>/etc/fstab
echo "/var/lib/container/kubelet /var/lib/kubelet none defaults,bind 0 0" >>/etc/fstab
echo "/var/lib/container/docker /var/lib/docker none defaults,bind 0 0" >>/etc/fstab
mkdir /var/lib/docker
mount -a
- Migrate the Docker data that has been backed up to the formatted disk, such as mv /var/lib/docker_data/* /var/lib/docker/.
- Start the Docker daemon and kubelet, and check the data location.
- In the CLI, enter service docker start to start the Docker daemon, and enter service kubelet start to start kubelet.
- In the CLI, enter df to check whether /var/lib/docker has been mounted to the disk. If you want to start the Kubernetes cluster, skip this
step.
- In the CLI, enter docker ps to check whether containers are lost. Restart containers as required. For example,
you can restart a container to which the
restart:always
label is attached.
- If a container has been migrated to other nodes, you can schedule it back to the node
to which you have attached the disk.