Docker 存储设置

docker默认单个容器可以使用数据空间大小10GB,docker可用数据总空间100GB,元数据可用总空间2GB。用docker info信息可以查看Data Space Total、Metadata Space Total等信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ docker info
Containers: 23
Images: 45
Storage Driver: devicemapper
Pool Name: docker-8:36-13501344-pool
Pool Blocksize: 65.54 kB
Data file: /var/lib/docker/devicemapper/devicemapper/data
Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
Data Space Used: 1.684 GB
Data Space Total: 107.4 GB
Metadata Space Used: 2.699 MB
Metadata Space Total: 2.147 GB
Library Version: 1.02.89-RHEL6 (2014-09-01)
Execution Driver: native-0.2
Kernel Version: 2.6.32-504.el6.x86_64
Operating System: <unknown>
CPUs: 24
Total Memory: 47.09 GiB
Name: docker-01
ID: S3F6:VR2L:WH7X:QLQO:RH6P:EVBT:QRUW:NNMV:4MQ7:YADK:Q6OV:Q2IQ
$

占用本地文件系统空间大小如下(data存放的是image和container数据,metadata存放的是tag、name、status等基本信息):

1
2
3
4
5
$ ll /var/lib/docker/devicemapper/devicemapper/ -h
total 35G
-rw------- 1 root root 100G Jun 5 13:43 data
-rw------- 1 root root 2.0G Jun 5 13:43 metadata
$

可以使用df -hT查看容器可以使用的空间大小,下面的大约是10GB(同一数据块,不同文件系统大小存在差异)

1
2
3
4
5
6
7
8
9
10
11
$ docker exec zhijie df -hT
Filesystem Type Size Used Avail Use% Mounted on
rootfs rootfs 9.8G 1.8G 7.5G 20% /
/dev/mapper/docker-8:3-61079671-9623cd0329b8e2a093cae0911ce6dabe515397391252ed410d6dd9b5c06da77f
ext4 9.8G 1.8G 7.5G 20% /
tmpfs tmpfs 24G 0 24G 0% /dev
shm tmpfs 64M 0 64M 0% /dev/shm
/dev/sda3 ext4 1.1T 228G 800G 23% /etc/resolv.conf
/dev/sda3 ext4 1.1T 228G 800G 23% /etc/hostname
/dev/sda3 ext4 1.1T 228G 800G 23% /etc/hosts
$

当一个容器的数据空间大于10GB后,那么这个容器将不能写入新的数据文件。如果容器需要很大的数据空间,可以使用数据卷挂在到宿主机或存储上。当容器太多使用数据卷挂载方式后,所有容器使用的数据空间已经大于100GB,这时将不能新建或运行容器。如何突破这种限制,这篇文章给出了解答:https://github.com/docker/docker/tree/master/daemon/graphdriver/devmapper
分两种情况讨论:
1、如果docker第一次启动,在启动docker前,可以使用参数–storage-opt分别指定dm.basesize、dm.loopdatasize、dm.loopmetadatasize等项,指定单个容器可用数据空间、docker可用数据空间、元数据可用数据空间。以centos6.6为例(修改配置文件/etc/sysconfig/docker):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cat /etc/sysconfig/docker
# /etc/sysconfig/docker
#
# Other arguments to pass to the docker daemon process
# These will be parsed by the sysv initscript and appended
# to the arguments list passed to docker -d

other_args="--storage-opt dm.basesize=20G --storage-opt dm.loopdatasize=200G --storage-opt dm.loopmetadatasize=4G -H tcp://127.0.0.1:2345 -H unix:///var/run/docker.sock"
DOCKER_CERT_PATH=/etc/docker

# Location used for temporary files, such as those created by
# # docker load and build operations. Default is /var/lib/docker/tmp
# # Can be overriden by setting the following environment variable.
# # DOCKER_TMPDIR=/var/tmp
$

分别指定单个容器可用数据空间20GB、docker可用数据空间200GB、元数据可用空间4GB。然后启动docker,用docker info可以查看配置已生效:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ docker info
Containers: 0
Images: 5
Storage Driver: devicemapper
Pool Name: docker-8:36-13500724-pool
Pool Blocksize: 65.54 kB
Data file: /var/lib/docker/devicemapper/devicemapper/data
Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
Data Space Used: 475.5 MB
Data Space Total: 214.7 GB
Metadata Space Used: 1.364 MB
Metadata Space Total: 4.295 GB
Library Version: 1.02.89-RHEL6 (2014-09-01)
Execution Driver: native-0.2
Kernel Version: 2.6.32-504.el6.x86_64
Operating System: <unknown>
CPUs: 24
Total Memory: 47.09 GiB
Name: docker-02
ID: Y6M7:AMXG:WQSI:JTPJ:SCNL:B76Z:6VHY:EMWH:56ND:LZBN:FTTY:3EIV
$

2、如果是正在运行的docker,需要导出镜像docker commit、docker push、docker save等操作,然后停止docker服务,删除docker的数据文件(centos6.6是rm -rf /var/lib/docker/)。再修改docker的配置文件/etc/sysconfig/docker,重启docker服务生效。最后导入备份的镜像启动容器。

----------------本文结束 感谢阅读----------------