docker run
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
-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
--blkio-weight
--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。可以使用下面的方式查看: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
通过dd来验证写速度,输出如下图示:
可以看到容器的写磁盘速度被成功地限制到了1MB/s。device-read-bps等其他磁盘IO限制参数可以使用类似的方式进行验证。
容器空间大小限制
在docker使用devicemapper作为存储驱动时,默认每个容器和镜像的最大大小为10G。如果需要调整,可以在daemon启动参数中,使用dm.basesize来指定,但需要注意的是,修改这个值,不仅仅需要重启docker daemon服务,还会导致宿主机上的所有本地镜像和容器都被清理掉。
使用aufs或者overlay等其他存储驱动时,没有这个限制。
--cap-add,--cap-drop
--cap-add=[] Add Linux capabilities
--cap-drop=[] Drop Linux capabilities
Linux的Capability机制允许你将超级用户相关的高级权限划分成为不同的小单元. 目前Docker容器默认只用到了以下的Capability.
CHOWN, DAC_OVERRIDE, FSETID, FOWNER, MKNOD, NET_RAW, SETGID, SETUID, SETFCAP, SETPCAP, NET_BIND_SERVICE, SYS_CHROOT, KILL, AUDIT_WRITE
有些情况下,你也许需要调整上面罗列的特性。比如你正在构建一个容器,你用它来执行ntpd或是crony,为此它们要能够修改宿主的系统时间。由于不具备CAP_SYS_TIME 特性,容器无法工作。为了应对这种情况,在Docker之前的版本中,容器必须以提权模式运行(使用--privileged 选项),这会禁用所有安全机制。
在Docker的1.3版中,新添了--cap-add和--cap-drop选项。要让一个ntpd容器跑起来,你现在只要执行以下命令:
#docker run -d --cap-add SYS_TIME ntpd
就可以将SYS_TIME 特性添加到你的容器中。
再举个例子,如果你确定你的容器不会改变任何进程的UID和GID,那完全可以将这些
特性从你的容器中移除,这样会更安全:
#docker run --cap-drop SETUID --cap-drop SETGID --cap-drop FOWNER fedora /bin/sh
命令示例,用于查看启用的特性:
# pscap | grep 2912
运行结果示例:
5417 2912 root sh chown, dac_override, fsetid, kill, setpcap, net_bind_service, net_raw, sys_chroot, mknod, audit_write, setfcap
或者你可以先移除所有特性,然后再把一个添加回去:
#docker run --cap-drop ALL --cap-add SYS_TIME ntpd /bin/sh
查看启用的特性:
# pscap | grep 2382
5417 2382 root sh sys_time
--cgroup-parent
--cgroup-parent= Optional parent cgroup for the container
--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=0 Limit CPU CFS (Completely Fair Scheduler) period
--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,表示不做控制。
举个例子,如果容器进程需要每1秒使用单个CPU的0.2秒时间,可以将cpu-period设置为1000000(即1秒),cpu-quota设置为200000(0.2秒)。当然,在多核情况下,如果允许容器进程需要完全占用两个CPU,则可以将cpu-period设置为100000(即0.1秒),cpu-quota设置为200000(0.2秒)。
使用示例:
使用命令docker run -tid –cpu-period 100000 –cpu-quota 200000 ubuntu,创建容器,则最终生成的cgroup的cpu周期配置可以下面的文件中找到:
# cat /sys/fs/cgroup/cpu/docker/<容器的完整长ID>/cpu.cfs_period_us
100000
# cat /sys/fs/cgroup/cpu/docker/<容器的完整长ID>/cpu.cfs_quota_us
200000
--cpuset-cpus, --cpuset-mems
--cpuset-cpus= CPUs in which to allow execution (0-3, 0,1)
--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内核配置如下:
# 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"参数。
# docker run -it -d ubuntu:14.04 /bin/bash
17aebdd365d86c3379aec5ddec364765d467a8d33d7210700a6a758a16206611
--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=[] Read in 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=[] Read in 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
--lxc-conf
--lxc-conf=[] Add custom lxc options
设置lxc配置项。
示例:
docker run --lxc-conf="https://www.sodocs.net/doc/d85596895.html,work.type=veth" --lxc-conf="https://www.sodocs.net/doc/d85596895.html,work.ipv4=192.168.1.10/24" --lxc-conf="https://www.sodocs.net/doc/d85596895.html,work.ipv4.gateway=192.168.1.1" --lxc-conf="https://www.sodocs.net/doc/d85596895.html,work.link=docker0" --lxc-conf="https://www.sodocs.net/doc/d85596895.html,=eth0" --lxc-conf="https://www.sodocs.net/doc/d85596895.html,work.flags=up" -it --net=none ubuntu:14.04 /bin/bash
--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)。
注意:执行上述命令时,命令行可能会输出下面的警告:
WARNING: Your kernel does not support swap limit capabilities, memory limited without swap.这是因为主机上默认不启用cgroup来控制swap分区,可以参考docker官方的相应文档,修改grub启动参数。
在容器中,依次使用下面的stress命令,即可对容器的内存进行压力测试,确认内存。stress –vm 1 –vm-bytes 256M –vm-hang 0 &
stress –vm 1 –vm-bytes 250M –vm-hang 0 &
可以发现,使用256MB进行压力测试时,由于超过了内存上限(128MB内存+128MB swap),进程被OOM杀死。使用250MB进行压力测试时,进程可以正常运行,并且通过docker stats可以查看到容器的内存已经满负载了。
--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=-1 Tuning 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 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需要复用这个网络堆栈,则需要如下操作:
#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
--restart
--restart=no Restart policy to apply when a container exits
当容器退出或宿主机重启的时候,容器接着会重启操作。
重启策略:
no - 不重启
on-failure - container推出状态非0时重启
always - 始终重启
示例:
#docker run -it --restart=always ubuntu:14.04 /bin/bash
当退出容器时,再查看容器的状态为UP
--rm
--rm=false Automatically remove the container when it exits
当容器退出时,清除所有该容器的信息。
--security-opt
--security-opt=[] Security Options
安全选项。
--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:
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
--uts
--uts= UTS namespace to use
-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-7126991c7fbb 29G 2.6G 25G 10% /data1
root@fac11d44de3e:/# cd /data1
2.将宿主机的目录添加到容器
将宿主机的/data_web加载为容器/data目录
# docker run -it --name web -v /data_web:/data ubuntu:14.04 /bin/bash
--volume-driver
--volume-driver= Optional volume driver for the container
--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#