資源限額
一個docker host 會執行多個容器,每個容器都需要CPU、記憶體、IO資源。對於KVM和vmware等虛擬化技術,使用者可以控制分配多少CPU、MEM資源給虛擬機器。對於容器而言,Docker也提供了類似的機制避免某個容器因佔用太多資源而影響其他容器乃至整個host的效能
記憶體限額
與作業系統類似,容器可以使用的記憶體包括兩部分,實體記憶體和swap。docker通過下面兩個引數來控制容器記憶體的使用量。
-m 或 --memory 設定記憶體的使用限額,比如 100M 2G
--memory-swap 設定記憶體+swap的使用限額
以上兩個引數預設值為 -1 即不限制
如果只指定了 -m 200M 那麼最多可以使用 200M記憶體 和 200M swap
docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 280M
分配200M 記憶體 和 300M swap
--vm 1 啟動一個記憶體執行緒
--vm-bytes 280M 每個執行緒分配280M記憶體
progrium/stress 會不停的分配記憶體,釋放記憶體
root@docker-lab:~# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 280M
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [7] forked
stress: dbug: [7] allocating 293601280 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 293601280 bytes
stress: dbug: [7] allocating 293601280 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 293601280 bytes
stress: dbug: [7] allocating 293601280 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 293601280 bytes
root@docker-lab:~# docker run -it -m 200M progrium/stress --vm 1 --vm-bytes 410M # 記憶體不足,自動退出
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [8] forked
stress: dbug: [8] allocating 429916160 bytes ...
stress: dbug: [8] touching bytes in strides of 4096 bytes ...
stress: FAIL: [1] (416) <-- worker 8 got signal 9
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (422) kill error: No such process
stress: FAIL: [1] (452) failed run completed in 1s
root@docker-lab:~# docker run -it -m 200M progrium/stress --vm 1 --vm-bytes 390M
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [7] forked
stress: dbug: [7] allocating 408944640 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 408944640 bytes
stress: dbug: [7] allocating 408944640 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 408944640 bytes