专业编程基础技术教程

网站首页 > 基础教程 正文

docker build stress命令(docker build arg)

ccvgpt 2024-07-19 12:53:53 基础教程 7 ℃

一、简介

stress 可以对linux机器做压力测试,是一个比较好用的命令

二、创建一个ubuntu image

[root@iZ2vceej7yes1abpm7yec2Z ~]# docker run -it ubuntuUnable to find image 'ubuntu:latest' locallylatest: Pulling from library/ubuntu6a5697faee43: Pull complete ba13d3bc422b: Pull complete a254829d9e55: Pull complete Digest: sha256:fff16eea1a8ae92867721d90c59a75652ea66d29c05294e6e2f898704bdb8cf1Status: Downloaded newer image for ubuntu:latest[root@iZ2vceej7yes1abpm7yec2Z ~]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEubuntu              latest              d70eaf7277ea        2 weeks ago         72.9MB

三、在容器中安装stress

由于pull下来的ubuntu是一个基础班,非常的干净,连vim都没有,所以先更换源:

docker build stress命令(docker build arg)

root@76c25bdb1008:/# echo "deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse" >/etc/apt/sources.listroot@76c25bdb1008:/# root@76c25bdb1008:/# more etc/apt/sources.listdeb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverseroot@76c25bdb1008:/# root@76c25bdb1008:/# echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse" >> /etc/apt/sources.listroot@76c25bdb1008:/# root@76c25bdb1008:/# echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse" >> /etc/apt/sources.listroot@76c25bdb1008:/# root@76c25bdb1008:/# echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse" >> /etc/apt/sources.listroot@76c25bdb1008:/# root@76c25bdb1008:/# echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse" >> /etc/apt/sources.listroot@76c25bdb1008:/# root@76c25bdb1008:/# echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse" >> /etc/apt/sources.listroot@76c25bdb1008:/# root@76c25bdb1008:/# echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse" >> /etc/apt/sources.listroot@76c25bdb1008:/# root@76c25bdb1008:/# echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse" >> /etc/apt/sources.listroot@76c25bdb1008:/# root@76c25bdb1008:/# echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse" >> /etc/apt/sources.listroot@76c25bdb1008:/# root@76c25bdb1008:/# echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse" >> /etc/apt/sources.list

apt-get update && apt-get install -y stress

root@76c25bdb1008:/# apt updateGet:1 http://mirrors.aliyun.com/ubuntu xenial InRelease [247 kB]Get:2 http://mirrors.aliyun.com/ubuntu xenial-security InRelease [109 kB]Get:3 http://mirrors.aliyun.com/ubuntu xenial-updates InRelease [109 kB]Get:4 http://mirrors.aliyun.com/ubuntu xenial-proposed InRelease [260 kB]Get:5 http://mirrors.aliyun.com/ubuntu xenial-backports InRelease [107 kB]Get:6 http://mirrors.aliyun.com/ubuntu xenial/universe Sources [9802 kB]Get:7 http://mirrors.aliyun.com/ubuntu xenial/restricted Sources [5179 B]Get:8 http://mirrors.aliyun.com/ubuntu xenial/main Sources [1103 kB]Get:9 http://mirrors.aliyun.com/ubuntu xenial/multiverse Sources [215 kB]Get:10 http://mirrors.aliyun.com/ubuntu xenial/multiverse amd64 Packages [176 kB]Get:11 http://mirrors.aliyun.com/ubuntu xenial/universe amd64 Packages [9827 kB]Get:12 http://mirrors.aliyun.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]Get:13 http://mirrors.aliyun.com/ubuntu xenial/main amd64 Packages [1558 kB]Get:14 http://mirrors.aliyun.com/ubuntu xenial-security/universe Sources [244 kB]Get:15 http://mirrors.aliyun.com/ubuntu xenial-security/multiverse Sources [4952 B]Get:16 http://mirrors.aliyun.com/ubuntu xenial-security/main Sources [299 kB]Get:17 http://mirrors.aliyun.com/ubuntu xenial-security/restricted Sources [3239 B]Get:18 http://mirrors.aliyun.com/ubuntu xenial-security/main amd64 Packages [1841 kB]Get:19 http://mirrors.aliyun.com/ubuntu xenial-security/multiverse amd64 Packages [9249 B]Get:20 http://mirrors.aliyun.com/ubuntu xenial-security/universe amd64 Packages [969 kB]Get:21 http://mirrors.aliyun.com/ubuntu xenial-security/restricted amd64 Packages [15.9 kB]Get:22 http://mirrors.aliyun.com/ubuntu xenial-updates/universe Sources [533 kB]Get:23 http://mirrors.aliyun.com/ubuntu xenial-updates/restricted Sources [3941 B]Get:24 http://mirrors.aliyun.com/ubuntu xenial-updates/multiverse Sources [13.5 kB]Get:25 http://mirrors.aliyun.com/ubuntu xenial-updates/main Sources [655 kB]Get:26 http://mirrors.aliyun.com/ubuntu xenial-updates/universe amd64 Packages [1513 kB]Get:27 http://mirrors.aliyun.com/ubuntu xenial-updates/main amd64 Packages [2353 kB]Get:28 http://mirrors.aliyun.com/ubuntu xenial-updates/restricted amd64 Packages [16.4 kB]Get:29 http://mirrors.aliyun.com/ubuntu xenial-updates/multiverse amd64 Packages [26.7 kB]Get:30 http://mirrors.aliyun.com/ubuntu xenial-proposed/main Sources [9460 B]                                                                                                       Get:31 http://mirrors.aliyun.com/ubuntu xenial-proposed/universe Sources [4365 B]                                                                                                   Get:32 http://mirrors.aliyun.com/ubuntu xenial-proposed/universe amd64 Packages [18.3 kB]                                                                                           Get:33 http://mirrors.aliyun.com/ubuntu xenial-proposed/main amd64 Packages [98.5 kB]                                                                                               Get:34 http://mirrors.aliyun.com/ubuntu xenial-backports/universe Sources [12.3 kB]                                                                                                 Get:35 http://mirrors.aliyun.com/ubuntu xenial-backports/main Sources [7276 B]                                                                                                      Get:36 http://mirrors.aliyun.com/ubuntu xenial-backports/universe amd64 Packages [12.6 kB]                                                                                          Get:37 http://mirrors.aliyun.com/ubuntu xenial-backports/main amd64 Packages [10.9 kB]                                                                                              Fetched 32.2 MB in 7s (4906 kB/s)                                                                                                                                                   Reading package lists... DoneBuilding dependency tree       Reading state information... DoneAll packages are up to date.root@76c25bdb1008:/# root@76c25bdb1008:/# root@76c25bdb1008:/# root@76c25bdb1008:/# root@76c25bdb1008:/# apt-get install -y stressReading package lists... DoneBuilding dependency tree       Reading state information... DoneThe following NEW packages will be installed:  stress0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.Need to get 16.7 kB of archives.After this operation, 75.8 kB of additional disk space will be used.Get:1 http://mirrors.aliyun.com/ubuntu xenial/universe amd64 stress amd64 1.0.4-1 [16.7 kB]Fetched 16.7 kB in 0s (59.7 kB/s) debconf: delaying package configuration, since apt-utils is not installedSelecting previously unselected package stress.(Reading database ... 4121 files and directories currently installed.)Preparing to unpack .../stress_1.0.4-1_amd64.deb ...Unpacking stress (1.0.4-1) ...Setting up stress (1.0.4-1) ...root@76c25bdb1008:/# which stress/usr/bin/stressroot@76c25bdb1008:/# 

stress --help

 stress --help`stress' imposes certain types of compute stress on your system Usage: stress [OPTION [ARG]] ... -?, --help         show this help statement     --version      show version statement -v, --verbose      be verbose -q, --quiet        be quiet -n, --dry-run      show what would have been done -t, --timeout N    timeout after N seconds     --backoff N    wait factor of N microseconds before work starts -c, --cpu N        spawn N workers spinning on sqrt() -i, --io N         spawn N workers spinning on sync() -m, --vm N         spawn N workers spinning on malloc()/free()     --vm-bytes B   malloc B bytes per vm worker (default is 256MB)     --vm-stride B  touch a byte every B bytes (default is 4096)     --vm-hang N    sleep N secs before free (default none, 0 is inf)     --vm-keep      redirty memory instead of freeing and reallocating -d, --hdd N        spawn N workers spinning on write()/unlink()     --hdd-bytes B  write B bytes per hdd worker (default is 1GB)

stress --vm 1 --verbose

启动一个worker ,分配256MB 然后释放 ,一直循环往复

:/# stress --vm 1 --verbosestress: info: [440] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hddstress: dbug: [440] using backoff sleep of 3000usstress: dbug: [440] --> hogvm worker 1 [441] forkedstress: dbug: [441] allocating 268435456 bytes ...stress: dbug: [441] touching bytes in strides of 4096 bytes ...stress: dbug: [441] freed 268435456 bytesstress: dbug: [441] allocating 268435456 bytes ...stress: dbug: [441] touching bytes in strides of 4096 bytes ...stress: dbug: [441] freed 268435456 bytesstress: dbug: [441] allocating 268435456 bytes ...stress: dbug: [441] touching bytes in strides of 4096 bytes ...stress: dbug: [441] freed 268435456 bytesstress: dbug: [441] allocating 268435456 bytes ...

如果分配的内存超过宿主机内存会发生什么?

/# stress --vm 1 --vm-bytes 50000M --verbosestress: info: [442] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hddstress: dbug: [442] using backoff sleep of 3000usstress: dbug: [442] --> hogvm worker 1 [443] forkedstress: dbug: [443] allocating 52428800000 bytes ...stress: FAIL: [443] (494) hogvm malloc failed: Cannot allocate memorystress: FAIL: [442] (394) <-- worker 443 returned error 1stress: WARN: [442] (396) now reaping child worker processesstress: FAIL: [442] (451) failed run completed in 0s

容器 无法分配。

四、编写docker file 然后build一个命令行程序

ubuntu-stress]# more Dockerfile FROM ubuntuRUN apt-get update && apt-get install -y stressENTRYPOINT ["/usr/bin/stress"]CMD []

ubuntu-stress]# docker build -t ninesun0318/ubuntu-stress .

ubuntu-stress]# docker imagesREPOSITORY                  TAG                 IMAGE ID            CREATED              SIZEninesun0318/ubuntu-stress   latest              675f527c4ed1        About a minute ago   99.7MBninesun0318/flask           latest              1f7f63f7ff02        26 hours ago         884MBpython                      3.6                 1297140c6dd2        6 days ago           874MBubuntu                      latest              d70eaf7277ea        2 weeks ago          72.9MBcentos                      latest              0d120b6ccaa8        3 months ago         215MBhello-world                 latest              bf756fb1ae65        10 months ago        13.3kB

通过entrypoint + cmd 是一种流行的运行命令行方式

以一种传参的方式在容器中运行命令

 ubuntu-stress]# docker run -it ninesun0318/ubuntu-stress --vm 1 --verbosestress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hddstress: dbug: [1] using backoff sleep of 3000usstress: dbug: [1] --> hogvm worker 1 [5] forkedstress: dbug: [5] allocating 268435456 bytes ...stress: dbug: [5] touching bytes in strides of 4096 bytes ...stress: dbug: [5] freed 268435456 bytesstress: dbug: [5] allocating 268435456 bytes ...stress: dbug: [5] touching bytes in strides of 4096 bytes ...stress: dbug: [5] freed 268435456 bytesstress: dbug: [5] allocating 268435456 bytes ...stress: dbug: [5] touching bytes in strides of 4096 bytes ...stress: dbug: [5] freed 268435456 bytesstress: dbug: [5] allocating 268435456 bytes ...
 ubuntu-stress]# docker run -it ninesun0318/ubuntu-stress --vm 1 --verbosestress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hddstress: dbug: [1] using backoff sleep of 3000usstress: dbug: [1] --> hogvm worker 1 [5] forkedstress: dbug: [5] allocating 268435456 bytes ...stress: dbug: [5] touching bytes in strides of 4096 bytes ...stress: dbug: [5] freed 268435456 bytesstress: dbug: [5] allocating 268435456 bytes ...stress: dbug: [5] touching bytes in strides of 4096 bytes ...stress: dbug: [5] freed 268435456 bytesstress: dbug: [5] allocating 268435456 bytes ...stress: dbug: [5] touching bytes in strides of 4096 bytes ...stress: dbug: [5] freed 268435456 bytesstress: dbug: [5] allocating 268435456 bytes ...


Tags:

最近发表
标签列表