一、容器的基本使用
1、docker container命令的參數:
[root@master ~]# docker container
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
初次啓動一個nginx容器,使用docker run或docker container run,再來了解一下docker run:
[root@master ~]# docker run
"docker run" requires at least 1 argument.
See 'docker run --help'.
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Docker run參數:
-d,-d=true 在後臺模式(或者直接使用-d)
-a, --attach=[] 連接容器的stdin、stdout、stderr (默認連接這三個流)
-t, --tty=false 使用終端。經常和 -i一起使用。
--sig-proxy=true 代理所有收到的系統信息(我測試了一下好像沒有作用)
-i, --interactive=false 打開STDIN和容器交互。經常和 -t一起使用。
-p 手動指定端口映射,如-p 80:80
-P 系統隨機指定端口映射
比如啓動一個nginx容器:
[root@master ~]# docker run -itd -P --name nginx docker.io/nginx /bin/bash
19f8da31319adae9d183a7ecca7e9c2e4245ebef126fcf37f9715a847cb5c499
停止一個容器:
[root@master ~]# docker stop nginx
啓動一個容器:
[root@master ~]# docker start nginx
重啓一個容器:
[root@master ~]# docker restart nginx
刪除一個容器:
[root@master ~]# docker rm ID
二、資源控制
1、內存限額
與操作系統類似,容器可使用內存分爲物理內存和swap,docker可使用下面兩個參數控制容器的內存:
(1)-m或—memory,設置內存限額,比如100M,2GB;
(2)–memory-swap,設置內存+swap的使用限額,只有在—memory設置時纔有意義;
例:啓動一個容器,內存限額爲200M,內存加上swap一共300M
[root@master ~]# docker run -itd -P --name nginx -m 200M --memory-swap 300M docker.io/nginx
若不添加以上兩組參數,默認資源沒有限制。
如果–memory-swap 設置爲0,則忽略該設置,並將該值視爲未設置;
如果–memory-swap 設置爲與值相同的值–memory,並且–memory設置爲正整數,則容器無權訪問swap。
若只制定–memory,而不指–memory-swap,則–memory-swap大小默認爲–memory的兩倍,如:
[root@master ~]# docker run -itd -P --name nginx -m 200M docker.io/nginx
利用progrium/stress景象進行壓力測試,資源限額200M,內存+swap爲300M,使用一個線程,並且一個線程分配超過300M內存:
[root@master ~]# docker run -it -m 200M --memory-swap 300M progrium/stress --vm 1 --vm-bytes 350M
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 [6] forked
stress: dbug: [6] allocating 367001600 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: FAIL: [1] (416) <-- worker 6 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
可以看到–vm-bytes 350M超過了–memory-swap 300M,所以提示kill error: No such process,進程直接結束了。
2、CPU限額
默認情況下,每個容器對主機CPU週期的訪問權限是不受限制的。
docker 提供了–cpu-period、–cpu-quota兩個參數控制容器可以分配到的 CPU 時鐘週期。 --cpu-period是用來指定容器對 CPU 的使用要在多長時間內做一次重新分配。
–cpu-quota是用來指定在這個週期內,最多可以有多少時間用來跑這個容器。跟 –cpu-shares 不同的是這種配置是指定一個絕對值,而且沒有彈性在裏面,容器對 CPU 資源的使用絕對不會超過配置的值。
使用示例:[ cpu-period 設置爲1000000(即1秒),cpu-quota 設置爲 200000(0.2秒)]
docker run -tid --cpu-period 100000 --cpu-quota 200000 ubuntu
限制CPU個數:
在 docker 1.13 及更高的版本上,能夠很容易的限制容器可以使用的主機 CPU 個數。只需要通過 --cpus 選項指定容器可以使用的 CPU 個數就可以了,並且還可以指定如 1.5 之類的小數。
通過下面的命令創建容器,–cpus=2 表示容器最多可以使用主機上兩個 CPU:
docker run -it --rm --cpus=1.5 ubuntu:16.04 /bin/bash
使用特定0-1的cpu:
docker run -idt --name a1 --cpuset-cpus=0-1 ubuntu:18.04 /bin/bash