1、把container放后台运行
[root@localhost dockerfile3]# docker run bigni/python_flask #默认运行image,container里运行的app会放在前台,如果可控制台关闭,则程序也会关闭 * Serving Flask app "app" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) ^[[A^[[A^C[root@localhost dockerfile3]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost dockerfile3]# docker run -d bigni/python_flask #加个d 参数,则会放后台运行 0ff6994da726c86037c5d7cd0eb3ba89c339cdabac5c7df92e112d9c4c417d61 [root@localhost dockerfile3]# [root@localhost dockerfile3]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ff6994da726 bigni/python_flask "python app.py" 4 seconds ago Up 4 seconds 5000/tcp relaxed_banzai [root@localhost dockerfile3]#
2、如何进入正在运行的container里
[root@localhost dockerfile3]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ff6994da726 bigni/python_flask "python app.py" 4 seconds ago Up 4 seconds 5000/tcp relaxed_banzai [root@localhost dockerfile3]# [root@localhost dockerfile3]# [root@localhost dockerfile3]# [root@localhost dockerfile3]# docker exec -it 0ff6994da726 /bin/bash #使用exec 加上-it要进入交互界面, /bin/bash表示要运行的命令 root@0ff6994da726:/app# ls app.py root@0ff6994da726:/app# pwd /app root@0ff6994da726:/app# exit #退出 exit [root@localhost dockerfile3]# docker ps #退出后进程还在后台运行,不影响原来在运行的app CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ff6994da726 bigni/python_flask "python app.py" 3 minutes ago Up 3 minutes 5000/tcp relaxed_banzai [root@localhost dockerfile3]#
3、查看运行中的容器的IP地址
[root@localhost dockerfile3]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ff6994da726 bigni/python_flask "python app.py" 7 minutes ago Up 7 minutes 5000/tcp relaxed_banzai [root@localhost dockerfile3]# docker exec -it 0ff6994da726 ip Usage: ip [ OPTIONS ] OBJECT { COMMAND | help } ip [ -force ] -batch filename where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable | tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm | netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila } OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] | -h[uman-readable] | -iec | -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } | -4 | -6 | -I | -D | -B | -0 | -l[oops] { maximum-addr-flush-attempts } | -br[ief] | -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] | -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]} [root@localhost dockerfile3]# docker exec -it 0ff6994da726 ip address #查看ip地址 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 68: eth0@if69: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever [root@localhost dockerfile3]#
4、停止运行的container 可以使用
[root@localhost dockerfile3]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0ff6994da726 bigni/python_flask "python app.py" 11 minutes ago Up 11 minutes 5000/tcp relaxed_banzai [root@localhost dockerfile3]# docker stop 0f #id可以缩写, 或者使用 docker container stop containerID 0f [root@localhost dockerfile3]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost dockerfile3]#
5、指定container name
PS:name也可以和ID一样,具有唯一性
[root@localhost dockerfile3]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost dockerfile3]# docker run -d bigni/python_flask --name=python_flask a0f19d7ec136c6359cee143d00f4f2619d09381279576c377f4d5fca33c88aed docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"--name=python_flask\": executable file not found in $PATH": unknown. [root@localhost dockerfile3]# docker run -d --name=python_flask bigni/python_flask e9352a7f74a34ef5fd4470e44330496ef215b626db30712fdc62b5e1ec5361e2 [root@localhost dockerfile3]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e9352a7f74a3 bigni/python_flask "python app.py" 5 seconds ago Up 4 seconds 5000/tcp python_flask [root@localhost dockerfile3]#
6、查看docker object low-level information
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
[root@localhost dockerfile3]# docker inspect e9352a7f74a3 [ { "Id": "e9352a7f74a34ef5fd4470e44330496ef215b626db30712fdc62b5e1ec5361e2", "Created": "2019-07-09T13:54:12.711239412Z", "Path": "python", "Args": [ "app.py" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 12966, "ExitCode": 0, "Error": "", "StartedAt": "2019-07-09T13:54:12.984885505Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:9ca7a3d1c78e0e6c5e5871a0fb3d8640cf1a3f340da6a700c265a15c727d36f1", "ResolvConfPath": "/var/lib/docker/containers/e9352a7f74a34ef5fd4470e44330496ef215b626db30712fdc62b5e1ec5361e2/resolv.conf", "HostnamePath": "/var/lib/docker/containers/e9352a7f74a34ef5fd4470e44330496ef215b626db30712fdc62b5e1ec5361e2/hostname", "HostsPath": "/var/lib/docker/containers/e9352a7f74a34ef5fd4470e44330496ef215b626db30712fdc62b5e1ec5361e2/hosts", "LogPath": "/var/lib/docker/containers/e9352a7f74a34ef5fd4470e44330496ef215b626db30712fdc62b5e1ec5361e2/e9352a7f74a34ef5fd4470e44330496ef215b626db30712fdc62b5e1ec5361e2-json.log", "Name": "/python_flask", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "shareable", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/b0fd3dcb14f098298cffdabbd846c50913def554fbedcc9964f6d8340133f444-init/diff:/var/lib/docker/overlay2/03a9b82b2c9e96491837c0a33dd45886adbfa7e2341ba32f3406347920fdb555/diff:/var/lib/docker/overlay2/da3992aa0942e84aaf65255e86b76aa2f33a67d0bd365e703342702678665744/diff:/var/lib/docker/overlay2/97fe6e5072531114ad2c5776bcfc3b8bfb99142415cee98d0b7f99e4f85a3406/diff:/var/lib/docker/overlay2/766fd8fb4b7ab5e6262fc6eaddb1bf6217d36f39c1df003efcc912a2a1ced538/diff:/var/lib/docker/overlay2/4a1b0db496f066aa43883d8ca0a59595a9cdfdb84cc725288aaa21d8c43f3e4e/diff:/var/lib/docker/overlay2/3a95f72acbfdebb697651bbf6faed202b1ddec3ed0f5c2c1a473504ffb92e5a1/diff:/var/lib/docker/overlay2/8c21df782bdedb66d0a1d465911e7d0f1607ca91df6625d1a3226cfbce86b799/diff:/var/lib/docker/overlay2/4c84db527317ca84f629017feb44a390dea6e7447eb1b20fafd02158c782daa2/diff:/var/lib/docker/overlay2/b09af7df97608383eae6fcaa95fc598493fa2ec7d32bf22ad36e63ac6eb2d79a/diff:/var/lib/docker/overlay2/587c552e7b5dc0a9092cf8789ab84c12b2d1fca39bc2a224ecd10c11cd802105/diff:/var/lib/docker/overlay2/c9ab7356f6f29bd2fbfc084c4a7141d634b5092b9242979dbacf8907aaf97aa7/diff", "MergedDir": "/var/lib/docker/overlay2/b0fd3dcb14f098298cffdabbd846c50913def554fbedcc9964f6d8340133f444/merged", "UpperDir": "/var/lib/docker/overlay2/b0fd3dcb14f098298cffdabbd846c50913def554fbedcc9964f6d8340133f444/diff", "WorkDir": "/var/lib/docker/overlay2/b0fd3dcb14f098298cffdabbd846c50913def554fbedcc9964f6d8340133f444/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "e9352a7f74a3", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "5000/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8", "PYTHONIOENCODING=UTF-8", "GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF", "PYTHON_VERSION=2.7.16", "PYTHON_PIP_VERSION=19.1.1" ], "Cmd": [ "python", "app.py" ], "ArgsEscaped": true, "Image": "bigni/python_flask", "Volumes": null, "WorkingDir": "/app", "Entrypoint": null, "OnBuild": null, "Labels": { "author": "BIGNI" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "fa59981a59c7d43a82cff0c4c7549d51223848c845fd4c9792eab3310f1d2cdc", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "5000/tcp": null }, "SandboxKey": "/var/run/docker/netns/fa59981a59c7", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "0b0627742a3459b1f156862b9ba5ee779bb0158457e89cd284c5b68916a6a62f", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "d74eae6754f77ff10150ff7d99c411bc54bb3e35c2feb05eff54f68206a5a22c", "EndpointID": "0b0627742a3459b1f156862b9ba5ee779bb0158457e89cd284c5b68916a6a62f", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] [root@localhost dockerfile3]#
7、使用stress命令对image 内核进行测试
1、进入image交互界面
docker run -it ubuntu
2、安装stress
root@78a8b33fc484:/# apt-get update && apt-get install -y stress
3、stress --help查看参数
root@78a8b33fc484:/# 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 #debug调试 -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) #进程占用内存大小,默认是256MB --vm-stride B touch a byte every B bytes (default is 4096) --vm-hang N sleep N secs before free (default is 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) --hdd-noclean do not unlink files created by hdd workers Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s Note: Numbers may be suffixed with s,m,h,d,y (time) or B,K,M,G (size).
root@78a8b33fc484:/# stress --vm 2 stress: info: [77] dispatching hogs: 0 cpu, 0 io, 2 vm, 0 hdd