关于docker:性能环境之docker操作指南3全网最全

5次阅读

共计 11649 个字符,预计需要花费 30 分钟才能阅读完成。

docker run

Usage: docker run [OPTIONS] IMAGE COMMAND

-a

-a,–attach=[]Attach to STDIN, STDOUT or STDERR

如果在执行 run 命令时没有指定 -a,那么 docker 默认会挂载所有规范数据流,包含输入输出和谬误。你能够特地指定挂载哪个规范流。

$ docker run -a stdin -a stdout -i -t ubuntu:14.04/bin/bash
(只挂载规范输入输出)

–add-host

–add-host=[]Add a custom host-to-IP mapping (host:ip)

增加 host-ip 到容器的 hosts 文件

$ docker run -it –add-host db:192.168.1.1 ubuntu:14.04/bin/bash
root@70887853379d:/# cat /etc/hosts
172.17.0.270887853379d
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.1 db

–blkio-weight

–blkio-weight=0Block IO (relative weight), between 10 and 1000

绝对于 CPU 和内存的配额管制,docker 对磁盘 IO 的管制绝对不成熟,大多数都必须在有宿主机设施的状况下应用。次要包含以下参数:

device-read-bps:限度此设施上的读速度(bytes per second),单位能够是 kb、mb 或者 gb.
-device-read-iops:通过每秒读 IO 次数来限度指定设施的读速度。
–device-write-bps:限度此设施上的写速度(bytes per second),单位能够是 kb、mb 或者 gb。
–device-write-iops:通过每秒写 IO 次数来限度指定设施的写速度。
–blkio-weight:容器默认磁盘 IO 的加权值,有效值范畴为 10-100。
–blkio-weight-device:针对特定设施的 IO 加权管制。其格局为 DEVICE_NAME:WEIGHT
存储配额管制的相干参数,能够参考 Red Hat 文档中 blkio 这一章,理解它们的具体作用。
磁盘 IO 配额管制示例
blkio-weight
要使–blkio-weight 失效,须要保障 IO 的调度算法为 CFQ。能够应用上面的形式查看:

root@ubuntu:~# cat /sys/block/sda/queue/scheduler
noop [deadline] cfq

应用上面的命令创立两个–blkio-weight 值不同的容器:

$ docker run -ti –rm –blkio-weight 100 ubuntu:stress
$ docker run -ti –rm –blkio-weight 1000 ubuntu:stress

在容器中同时执行上面的 dd 命令,进行测试:

time dd if=/dev/zero of=test.out bs=1M count=1024 oflag=direct

device-write-bps
应用上面的命令创立容器,并执行命令验证写速度的限度。

$ docker run -tid –name disk1 –device-write-bps /dev/sda:1mb ubuntu:stress

容器空间大小限度
在 docker 应用 devicemapper 作为存储驱动时,默认每个容器和镜像的最大大小为 10G。如果须要调整,能够在 daemon 启动参数中,应用 dm.basesize 来指定,但须要留神的是,批改这个值,不仅仅须要重启 docker daemon 服务,还会导致宿主机上的所有本地镜像和容器都被清理掉。
应用 aufs 或者 overlay 等其余存储驱动时,没有这个限度。
–cidfile=

–cidfile=Write the container ID to the file

将 container ID 保留到 cid_file, 保留的格局为长 UUID

$ docker run -it –cidfile=cid_file ubuntu:14.04/bin/bash

cat cid_file

5fcf835f2688844d1370e6775247c35c9d36d47061c4fc73e328f9ebf920b402

–cpu-shares

–cpu-shares=0 CPU shares (relative weight)

默认状况下,应用 - c 或者 –cpu-shares 参数值为 0,能够赋予以后流动 container 1024 个 cpu 共享周期。这个 0 值能够针对流动的 container 进行批改来调整不同的 cpu 循环周期。
比方,咱们应用 - c 或者 –cpu-shares = 0 启动了 C0,C1,C2 三个 container,应用 -c/–cpu-shares=512 启动了 C3. 这时,C0,C1,C2 能够 100% 的应用 CPU 资源(1024),但 C3 只能应用 50% 的 CPU 资源(512)。如果这个 host 的 OS 是时序调度类型的,每个 CPU 工夫片是 100 微秒,那么 C0,C1,C2 将齐全应用掉这 100 微秒,而 C3 只能应用 50 微秒。
–cpu-period, –cpu-quota

–cpu-period=0Limit CPU CFS (CompletelyFairScheduler) period
–cpu-quota=0Limit CPU CFS (CompletelyFairScheduler) quota

–cpu-period 和 –cpu-quota,这两个参数是相互配合的,–cpu-period 和 –cpu-quota 的这种配置叫 Ceiling Enforcement Tunable Parameters,–cpu-shares 的这种配置叫 Relative Shares Tunable Parameters。–cpu-period 是用来指定容器对 CPU 的应用要在多长时间内做一次重新分配,而 –cpu-quota 是用来指定在这个周期内,最多能够有多少工夫用来跑这个容器。跟 –cpu-shares 不同的是这种配置是指定一个绝对值,而且没有弹性在外面,容器对 CPU 资源的应用相对不会超过配置的值。
比如说 A 容器配置的 –cpu-period=100000 –cpu-quota=50000,那么 A 容器就能够最多应用 50% 个 CPU 资源,如果配置的 –cpu-quota=200000,那就能够应用 200% 个 CPU 资源。
那么有什么样的利用场景呢?简略举个例子,退出对外提供 A 和 B 两个服务,然而 A 的优先级比 B 要高,如果只用 –cpu-shares 来配置,B 服务占用资源太高时是会对 A 有肯定的影响的,然而如果通过 –cpu-period 和 –cpu-quota 来配置,就能起到相对的管制,做到无论 B 怎么样,都不会影响到 A。
cpu-period 和 cpu-quota 的单位为微秒(μs)。cpu-period 的最小值为 1000 微秒,最大值为 1 秒(10^6 μs),默认值为 0.1 秒(100000 μs)。cpu-quota 的值默认为 -1,示意不做管制。
–cpuset-cpus, –cpuset-mems

–cpuset-cpus=CPUsin which to allow execution (0-3,0,1)
–cpuset-mems=MEMsin which to allow execution (0-3,0,1)

对多核 CPU 的服务器,docker 还能够管制容器运行限定应用哪些 cpu 内核和内存节点,即应用–cpuset-cpus 和–cpuset-mems 参数。对具备 NUMA 拓扑(具备多 CPU、多内存节点)的服务器尤其有用,能够对须要高性能计算的容器进行性能最优的配置。如果服务器只有一个内存节点,则–cpuset-mems 的配置基本上不会有显著成果。
应用示例:
命令 docker run -tid –name cpu1 –cpuset-cpus 0-2 ubuntu,示意创立的容器只能用 0、1、2 这三个内核。最终生成的 cgroup 的 cpu 内核配置如下:

cat /sys/fs/cgroup/cpuset/docker/< 容器的残缺长 ID>/cpuset.cpus

0-2

通过 docker exec < 容器 ID> taskset -c -p 1(容器外部第一个过程编号个别为 1),能够看到容器中过程与 CPU 内核的绑定关系,能够认为达到了绑定 CPU 内核的目标。
-d, –detach

-d,–detach=false Run container in background and print container ID

如果在 docker run 前面追加 -d=true 或者 -d,则 containter 将会运行在后盾模式(Detached mode)。此时所有 I / O 数据只能通过网络资源或者共享卷组来进行交互。因为 container 不再监听你执行 docker run 的这个终端命令行窗口。但你能够通过执行 docker attach 来从新挂载这个 container 外面。须要留神的时,如果你抉择执行 - d 使 container 进入后盾模式,那么将无奈配合 ”–rm” 参数。
–device=

–device=[]Add a host device to the container

–disable-content-trust

–disable-content-trust=true Skip image verification

跳过镜像验证。
–dns

–dns=[]Set custom DNS servers

自定义 DNS.

$ docker run -it –dns=8.8.8.8–rm ubuntu:14.04/bin/bash
root@b7a6f0e63e65:/# cat /etc/resolv.conf
nameserver 8.8.8.8

–dns-opt

–dns-opt=[]Set DNS options

–dns-search

–dns-search=[]Set custom DNS search domains

-e, –env

-e,–env=[]Set environment variables

自这义环境变量。
–entrypoint

–entrypoint=Overwrite the default ENTRYPOINT of the image

字面意思是进入点,而它的性能也恰如其意。
An ENTRYPOINT allows you to configure a container that will run as an executable. 它能够让你的容器性能体现得像一个可执行程序一样。
示例一:
应用上面的 ENTRYPOINT 结构镜像:

ENTRYPOINT [“/bin/echo”]

那么 docker build 进去的镜像当前的容器性能就像一个 /bin/echo 程序:
比方我 build 进去的镜像名称叫 imageecho,那么我能够这样用它:

docker run -it imageecho“this is a test”

这里就会输入”this is a test”这串字符,而这个 imageecho 镜像对应的容器体现进去的性能就像一个 echo 程序一样。你增加的参数“this is a test”会增加到 ENTRYPOINT 前面,就成了这样 /bin/echo“this is a test”。
示例二:

ENTRYPOINT [“/bin/cat”]

结构进去的镜像你能够这样运行(假如名为 st):

docker run -it st /etc/fstab

这样相当:/bin/cat /etc/fstab 这个命令的作用。运行之后就输入 /etc/fstab 里的内容。
–env-file

–env-file=[]Readin a file of environment variables

读取设置环境变量的文件.
–expose

–expose=[]Expose a port or a range of ports

通知 Docker 服务端容器裸露的端口号,供互联零碎应用。

$ docker run -it –expose=22–rm ubuntu:14.04/bin/bash

–group-add

–group-add=[]Add additional groups to join

-h, –hostname

-h,–hostname=Container host name

设置容器主机名。

$ docker run -it –hostname=web –rm ubuntu:14.04/bin/bash
进入容器后
root@web:/#

-i, –interactive=false

-i,–interactive=false Keep STDIN open even if not attached

放弃规范输出,常同 - t 一起应用来申请一个控制台进行数据交互。
–ipc

–ipc= IPC namespace to use

IPC(POSIX/SysV IPC)命名空间提供了互相隔离的命名共享内存,信号灯变量和音讯队列。
共享内存能够进步过程数据交互速度。共享内存个别用在 database 和高性能利用 (C/OpenMPI, C++/using boost libraries) 上或者金融服务上。如果须要容器外面部署上述类型的利用,那么就应该在多个容器间接采取共享内存了。
–kernel-memory

–kernel-memory=Kernel memory limit

内核内存,不会被替换到 swap 上。个别状况下,不倡议批改,能够间接参考 docker 的官网文档。
-l, –label

-l,–label=[]Set meta data on a container
–label-file=[]Readin a line delimited file of labels

–link

–link=[]Add link to another container

用于连贯两个容器。
示例: 连贯两个容器
启动容器 1:web

$ docker run –name web -d -p 22-p 80-it webserver:v1

启动容器 2:ap1 连贯到 web,并命名为 apache

$ docker run –name ap1 –link=web:apache -d -p 22-p 80-it webserver:v1

–log-driver

–log-driver=Logging driver for container
–log-opt=[]Log driver options

Docker 减少了对 json-file 型(默认)log driver 的 rotate 性能,咱们可通过 max-size 和 max-file 两个–log-opt 来配置。比方:咱们启动一个 nginx 容器,采纳 json-file 日志引擎,每个 log 文件限度最大为 1k,轮转的日志个数为 5 个:

docker run -d –log-driver=json-file –log-opt max-size=1k–log-opt max-file=5–name webserver -p 9988:80 nginx

有了 rotate,咱们就不用放心某个 container 的日志暴涨而将同 host 的其余 container 拖死了。
–mac-address

–mac-address=Container MAC address

(e.g. 92:d0:c6:0a:29:33)
设置容器的 mac 地址。
-m, –memory

-m,–memory=Memory limit

设置容器应用的最大内存下限。默认单位为 byte,能够应用 K、G、M 等带单位的字符串。
默认状况下,容器能够应用主机上的所有闲暇内存。
设置容器的内存下限,参考命令如下所示:

docker run -tid —name mem1 —memory 128m ubuntu:14.04/bin/bash

默认状况下,除了–memory 指定的内存大小以外,docker 还为容器调配了同样大小的 swap 分区,也就是说,下面的命令创立出的容器实际上最多能够应用 256MB 内存,而不是 128MB 内存。如果须要自定义 swap 分区大小,则能够通过联结应用–memory–swap 参数来实现管制。
对下面的命令创立的容器,能够查看到在 cgroups 的配置文件中,查看到容器的内存大小为 128MB (128×1024×1024=134217728B),内存和 swap 加起来大小为 256MB (256×1024×1024=268435456B)。

cat /sys/fs/cgroup/memory/docker/< 容器的残缺 ID>/memory.limit_in_bytes

134217728

cat /sys/fs/cgroup/memory/docker/< 容器的残缺 ID>/memory.memsw.limit_in_bytes

268435456

留神:执行上述命令时,命令行可能会输入上面的正告:
WARNING: Your kernel does not support swap limit capabilities, memory limited without swap.
这是因为主机上默认不启用 cgroup 来管制 swap 分区,能够参考 docker 官网的相应文档,批改 grub 启动参数。
–memory-reservation

–memory-reservation=Memory soft limit

启用弹性的内存共享,当宿主机资源短缺时,容许容器尽量多地应用内存,当检测到内存竞争或者低内存时,强制将容器的内存升高到 memory-reservation 所指定的内存大小。依照官网说法,不设置此选项时,有可能呈现某些容器长时间占用大量内存,导致性能上的损失。
–memory-swap

–memory-swap=Total memory (memory + swap),’-1′ to disable swap

等于内存和 swap 分区大小的总和,设置为 - 1 时,示意 swap 分区的大小是有限的。默认单位为 byte,能够应用 K、G、M 等带单位的字符串。如果–memory-swap 的设置值小于–memory 的值,则应用默认值,为–memory-swap 值的两倍。
–memory-swappiness

–memory-swappiness=-1Tuning container memory swappiness (0 to 100)

管制过程将物理内存替换到 swap 分区的偏向,默认系数为 60。系数越小,就越偏向于应用物理内存。值范畴为 0 -100。当值为 100 时,示意尽量应用 swap 分区;当值为 0 时,示意禁用容器 swap 性能(这点不同于宿主机,宿主机 swappiness 设置为 0 也不保障 swap 不会被应用)。
–name

–name=Assign a name to the container

为容器指定一个名字。

$ docker run -it –name=web ubuntu:14.04/bin/bash

–net

–net=default Set the Networkfor the container

以下是网络设置中罕用的参数:

none 敞开 container 内的网络连接:
将网络模式设置为 none 时,这个 container 将不容许拜访任何内部 router。这个 container 外部只会有一个 loopback 接口,而且不存在任何能够拜访内部网络的 router。
bridge 通过 veth 接口来连贯 contianer 默认选项:
Docker 默认是将 container 设置为 bridge 模式。此时在 host 下面讲存在一个 docker0 的网络接口,同时会针对 container 创立一对 veth 接口。其中一个 veth 接口是在 host 充当网卡桥接作用,另外一个 veth 接口存在于 container 的命名空间中,并且指向 container 的 loopback。Docker 会主动给这个 container 调配一个 IP,并且将 container 内的数据通过桥接转发到内部。
host 容许 container 应用 host 的网络堆栈信息:
当网络模式设置为 host 时,这个 container 将齐全共享 host 的网络堆栈。host 所有的网络接口将齐全对 container 凋谢。container 的主机名也会存在于 host 的 hostname 中。这时,container 所有对外裸露的 port 和对其它 container 的 link,将齐全生效。
Container:
当网络模式设置为 Container 时,这个 container 将齐全复用另外一个 container 的网络堆栈。同时应用时这个 container 的名称必须要合乎上面的格局:–net container:.
比方以后有一个绑定了本地地址 localhost 的 redis container。如果另外一个 container 须要复用这个网络堆栈,则须要如下操作:
$ docker run -d –name redis example/redis –bind 127.0.0.1

use the redis container’s network stack to access localhost

$ sudo docker run –rm -ti –net container:redis example/redis-cli -h 127.0.0.1

–oom-kill-disable

–oom-kill-disable=false Disable OOM Killer

-P, –publish-all

-P,–publish-all=false Publish all exposed ports to random ports

对外映射所有端口。
-p, –publish

-p,–publish=[]Publish a container’s port(s) to the host

对外映射指定端口,如不指定映射后的端口将随机指定。

$ docker run –d -p 10022:22-p 10080:80-it webserver:v1

应用 docker run 来启动咱们创立的容器。- d 让容器当前台形式运行。应用多个 - p 来映射多个端口,将容器的 22 端口映射为本地的 10022,80 映射为 10080。
–pid

–pid= PID namespace to use

设置容器的 PID 模式。两种:

host: use the host’s PID namespace inside the container.
Note: the host mode gives the container full access to local PID and is therefore considered insecure.

–privileged

–privileged=false Give extended privileges to this container

默认状况下 container 是不能拜访任何其余设施的。然而通过 ”privileged”,container 就领有了拜访任何其余设施的权限。
当操作者执行 docker run –privileged 时,Docker 将领有拜访 host 所有设施的权限

$ docker run -it –rm –privileged ubuntu:14.04/bin/bash

–read-only

–read-only=false Mount the container’s root filesystem as read only

启用后,容器的文件系统将为只读。

$ docker run -it –rm –read-only ubuntu:14.04/bin/bash
root@d793e24f0af1:/# touch a
touch: cannot touch ‘a’:Read-only file system

no,默认策略,在容器退出时不重启容器
on-failure,在容器非正常退出时(退出状态非 0),才会重启容器
on-failure:3,在容器非正常退出时重启容器,最多重启 3 次
always,在容器退出时总是重启容器,当操作系统或 docker 服务重启时,该容器总能随系统启动
unless-stopped,在容器退出时总是重启容器,然而不思考在 Docker 守护过程启动时就曾经进行了的容器
示例:

$ docker run -it –restart=always ubuntu:14.04/bin/bash

–rm

–rm=false Automatically remove the container when it exits

当容器退出时,革除所有该容器的信息。
–security-opt

–security-opt=[]SecurityOptions

平安选项。
–sig-proxy

–sig-proxy=true|false
Proxy received signals to the process (non-TTY mode only). SIGCHLD, SIGSTOP, and SIGKILL are not proxied.The default is true.

–stop-signal

–stop-signal=SIGTERM Signal to stop a container, SIGTERM by default

-t, –tty

-t,–tty=false Allocate a pseudo-TTY

调配一个模仿终端,常和 - i 一块应用.
-u, –user

-u,–user=Username or UID (format:<name|uid>[:<group|gid>])
Sets the username or UID used and optionally the groupname or GID for the specified command.
The followings examples are all valid:
–user [user | user:group | uid | uid:gid | user:gid | uid:group]
Without this argument the command will be run as root in the container.

–ulimit

–ulimit=[]Ulimit options
–default-ulimit,docker daemon 的启动参数,可能指定默认 container ulimit 配置。如果此参数没配置,则默认从 docker daemon 继承;
–ulimit,docker run 的参数,可能笼罩 docker daemon 指定的 ulimit 默认值。如果此参数没配置,则默认从 default-ulimit 继承;

$ docker run -it -d –ulimit nofile=20480:40960 ubuntu:14.04/bin/bash

-v, –volume

-v,–volume=[]Bind mount a volume

能够应用带有 -v 参数的 docker run 命令给容器增加一个数据卷.
1. 增加数据卷 /data1,会主动创立目录

$ docker run -it –name web -v /data1 ubuntu:14.04/bin/bash
root@fac11d44de3e:/# df -h
/dev/disk/by-uuid/1894172f-589b-4e8b-b763-7126991c7fbb29G2.6G25G10%/data1
root@fac11d44de3e:/# cd /data1

2. 将宿主机的目录增加到容器
将宿主机的 /data_web 加载为容器 /data 目录

$ docker run -it –name web -v /data_web:/data ubuntu:14.04/bin/bash

–volumes-from

–volumes-from=[]Mount volumes from the specified container(s)

从其余容器挂载目录。
1. 创立 dbdata 容器,并含有 /data 数据卷

$ docker run -it -v /data –name dbdata ubuntu:14.04/bin/bash

2. 创立 webserver1 挂载 dbdata 的数据卷

$ docker run -it –volumes-from dbdata –name webserver1 ubuntu:14.04/bin/bash

-w, –workdir

-w,–workdir=Working directory inside the container

设置容器的工作目录。

$ docker run -it –workdir=”/data” ubuntu:14.04/bin/bash
root@7868da4d2846:/data#

未完待续 ……

正文完
 0