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#
未完待续......