当前位置: > Linux服务器 > Docker >

docker动态扩展容器空间大小

时间:2016-06-04 22:00来源:linux.it.net.cn 作者:IT
 今天同事要在docker的jenkins容器里安装sdk,当他把他之前安装好的sdk传我时,我才发现居然有16g,docker的容器整个都才只有10个g,还好让我找到个可以动态扩展docker容器的文章,先分享给大家.
  系统:centos 6(64位)
  docker版本:1.7.1
  docker存储引擎:devicemapper
  宿主机分区格式必须是:ext2、ext3、ext4
脚本内容:
cat dynamic_modify_docker_disk.sh
 
01 #!/bin/bash
02 #This script is dynamic modify docker container disk
03 #Author Deng Lei
04 if [ -z $1 ] || [ -z $2 ]; then
05     echo "Usage: container_name increase_capacity"
06     echo "Example: I want increase 11G to test"
07     echo "The command is:   sh `basename $0` test 11"
08     exit 1
09                                                                                                                                                         fi
10 if [ `docker inspect $1 &>>/dev/null &&  echo 0 || echo 1` -eq 1 ];then
11     echo "The container $1 is no exist!"
12     exit 1
13 fi
14 container_id=`docker inspect -f '{{ .Id }}' $1`
15 now_disk=`dmsetup table /dev/mapper/docker-*-$container_id|awk '{print $2}'`
16 disk=$(($2*1024*1024*1024/512))
17 if [ $disk -lt $now_disk ];then
18     echo "I can't shink container $1 from $(($now_disk*512/1024/1024/1024))G to ${2}G!I only modify contanier increase disk!"
19     exit 1
20 fi
21 dmsetup table /dev/mapper/docker-*-$container_id|sed "s/0 [0-9]* thin/0 $disk thin/"|dmsetup load /dev/mapper/docker-*-$container_id
22 dmsetup resume /dev/mapper/docker-*-$container_id
23 resize2fs /dev/mapper/docker-*-$container_id
24 if [ $? -eq 0 ];then
25     echo "dynamic container $1 disk to ${2}G is success!"
26 else
27     echo "dynamic container $1 disk to ${2}G is fail!"
28 fi
 
使用方法:
chmod + x dynamic_modify_docker_disk.sh
./dynamic_modify_docker_disk.sh [容器名] [要调整的大小]
 
example:
./dynamic_modify_docker_disk.sh jenkins 30
 
我这里把jenkins容器的空间调大了30g.
ps:这个脚本只能增大容器的空间,不能缩小,所以请考虑清楚再使用.有兴趣的可以看看下面这篇文章,他讲得更加详细些.
http://dl528888.blog.51cto.com/2382721/1606170


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容