How to limit IO speed in docker and share file wit

2019-02-18 22:11发布

Temporarily, I use -v param like /rootfs/shared_dir:/docker/docker_file.

My container create some data file in the folder, And I want share the folder with my system.

Also I want to limit the IO speed in the docker container. How can I do this?

标签: docker
2条回答
姐就是有狂的资本
2楼-- · 2019-02-18 22:38

After version 1.10 docker added new features to manipulate IO speed in the container.

~$ 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

for example if you want to limit write speed to 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

without limit my speed is 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
查看更多
Deceive 欺骗
3楼-- · 2019-02-18 22:49

In my case, I'm using Centos7 with docker named volume,

First

find the device name belongs to the container you wanna limit.

docker inspect your_container_id | grep DeviceName

Than get something like

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

Second

df -h

then you will get something like

/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

the number of dm-x depends on how many containers you are running.find the dm-x that match your container device name number.

Then you can just did what @stambata said:

$ docker run -it --rm --device-write-bps /dev/dm-x:50mb centos /bin/bash
查看更多
登录 后发表回答