Chinaunix首页 | 论坛 | 博客
  • 博客访问: 25769
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 320
  • 用 户 组: 普通用户
  • 注册时间: 2021-05-11 17:57
文章分类
文章存档

2021年(31)

我的朋友

分类: LINUX

2021-09-08 10:32:32

docker run

		
  1. Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]    

-a

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

如果在执行run命令时没有指定-a,那么docker默认会挂载所有标准数据流,包括输入输出和错误。你可以特别指定挂载哪个标准流。

		
  1. $ docker run -a stdin -a stdout -i -t ubuntu:14.04 /bin/bash    

  2. (只挂载标准输入输出)

--add-host

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

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

		
  1. $ docker run -it --add-host db:192.168.1.1 ubuntu:14.04 /bin/bash

  2. root@70887853379d:/# cat /etc/hosts      

  3. 172.17.0.2      70887853379d    

  4. 127.0.0.1       localhost    

  5. ::1     localhost ip6-localhost ip6-loopback    

  6. fe00::0 ip6-localnet    

  7. ff00::0 ip6-mcastprefix    

  8. ff02::1 ip6-allnodes    

  9. ff02::2 ip6-allrouters    

  10. 192.168.1.1     db

--blkio-weight

		
  1. --blkio-weight=0    Block 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。可以使用下面的方式查看:

		
  1. root@ubuntu:~# cat /sys/block/sda/queue/scheduler

  2. noop [deadline] cfq    

使用下面的命令创建两个–blkio-weight值不同的容器:

		
  1. $ docker run -ti rm blkio-weight 100 ubuntu:stress

  2. $ docker run -ti rm blkio-weight 1000 ubuntu:stress    

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

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

device-write-bps

使用下面的命令创建容器,并执行命令验证写速度的限制。

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

容器空间大小限制

在docker使用devicemapper作为存储驱动时,默认每个容器和镜像的最大大小为10G。如果需要调整,可以在daemon启动参数中,使用dm.basesize来指定,但需要注意的是,修改这个值,不仅仅需要重启docker daemon服务,还会导致宿主机上的所有本地镜像和容器都被清理掉。
使用aufs或者overlay等其他存储驱动时,没有这个限制。

--cidfile=

		
  1. --cidfile=       Write the container ID to the file    

将container ID保存到cid_file, 保存的格式为长UUID

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

  2. #cat cid_file    

  3. 5fcf835f2688844d1370e6775247c35c9d36d47061c4fc73e328f9ebf920b402    

--cpu-shares

		
  1. --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

		
  1. --cpu-period=0      Limit CPU CFS (Completely Fair Scheduler) period    

  2. --cpu-quota=0       Limit CPU CFS (Completely Fair Scheduler) 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

		
  1. --cpuset-cpus=      CPUs in which to allow execution (0-3, 0,1)    

  2. --cpuset-mems=      MEMs in 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内核配置如下:

		
  1. # cat /sys/fs/cgroup/cpuset/docker/<容器的完整长ID>/cpuset.cpus    

  2. 0-2    

通过docker exec <容器ID> taskset -c -p 1(容器内部第一个进程编号一般为1),可以看到容器中进程与CPU内核的绑定关系,可以认为达到了绑定CPU内核的目的。

-d, --detach

		
  1. -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=

		
  1. --device=[]      Add a host device to the container    

--disable-content-trust

		
  1. --disable-content-trust=true Skip image verification    

跳过镜像验证。

--dns

		
  1. --dns=[]      Set custom DNS servers    

自定义DNS.

		
  1. $ docker run -it --dns=8.8.8.8 --rm ubuntu:14.04 /bin/bash    

  2. root@b7a6f0e63e65:/# cat /etc/resolv.conf      

  3. nameserver 8.8.8.8    

--dns-opt

		
  1. --dns-opt=[]     Set DNS options    

--dns-search

		
  1. --dns-search=[]     Set custom DNS search domains

-e, --env

		
  1. -e, --env=[]     Set environment variables    

自这义环境变量。

--entrypoint

		
  1. --entrypoint=       Overwrite the default ENTRYPOINT of the image    

字面意思是进入点,而它的功能也恰如其意。
An ENTRYPOINT allows you to configure a container that will run as an executable.它可以让你的容器功能表现得像一个可执行程序一样。

示例一:

使用下面的ENTRYPOINT构造镜像:

		
  1. ENTRYPOINT ["/bin/echo"]      

那么docker build出来的镜像以后的容器功能就像一个/bin/echo程序:
比如我build出来的镜像名称叫imageecho,那么我可以这样用它:

		
  1. 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” 。

示例二:

		
  1. ENTRYPOINT ["/bin/cat"]    

构造出来的镜像你可以这样运行(假设名为st):

		
  1. docker run -it st /etc/fstab      

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

--env-file

		
  1. --env-file=[]       Read in a file of environment variables    

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

--expose

		
  1. --expose=[]      Expose a port or a range of ports  

告诉Docker服务端容器暴露的端口号,供互联系统使用。

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

--group-add

		
  1. --group-add=[]      Add additional groups to join    

-h, --hostname

		
  1. -h, --hostname=     Container host name    

设置容器主机名。

		
  1. $ docker run -it --hostname=web --rm ubuntu:14.04 /bin/bash    

  2. 进入容器后  

  3. root@web:/#    

-i, --interactive=false

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

保持标准输入,常同-t一起使用来申请一个控制台进行数据交互。

--ipc

		
  1. --ipc=        IPC namespace to use    

IPC(POSIX/SysV IPC)命名空间提供了相互隔离的命名共享内存,信号灯变量和消息队列。
共享内存可以提高进程数据交互速度。共享内存一般用在database和高性能应用(C/OpenMPI, C++/using boost libraries)上或者金融服务上。如果需要容器里面部署上述类型的应用,那么就应该在多个容器直接采取共享内存了。

--kernel-memory

		
  1. --kernel-memory=    Kernel memory limit    

内核内存,不会被交换到swap上。一般情况下,不建议修改,可以直接参考docker的官方文档。

-l, --label

		
  1. -l, --label=[]      Set meta data on a container    

  2. --label-file=[]     Read in a line delimited file of labels    

--link

		
  1. --link=[]        Add link to another container    

用于连接两个容器。

示例:连接两个容器

启动容器1:web

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

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

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

--log-driver

		
  1. --log-driver=       Logging driver for container    

  2. --log-opt=[]     Log driver options    

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

		
  1. 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

		
  1. --mac-address=      Container MAC address

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

-m, --memory

		
  1. -m, --memory=       Memory limit    

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

		
  1. 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)。

		
  1. #cat /sys/fs/cgroup/memory/docker/<容器的完整ID>/memory.limit_in_bytes

  2. 134217728

  3. #cat /sys/fs/cgroup/memory/docker/<容器的完整ID>/memory.memsw.limit_in_bytes

  4. 268435456  

注意:执行上述命令时,命令行可能会输出下面的警告:
WARNING: Your kernel does not support swap limit capabilities, memory limited without swap.
这是因为主机上默认不启用cgroup来控制swap分区,可以参考docker官方的相应文档,修改grub启动参数。

--memory-reservation

		
  1. --memory-reservation=     Memory soft limit    

启用弹性的内存共享,当宿主机资源充足时,允许容器尽量多地使用内存,当检测到内存竞争或者低内存时,强制将容器的内存降低到memory-reservation所指定的内存大小。按照官方说法,不设置此选项时,有可能出现某些容器长时间占用大量内存,导致性能上的损失。

--memory-swap

		
  1. --memory-swap=      Total memory (memory + swap), '-1' to disable swap    

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

--memory-swappiness

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

控制进程将物理内存交换到swap分区的倾向,默认系数为60。系数越小,就越倾向于使用物理内存。值范围为0-100。当值为100时,表示尽量使用swap分区;当值为0时,表示禁用容器 swap 功能(这点不同于宿主机,宿主机 swappiness 设置为 0 也不保证 swap 不会被使用)。

--name

		
  1. --name=       Assign a name to the container    

为容器指定一个名字。

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

--net

		
  1. --net=default       Set the Network for 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需要复用这个网络堆栈,则需要如下操作:

		
  1. $ docker run -d --name redis example/redis --bind 127.0.0.1    

  2. # use the redis container's network stack to access localhost    

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

--oom-kill-disable

		
  1. --oom-kill-disable=false  Disable OOM Killer    

-P, --publish-all

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

对外映射所有端口。

-p, --publish

		
  1. -p, --publish=[]    Publish a container's port(s) to the host    

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

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

使用docker run来启动我们创建的容器。-d让容器以后台方式运行。使用多个-p来映射多个端口,将容器的22端口映射为本地的10022,80映射为10080。

--pid

		
  1. --pid=        PID namespace to use    

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

		
  1. host: use the host's PID namespace inside the container.    

  2. Note: the host mode gives the container full access to local PID and is therefore considered insecure.    

--privileged

		
  1. --privileged=false     Give extended privileges to this container    

默认情况下container是不能访问任何其他设备的。但是通过"privileged",container就拥有了访问任何其他设备的权限。
当操作者执行docker run --privileged时,Docker将拥有访问host所有设备的权限

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

--read-only

		
  1. --read-only=false      Mount the container's root filesystem as read only    

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

		
  1. $ docker run -it --rm --read-only ubuntu:14.04 /bin/bash    

  2. root@d793e24f0af1:/# touch a    

  3. touch: cannot touch 'a': Read-only file system    

  • no,默认策略,在容器退出时不重启容器

  • on-failure,在容器非正常退出时(退出状态非0),才会重启容器

  • on-failure:3,在容器非正常退出时重启容器,最多重启3次

  • always,在容器退出时总是重启容器,当操作系统或docker服务重启时,该容器总能随系统启动

  • unless-stopped,在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器

示例:

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

--rm

		
  1. --rm=false       Automatically remove the container when it exits    

当容器退出时,清除所有该容器的信息。

--security-opt

		
  1. --security-opt=[]      Security Options    

安全选项。

--sig-proxy

		
  1. --sig-proxy=true|false    

  2. Proxy received signals to the process (non-TTY mode only). SIGCHLD, SIGSTOP, and SIGKILL are not proxied. The default is true.  

--stop-signal

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

-t, --tty

		
  1. -t, --tty=false     Allocate a pseudo-TTY    

分配一个模拟终端,常和-i一块使用.

-u, --user

		
  1. -u, --user=      Username or UID (format: <name|uid>[:<group|gid>])    

  2. Sets the username or UID used and optionally the groupname or GID for the specified command.    

  3. The followings examples are all valid:    

  4. --user [user | user:group | uid | uid:gid | user:gid | uid:group ]    

  5. Without this argument the command will be run as root in the container.    

--ulimit

		
  1. --ulimit=[]      Ulimit options    

  2. --default-ulimitdocker daemon的启动参数,能够指定默认container ulimit配置。如果此参数没配置,则默认从docker daemon继承;    

  3. --ulimitdocker run的参数,能够覆盖docker daemon指定的ulimit默认值。如果此参数没配置,则默认从default-ulimit继承;    

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

-v, --volume

		
  1. -v, --volume=[]     Bind mount a volume    

可以使用带有 -v 参数的 docker run 命令给容器添加一个数据卷.

1.添加数据卷/data1,会自动创建目录

		
  1. $ docker run -it --name web -v /data1 ubuntu:14.04 /bin/bash    

  2. root@fac11d44de3e:/# df -h    

  3. /dev/disk/by-uuid/1894172f-589b-4e8b-b763-7126991c7fbb   29G  2.6G   25G  10% /data1    

  4. root@fac11d44de3e:/# cd /data1

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

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

--volumes-from

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

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

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

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

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

-w, --workdir

		
  1. -w, --workdir=      Working directory inside the container    

设置容器的工作目录。

		
  1. $ docker run -it --workdir="/data" ubuntu:14.04 /bin/bash    

  2. root@7868da4d2846:/data#    


未完待续......

收录于话题 #性能知识集锦
19
上一篇性能环境之docker操作指南2(全网最全)下一篇性能基础之浅谈常见接口性能压测

阅读(641) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~