本文针对Docker1.12的48条命令,列出了container相关的18条,划分未必精确。 接下来看看这些条命令都是怎么用的。

container相关的命令

项番命令详解
No.1attachAttach to a running container
No.3commitCreate a new image from a container’s changes
No.4cpCopy files/folders between a container and the local filesystem
No.5createCreate a new container
No.7diffInspect changes on a container’s filesystem
No.9execRun a command in a running container
No.10exportExport a container’s filesystem as a tar archive
No.13importImport the contents from a tarball to create a filesystem image
No.16killKill one or more running containers
No.23pausePause all processes within one or more containers
No.29renameRename a container
No.30restartRestart a container
No.31rmRemove one or more containers
No.33runRun a command in a new container
No.38startStart one or more stopped containers
No.40stopStop one or more running containers
No.44unpauseUnpause all processes within one or more containers
No.48waitBlock until a container stops, then print its exit code

状态管理相关的命令

在上面的18条命令中,抽出9条有关container的状态管理的命令来逐一确认。状态管理是什么,container的生老病死,创建/启动/停止/删除等等。

项番命令详解
No.5createCreate a new container
No.33runRun a command in a new container
No.38startStart one or more stopped containers
No.40stopStop one or more running containers
No.30restartRestart a container
No.44unpauseUnpause all processes within one or more containers
No.23pausePause all processes within one or more containers
No.16killKill one or more running containers
No.48waitBlock until a container stops, then print its exit code

run

docker run可能是刚开始学习docker的除了docker images和docker ps之外用的最多的命令之一了。使用docker run可以使用image启动一个container的实例。

[root@liumiaocn ~]# docker images |grep busybox
busybox                   latest              2b8fd9751c4c        3 months ago        1.093 MB
[root@liumiaocn ~]#

运行一个busybox镜像的container

[root@liumiaocn ~]# docker run -it busybox /bin/sh
/ # hostname
3efaa5538278
/ #

可以使用同一个镜像启动多个不同的container,可牵强地像类和实例的关系。

[root@liumiaocn ~]# docker run -it busybox /bin/sh
/ # hostname
b847f7213021
/ #

使用前面学到的docker ps来确认container状态

[root@liumiaocn ~]# docker ps |grep busybox
b847f7213021        busybox                         "/bin/sh"                2 minutes ago       Up 2 minutes                                                       loving_engelbart
3efaa5538278        busybox                         "/bin/sh"                3 minutes ago       Up 2 minutes                                                       romantic_euclid
[root@liumiaocn ~]#

诸如loving_engelbart是docker自动给container命的名称,通过–name就可以制定想要的名称,-i为交互的方式,-t是分配一个伪终端。dokcer run的option如下,通过简单确认可以看到其出了-i -t这样的option之外,创建container时候对IO/CPU/MEMORY的控制设定等等也都提供了接口,通过一个简单的参数就能实现了,而不再像前面我们在介绍cgroups的时候限制一个CPU要折腾半天了。限于篇幅,又加上docker run或者create在实际使用的时候很多option都会被用到,仅在run的命令时列出option,剩余的请自行docker 命令 –help。

option说明
–add-host valueAdd a custom host-to-IP mapping (host:ip) (default [])
-a, –attach valueAttach to STDIN, STDOUT or STDERR (default [])
–blkio-weight valueBlock IO (relative weight), between 10 and 1000
–blkio-weight-device valueBlock IO weight (relative device weight) (default [])
–cap-add valueAdd Linux capabilities (default [])
–cap-drop valueDrop Linux capabilities (default [])
–cgroup-parent stringOptional parent cgroup for the container
–cidfile stringWrite the container ID to the file
–cpu-percent intCPU percent (Windows only)
–cpu-period intLimit CPU CFS (Completely Fair Scheduler) period
–cpu-quota intLimit CPU CFS (Completely Fair Scheduler) quota
-c, –cpu-shares intCPU shares (relative weight)
–cpuset-cpus stringCPUs in which to allow execution (0-3, 0,1)
–cpuset-mems stringMEMs in which to allow execution (0-3, 0,1)
-d, –detachRun container in background and print container ID
–detach-keys stringOverride the key sequence for detaching a container
–device valueAdd a host device to the container (default [])
–device-read-bps valueLimit read rate (bytes per second) from a device (default [])
–device-read-iops valueLimit read rate (IO per second) from a device (default [])
–device-write-bps valueLimit write rate (bytes per second) to a device (default [])
–device-write-iops valueLimit write rate (IO per second) to a device (default [])
–disable-content-trustSkip image verification (default true)
–dns valueSet custom DNS servers (default [])
–dns-opt valueSet DNS options (default [])
–dns-search valueSet custom DNS search domains (default [])
–entrypoint stringOverwrite the default ENTRYPOINT of the image
-e, –env valueSet environment variables (default [])
–env-file valueRead in a file of environment variables (default [])
–expose valueExpose a port or a range of ports (default [])
–group-add valueAdd additional groups to join (default [])
–health-cmd stringCommand to run to check health
–health-interval durationTime between running the check
–health-retries intConsecutive failures needed to report unhealthy
–health-timeout durationMaximum time to allow one check to run
–helpPrint usage
-h, –hostname stringContainer host name
-i, –interactiveKeep STDIN open even if not attached
–io-maxbandwidth stringMaximum IO bandwidth limit for the system drive (Windows only)
–io-maxiops uintMaximum IOps limit for the system drive (Windows only)
–ip stringContainer IPv4 address (e.g. 172.30.100.104)
–ip6 stringContainer IPv6 address (e.g. 2001:db8::33)
–ipc stringIPC namespace to use
–isolation stringContainer isolation technology
–kernel-memory stringKernel memory limit
-l, –label valueSet meta data on a container (default [])
–label-file valueRead in a line delimited file of labels (default [])
–link valueAdd link to another container (default [])
–link-local-ip valueContainer IPv4/IPv6 link-local addresses (default [])
–log-driver stringLogging driver for the container
–log-opt valueLog driver options (default [])
–mac-address stringContainer MAC address (e.g. 92:d0:c6:0a:29:33)
-m, –memory stringMemory limit
–memory-reservation stringMemory soft limit
–memory-swap stringSwap limit equal to memory plus swap: ‘-1’ to enable unlimited swap
–memory-swappiness intTune container memory swappiness (0 to 100) (default -1)
–name stringAssign a name to the container
–network stringConnect a container to a network (default “default”)
–network-alias valueAdd network-scoped alias for the container (default [])
–no-healthcheckDisable any container-specified HEALTHCHECK
–oom-kill-disableDisable OOM Killer
–oom-score-adj intTune host’s OOM preferences (-1000 to 1000)
–pid stringPID namespace to use
–pids-limit intTune container pids limit (set -1 for unlimited)
–privilegedGive extended privileges to this container
-p, –publish valuePublish a container’s port(s) to the host (default [])
-P, –publish-allPublish all exposed ports to random ports
–read-onlyMount the container’s root filesystem as read only
–restart stringRestart policy to apply when a container exits (default “no”)
–rmAutomatically remove the container when it exits
–runtime stringRuntime to use for this container
–security-opt valueSecurity Options (default [])
–shm-size stringSize of /dev/shm, default value is 64MB
–sig-proxyProxy received signals to the process (default true)
–stop-signal stringSignal to stop a container, SIGTERM by default (default “SIGTERM”)
–storage-opt valueStorage driver options for the container (default [])
–sysctl valueSysctl options (default map[])
–tmpfs valueMount a tmpfs directory (default [])
-t, –ttyAllocate a pseudo-TTY
–ulimit valueUlimit options (default [])
-u, –user stringUsername or UID (format:
–userns stringUser namespace to use
–uts stringUTS namespace to use
-v, –volume valueBind mount a volume (default [])
–volume-driver stringOptional volume driver for the container
–volumes-from valueMount volumes from the specified container(s) (default [])
-w, –workdir stringWorking directory inside the container

create

基本跟run一致。下面我们通过create创建一个nginx的container.下载nginx的镜像。

[root@liumiaocn ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
6a5a5368e0c2: Pull complete
4aceccff346f: Pull complete
c8967f302193: Pull complete
Digest: sha256:1ebfe348d131e9657872de9881fe736612b2e8e1630e0508c354acb0350a4566
Status: Downloaded newer image for nginx:latest
[root@liumiaocn ~]#

使用docker run启动一个名为nginxtest 的nginxcontainer

[root@liumiaocn ~]# docker create --name=nginxtest -p 80:80 nginx
1e56ed7e1b922c225c612617591555757321ca0746a7477fcb665bb6848e2b70
[root@liumiaocn ~]#

可以看到此时的container的状态为created,port因为是运行期间container和host之间的mapping,皮之不存,port焉附,当然也不会有port信息,前面在讲解docker port的时候也说过需要对live的container执行就是这个道理。

[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   38 seconds ago      Created                                 nginxtest
[root@liumiaocn ~]#

start

用于启动停止了或者刚刚创建了的container

[root@liumiaocn ~]# docker start nginxtest
nginxtest
[root@liumiaocn ~]#

确认container结果

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   4 minutes ago       Up 20 seconds       0.0.0.0:80->80/tcp, 443/tcp   nginxtest
[root@liumiaocn ~]#

确认nginx

这里写图片描述

stop

停止容器

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   6 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp, 443/tcp   nginxtest
[root@liumiaocn ~]# docker stop nginxtest
nginxtest
[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   7 minutes ago       Exited (0) 3 seconds ago                       nginxtest
[root@liumiaocn ~]#

restart

再启动容器

[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   10 minutes ago      Exited (0) 3 minutes ago                       nginxtest
[root@liumiaocn ~]# docker restart nginxtest
nginxtest
[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   10 minutes ago      Up 2 seconds        0.0.0.0:80->80/tcp, 443/tcp   nginxtest
[root@liumiaocn ~]#

pause

pause用于将某container暂时停止

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   About an hour ago   Up 50 minutes       0.0.0.0:80->80/tcp, 443/tcp   nginxtest
[root@liumiaocn ~]# docker pause nginxtest
nginxtest
[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS                         NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   About an hour ago   Up 50 minutes (Paused)   0.0.0.0:80->80/tcp, 443/tcp   nginxtest
[root@liumiaocn ~]#

可以看到pause后状态提示为Paused,而且此时通过IE访问nginx会一直处于加载阶段

unpause

pause的反向行为。将暂停的container恢复

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS                         NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   About an hour ago   Up 57 minutes (Paused)   0.0.0.0:80->80/tcp, 443/tcp   nginxtest
[root@liumiaocn ~]# docker unpause nginxtest
nginxtest
[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   About an hour ago   Up 57 minutes       0.0.0.0:80->80/tcp, 443/tcp   nginxtest
[root@liumiaocn ~]#

可以看到状态已经恢复,而且一直在加载中的Nginx页面随着状态的恢复也能重新访问了。

wait 和 kill

wait用于等待某个container的终了,kill用于强制停止某个container

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp, 443/tcp   nginxtest
[root@liumiaocn ~]# docker wait nginxtest

执行了docker wait之后可以看到该进程一直不能返回,然后我们另外新打开一个终端使用docker kill停止这个正在running状态的container

[root@liumiaocn ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp, 443/tcp   nginxtest
[root@liumiaocn ~]# docker kill nginxtest
nginxtest
[root@liumiaocn ~]#

随着nginxtest被kill的同时,一直不能返回的docker wait进程也提示了终了code(137)后退出了。

[root@liumiaocn ~]# docker wait nginxtest
137
[root@liumiaocn ~]#

结果确认

[root@liumiaocn ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
1e56ed7e1b92        nginx               "nginx -g 'daemon off"   About an hour ago   Exited (137) 2 minutes ago                       nginxtest
[root@liumiaocn ~]#
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐