Docker adjust Rootfs partition size and dynamically expand container space

Source: Internet
Author: User
Tags ssh centos docker run

Adjust the partition size of Rootfs in Docker

The main introduction here is to adjust the size of the rootfs within the container, Rootfs the default is 10G, we can increase the size by the following method.

bash-4.1# DF-LHT
FileSystem Type Size Used avail use% mounted on
Rootfs rootfs 9.8G 1.4G 7.9G 15%/
Tmpfs tmpfs 921M 0 921M 0%/dev
SHM tmpfs 64M 0 64M 0%/dev/shm
/dev/vdb xfs 100G 2.1G 98G 3%/etc/resolv.conf
/dev/vdb xfs 100G 2.1G 98G 3%/etc/hostname
/dev/vdb xfs 100G 2.1G 98G 3%/etc/hosts
Tmpfs tmpfs 921M 0 921M 0%/run/secrets
Tmpfs tmpfs 921M 0 921M 0%/proc/kcore

First you must stop Docker,service Docker stop and then delete the files in the/var/lib/docker directory, which will cause all mirrors you build to be deleted, Rm-rf/var/lib/docker, and then start Docker, Service Docker start. Use the following parameters to set the size of the rootfs.

docker-d--storage-opt dm.basesize=30g

When the settings are complete, use the following command to view the results, and you can see that the partition size of the ROOTFS has been adjusted to 30G:

bash-4.1# DF-LHT
FileSystem Type Size Used avail use% mounted on
Rootfs rootfs 30G 1.4G 27G 5%/
Tmpfs tmpfs 921M 0 921M 0%/dev
SHM tmpfs 64M 0 64M 0%/dev/shm
/dev/vdb xfs 100G 2.7G 98G 3%/etc/resolv.conf
/dev/vdb xfs 100G 2.7G 98G 3%/etc/hostname
/dev/vdb xfs 100G 2.7G 98G 3%/etc/hosts
/DEV/VDA1 xfs 20G 3.8G 16G 20%/data/ukernel
Tmpfs tmpfs 921M 0 921M 0%/run/secrets
Tmpfs tmpfs 921M 0 921M 0%/proc/kcore

This approach is not very flexible, you can refer to the following methods for dynamic expansion, but in my CentOS

The following error occurred on 7: so no further research.

[root@10-10-63-106 ~]#./dynamic_modify_docker_disk.sh Trusting_lalande 20
RESIZE2FS 1.42.9 (28-dec-2013)
Resize2fs:device or resource busy while trying to open/dev/mapper/docker-253:1-100932822- A1e38bd164a34a17fa524e6b304466433f153974dcae82d06af465bef7afaedb
Couldn


Docker The dynamic expansion container space size for advanced applications



The default space for the Docker container is 10G, and if you want to specify the size of the default container (specified when you start the container), you can specify it by the Dm.basesize parameter in the Docker configuration file, such as

docker-d--storage-opt dm.basesize=20g

is to specify the default size of 20G, the specific parameters can refer to Https://github.com/docker/docker/tree/master/daemon/graphdriver/devmapper

The above method is only true when the container is generated, and the changes need to restart Docker, can not be dynamic to the runtime to specify the size of the container, I will explain how to dynamically expand the container space size.

Advantages of dynamic Expansion:

1, do not need to modify the Docker configuration, and restart the Docker service;

2, can be directly to the operation of the container dynamic expansion (can only increase, can not shrink);

Disadvantages:

1, Docker host partition format must be ext2, ext3, Ext4;

2, Docker storage engine must be Devicemapper

Storage Engine view, you can use Docker info to view

15:25:49 # Docker Info
Containers:5
images:62
Storage Driver:devicemapper
Pool Name:docker-8:17-37748738-pool
Data File:/data1/docker/devicemapper/devicemapper/data
Metadata File:/data1/docker/devicemapper/devicemapper/metadata
Data Space used:21498.9 Mb
Data Space total:102400.0 Mb
Metadata Space used:13.7 Mb
Metadata Space total:2048.0 Mb
Execution driver:lxc-1.0.6
Kernel version:3.10.0-123.el7.x86_64

From the above storage driver can see the engine I use.

The following are examples of dynamic extensions:

1. New Test Container set up

15:23:48 # Docker run--privileged-d-P--name= ' test ' Docker.ops-chukong.com:5000/centos6-http:new/usr/bin/supervis Ord
1716fe941926dbd0b247b85d73e83b9465322a5005edc3c6182b59a6ac0939a7
Root@ip-10-10-27-221:/tmp
15:24:01 # Docker Inspect test|grep-i add
"IPAddress": "172.17.0.18",
Root@ip-10-10-27-221:/tmp
15:24:08 # SSH 172.17.0.18
The authenticity of host ' 172.17.0.18 (172.17.0.18) ' can ' t be established.
RSA key fingerprint is 39:7c:13:9f:d4:b0:d7:63:fc:ff:ae:e3:46:a4:bf:6b.
Are you sure your want to continue connecting (yes/no)? Yes
warning:permanently added ' 172.17.0.18 ' (RSA) to the list of known hosts.
root@172.17.0.18 ' s Password:
Last Login:mon Nov 14:10:39 2014 from 172.17.42.1
root@1716fe941926:~
15:24:13 # DF-HT
FileSystem Type Size Used avail use% mounted on
Rootfs rootfs 9.8G 470M 8.8G 5%/
/dev/mapper/docker-8:17-37748738-1716fe941926dbd0b247b85d73e83b9465322a5005edc3c6182b59a6ac0939a7
Ext4 9.8G 470M 8.8G 5%/
SHM tmpfs 64M 0 64M 0%/dev/shm
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/.dockerinit
/dev/sda3 ext4 518G 30G 462G 6%/etc/resolv.conf
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/etc/hostname
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/etc/hosts
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/.dockerenv

You can see that I'm running the test container's root partition is the EXT4 partition, the container is 10G

2, use my script to dynamically expand the container space

Script Content

15:22:12 # Cat Dynamic_modify_docker_disk.sh
#!/bin/bash
#This script is dynamic modify Docker container disk
#Author Deng Lei
If [-Z $] | | [-Z $]; Then
echo "Usage:container_name increase_capacity"
echo "Example:i want increase 11G to test"
echo "The command Is:sh ' basename $ ' Test 11"
Exit 1
Fi
If [' Docker inspect $ &>>/dev/null && echo 0 | |-echo 1 '-eq 1];then
echo "The container is no exist!"
Exit 1
Fi
Container_id= ' Docker inspect-f ' {{. Id}} ' $ '
now_disk= ' dmsetup table/dev/mapper/docker-*-$container _id|awk ' {print $} '
disk=$ (($2*1024*1024*1024/512))
If [$disk-lt $now _disk];then
echo "I can ' t shink container $ (($now _disk*512/1024/1024/1024)) G to ${2}g! I only modify Contanier increase disk! "
Exit 1
Fi
Dmsetup table/dev/mapper/docker-*-$container _id|sed "s/0 [0-9]* thin/0 $disk thin/" |dmsetup load/dev/mapper/docker-*- $container _id
Dmsetup resume/dev/mapper/docker-*-$container _id
resize2fs/dev/mapper/docker-*-$container _id
If [$?-eq 0];then
echo "Dynamic container disk to ${2}g is success!"
Else
echo "Dynamic container disk to ${2}g is fail!"
Fi

Currently adding 20G space to the test container dynamically

15:24:40 # sh dynamic_modify_docker_disk.sh Test 20
Dynamic container test disk to 20G is success!
Root@ip-10-10-27-221:/tmp
15:24:46 # SSH 172.17.0.18
root@172.17.0.18 ' s Password:
Last Login:tue a 15:24:13 2015 from 172.17.42.1
root@1716fe941926:~
15:24:52 # DF-HT
FileSystem Type Size Used avail use% mounted on
Rootfs rootfs 20G 475M 19G 3%/
/dev/mapper/docker-8:17-37748738-1716fe941926dbd0b247b85d73e83b9465322a5005edc3c6182b59a6ac0939a7
Ext4 20G 475M 19G 3%/
SHM tmpfs 64M 0 64M 0%/dev/shm
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/.dockerinit
/dev/sda3 ext4 518G 30G 462G 6%/etc/resolv.conf
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/etc/hostname
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/etc/hosts
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/.dockerenv

Can be seen to have increased the success

The following increases to 50G for test

15:25:21 # sh dynamic_modify_docker_disk.sh Test 50
Dynamic container test disk to 50G is success!
Root@ip-10-10-27-221:/tmp
15:25:24 # SSH 172.17.0.18
root@172.17.0.18 ' s Password:
Last Login:tue a 15:24:52 2015 from 172.17.42.1
root@1716fe941926:~
15:25:27 # DF-HT
FileSystem Type Size Used avail use% mounted on
Rootfs rootfs 50G 480M 47G 1%/
/dev/mapper/docker-8:17-37748738-1716fe941926dbd0b247b85d73e83b9465322a5005edc3c6182b59a6ac0939a7
Ext4 50G 480M 47G 1%/
SHM tmpfs 64M 0 64M 0%/dev/shm
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/.dockerinit
/dev/sda3 ext4 518G 30G 462G 6%/etc/resolv.conf
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/etc/hostname
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/etc/hosts
/DEV/SDB1 ext4 1.8T 30G 1.7T 2%/.dockerenv

can also increase the success

But if I shrink to 30G,

15:25:45 # sh dynamic_modify_docker_disk.sh Test 30
I can ' t shink container test from 50G to 30g! I only modify Contanier increase disk!

is not scaled down and can only be added to the operation.

As for the principle of dynamic increase, please refer to http://jpetazzo.github.io/2014/01/29/docker-device-mapper-resize/

FAQ:

When using Docker in CentOS 7, the default storage engine is Devicemapper

When you are dynamically adjusting the Docker container disk space, there are

RESIZE2FS 1.42.9 (28-dec-2013)
Resize2fs:device or resource busy while trying to open/dev/mapper/docker-253:1-1270544- D2d2cef71c86910467c1afdeb79c1a008552f3f9ef9507bb1e04d77f2ad5eac4
Couldn ' t find valid filesystem superblock.

The reason is that RESIZE2FS only supports ext2, ext3, Ext4, and does not support XFS

Therefore, it is recommended that the file system format of the Docker server be adjusted to EXT4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.