如何限制docker中的IO速度和同时与系统共享文件?

时间:2016-03-22 03:54:18

标签: docker

暂时,我使用像-v这样的/rootfs/shared_dir:/docker/docker_file参数。

我的容器在文件夹中创建了一些数据文件,我想与我的系统共享该文件夹。

另外,我想限制docker容器中的IO速度。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:12)

版本1.10之后,docker添加了新功能来操纵容器中的IO速度。

~$ docker help run | grep -E 'bps|IO'
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  --blkio-weight                  Block IO (relative weight), between 10 and 1000
  --blkio-weight-device=[]        Block IO weight (relative device weight)
  --device-read-bps=[]            Limit read rate (bytes per second) from a device
  --device-read-iops=[]           Limit read rate (IO per second) from a device
  --device-write-bps=[]           Limit write rate (bytes per second) to a device
  --device-write-iops=[]          Limit write rate (IO per second) to a device

例如,如果要将写入速度限制为50 MB / s

$ docker run -it --rm --device-write-bps /dev/sda:50mb ubuntu /bin/bash
root@e88db9cb1263:/# time dd if=/dev/zero of=test.out bs=1M count=1024 oflag=direct
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 20.4334 s, 52.5 MB/s

real    0m20.437s
user    0m0.016s
sys     0m0.472s

没有限制我的速度是291 MB / s

~$ docker run -it ubuntu bash
root@2911226bd75e:/# time dd if=/dev/zero of=test.out bs=1M count=1024 oflag=direct
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 3.69373 s, 291 MB/s

real    0m3.696s
user    0m0.004s
sys     0m0.496s

答案 1 :(得分:0)

在我的情况下,我使用Centos7和名为volume的docker,

第一

找到设备名称属于您想要限制的容器。

docker inspect your_container_id | grep DeviceName

比得上

 "DeviceName": "docker-8:3-331888-ef7d6d85f9bda07ba4c5eb3b6f4b903fcb72554c223d9e467ab7df74917bc31c"

第二

df -h

那么你会得到像

这样的东西
/dev/dm-2        10G  416M  9.6G    5% /var/lib/docker/devicemapper/mnt/584688469e65ec95e5618f7a16692294219a1be90537659b0888e35225bb5721
shm              64M     0   64M    0% /var/lib/docker/containers/863b665d8587cf37eeab2ec82e72b879ffbee8cca65bb816e991108fc290545f/shm
/dev/dm-1        10G  223M  9.8G    3% /var/lib/docker/devicemapper/mnt/ab182292ebe8febe945783b84a1340232855c17ede23bc94ffc3eaecfa12e3d2
shm              64M     0   64M    0% /var/lib/docker/containers/c4262a88cd77a91cbe83e4132bccb52573b550cb3db6194f9a55d31d7a001449/shm
/dev/dm-4        10G  573M  9.5G    6% /var/lib/docker/devicemapper/mnt/8b422f5b431d5927940952a5f1f6486efe0771c555c8989cfcd5b62bc85d4ede
/dev/dm-3        10G  778M  9.3G    8% /var/lib/docker/devicemapper/mnt/fd9ff8355bd57a7808294fed7dccec10aba74faf494aa53c23ef5bb1cb561c5f
/dev/dm-5        10G  808M  9.3G    8% /var/lib/docker/devicemapper/mnt/1efe06cb650ab88dc0530220e26ff0809f6af6c8cc368f8f5f8f4ec2e8719a7b

dm-x的数量取决于您运行的容器数量。找到与您的容器设备名称编号匹配的dm-x。

然后你可以做@stambata说的话:

$ docker run -it --rm --device-write-bps /dev/dm-x:50mb centos /bin/bash