Docke 1 12 基础篇 48条命令 4

Posted firsttry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docke 1 12 基础篇 48条命令 4相关的知识,希望对你有一定的参考价值。

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

container相关的命令

项番命令详解
No.1 attach Attach to a running container
No.3 commit Create a new image from a container’s changes
No.4 cp Copy files/folders between a container and the local filesystem
No.5 create Create a new container
No.7 diff Inspect changes on a container’s filesystem
No.9 exec Run a command in a running container
No.10 export Export a container’s filesystem as a tar archive
No.13 import Import the contents from a tarball to create a filesystem image
No.16 kill Kill one or more running containers
No.23 pause Pause all processes within one or more containers
No.29 rename Rename a container
No.30 restart Restart a container
No.31 rm Remove one or more containers
No.33 run Run a command in a new container
No.38 start Start one or more stopped containers
No.40 stop Stop one or more running containers
No.44 unpause Unpause all processes within one or more containers
No.48 wait Block until a container stops, then print its exit code

状态管理相关的命令

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

项番命令详解
No.5 create Create a new container
No.33 run Run a command in a new container
No.38 start Start one or more stopped containers
No.40 stop Stop one or more running containers
No.30 restart Restart a container
No.44 unpause Unpause all processes within one or more containers
No.23 pause Pause all processes within one or more containers
No.16 kill Kill one or more running containers
No.48 wait Block 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 ~]#
  • 1
  • 2
  • 3

运行一个busybox镜像的container

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

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

[root@liumiaocn ~]# docker run -it busybox /bin/sh
/ # hostname
b847f7213021
/ #
  • 1
  • 2
  • 3
  • 4

使用前面学到的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 ~]#
  • 1
  • 2
  • 3
  • 4

诸如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 value Add a custom host-to-IP mapping (host:ip) (default [])
-a, –attach value Attach to STDIN, STDOUT or STDERR (default [])
–blkio-weight value Block IO (relative weight), between 10 and 1000
–blkio-weight-device value Block IO weight (relative device weight) (default [])
–cap-add value Add Linux capabilities (default [])
–cap-drop value Drop Linux capabilities (default [])
–cgroup-parent string Optional parent cgroup for the container
–cidfile string Write the container ID to the file
–cpu-percent int CPU percent (Windows only)
–cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
–cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
-c, –cpu-shares int CPU shares (relative weight)
–cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
–cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
-d, –detach Run container in background and print container ID
–detach-keys string Override the key sequence for detaching a container
–device value Add a host device to the container (default [])
–device-read-bps value Limit read rate (bytes per second) from a device (default [])
–device-read-iops value Limit read rate (IO per second) from a device (default [])
–device-write-bps value Limit write rate (bytes per second) to a device (default [])
–device-write-iops value Limit write rate (IO per second) to a device (default [])
–disable-content-trust Skip image verification (default true)
–dns value Set custom DNS servers (default [])
–dns-opt value Set DNS options (default [])
–dns-search value Set custom DNS search domains (default [])
–entrypoint string Overwrite the default ENTRYPOINT of the image
-e, –env value Set environment variables (default [])
–env-file value Read in a file of environment variables (default [])
–expose value Expose a port or a range of ports (default [])
–group-add value Add additional groups to join (default [])
–health-cmd string Command to run to check health
–health-interval duration Time between running the check
–health-retries int Consecutive failures needed to report unhealthy
–health-timeout duration Maximum time to allow one check to run
–help Print usage
-h, –hostname string Container host name
-i, –interactive Keep STDIN open even if not attached
–io-maxbandwidth string Maximum IO bandwidth limit for the system drive (Windows only)
–io-maxiops uint Maximum IOps limit for the system drive (Windows only)
–ip string Container IPv4 address (e.g. 172.30.100.104)
–ip6 string Container IPv6 address (e.g. 2001:db8::33)
–ipc string IPC namespace to use
–isolation string Container isolation technology
–kernel-memory string Kernel memory limit
-l, –label value Set meta data on a container (default [])
–label-file value Read in a line delimited file of labels (default [])
–link value Add link to another container (default [])
–link-local-ip value Container IPv4/IPv6 link-local addresses (default [])
–log-driver string Logging driver for the container
–log-opt value Log driver options (default [])
–mac-address string Container MAC address (e.g. 92:d0:c6:0a:29:33)
-m, –memory string Memory limit
–memory-reservation string Memory soft limit
–memory-swap string Swap limit equal to memory plus swap: ‘-1’ to enable unlimited swap
–memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
–name string Assign a name to the container
–network string Connect a container to a network (default “default”)
–network-alias value Add network-scoped alias for the container (default [])
–no-healthcheck Disable any container-specified HEALTHCHECK
–oom-kill-disable Disable OOM Killer
–oom-score-adj int Tune host’s OOM preferences (-1000 to 1000)
–pid string PID namespace to use
–pids-limit int Tune container pids limit (set -1 for unlimited)
–privileged Give extended privileges to this container
-p, –publish value Publish a container’s port(s) to the host (default [])
-P, –publish-all Publish all exposed ports to random ports
–read-only Mount the container’s root filesystem as read only
–restart string Restart policy to apply when a container exits (default “no”)
–rm Automatically remove the container when it exits
–runtime string Runtime to use for this container
–security-opt value Security Options (default [])
–shm-size string Size of /dev/shm, default value is 64MB
–sig-proxy Proxy received signals to the process (default true)
–stop-signal string Signal to stop a container, SIGTERM by default (default “SIGTERM”)
–storage-opt value Storage driver options for the container (default [])
–sysctl value Sysctl options (default map[])
–tmpfs value Mount a tmpfs directory (default [])
-t, –tty Allocate a pseudo-TTY
–ulimit value Ulimit options (default [])
-u, –user string Username or UID (format:
–userns string User namespace to use
–uts string UTS namespace to use
-v, –volume value Bind mount a volume (default [])
–volume-driver string Optional volume driver for the container
–volumes-from value Mount volumes from the specified container(s) (default [])
-w, –workdir string Working directory inside the container

create

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

[[email protected] ~]# 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
[[email protected] ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

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

可以看到此时的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 ~]#
  • 1
  • 2
  • 3
  • 4

start

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

[root@liumiaocn ~]# docker start nginxtest
nginxtest
[root@liumiaocn ~]#
  • 1
  • 2
  • 3

确认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 ~]#
  • 1
  • 2
  • 3
  • 4

确认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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以看到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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以看到状态已经恢复,而且一直在加载中的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
  • 1
  • 2
  • 3
  • 4

执行了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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

[root@liumiaocn ~]# docker wait nginxtest
137
[root@liumiaocn ~]#
  • 1
  • 2
  • 3

结果确认

[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 ~]#
  • 1
  • 2
  • 3
  • 4

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

以上是关于Docke 1 12 基础篇 48条命令 4的主要内容,如果未能解决你的问题,请参考以下文章

Docke 1 12 基础篇 48条命令 3

Docke 1 12 基础篇 48条命令 1

Docke 1 12 基础篇 48条命令 1

Docke--基础篇

docke--Dockerfile构建docker镜像

git基础教程(48)git终结篇大总结