容器container作为Docker三大基本概念之一,相当于一个系统中运行的软件。一个镜像下可以启动多个容器。通过Docker命令可以查看容器container支持的命令。

docker container --help

命令

等价命令

说明

container attach

attach

将本地标准输入、输出和错误流连接到运行中的容器

container commit

commit

根据当前容器创建一个新的镜像

container cp

cp

将宿主机与容器之间复制文件/文件夹

container create

create

创建一个新的容器

container diff

diff

检查容器文件系统上文件或目录的更改

container exec

exec

进入一个正在运行的容器

container export

export

将容器导出为本地文件

container inspect

查看一个或多个容器详细信息

container kill

kill

杀死一个或多个正在运行的容器

container logs

logs

获取容器日志

container ls

ps

获取容器列表

container pause

pause

暂停一个或多个容器

container unpause

unpause

取消一个或多个暂停的容器

container port

port

列出容器的端口映射或特定映射

container prune

删除全部停止的容器

container rename

rename

重命名一个容器

container restart

restart

重启一个或多个容器

container rm

rm

删除一个或多个容器

container run

run

运行一个新的容器

container start

start

启动一个或多个停止的容器

container stats

stats

显示容器资源使用统计信息的实时流

container stop

stop

停止一个或多个正在运行的容器

container top

top

显示容器运行的进程

container update

update

更新一个或多个容器配置

container wait

wait

等待到一个或多个容器停止,然后打印容器的退出代码

接下来就详细查看每个命令的使用方式。

attach

将本地标准输入、输出和错误流连接到运行中的容器

docker container attach [OPTIONS] CONTAINER 
or
docker attach [OPTIONS] CONTAINER

Options:
      --detach-keys string   Override the key sequence for detaching a container
      --no-stdin             Do not attach STDIN
      --sig-proxy            Proxy all received signals to the process (default true)

使用这个命令需要注意的时,当退出容器时,容器也会跟着停止。为了退出时容器不停止,可以使用参数--no-stdin

commit

根据当前容器创建一个新的镜像

docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
or
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)

这里启动的是一个tomcat容器

CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
ec4b4f0fd45c   4b        "catalina.sh run"   18 minutes ago   Up 18 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg

将这个容器制作一个新的容器

docker commit -a "arvin88" -m "测试commit命令" ec4b4f0fd45c arvin88/tomcat_user:v1
sha256:77fc61f55a0ca2b504db9265c950168bca7045a7a925abf649c1abaa9a311566

查看镜像

REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
arvin88/tomcat_user   v1        77fc61f55a0c   32 seconds ago   590MB

可以看到已经创建成功

注意:如果不指定仓库源:版本,默认创建出来的就是,可以通过tag命令来重新指定仓库源:版本

cp

将宿主机与容器之间复制文件/文件夹

docker container cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
or
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PAT
Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH

将宿主机的test.py文件拷贝到tomcat容器的webapps目录下

docker cp test.py ec4b4f0fd45c:/usr/local/tomcat/webapps

create

创建一个新的容器

docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]
or 
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

参数有点多,这里就不列出来了,有兴趣可以使用--help查看。

docker create -it -p 8089:8080 77fc61f55a0c

创建的新容器,默认是未启动的,可以使用start命令启动。

docker ps -a
CONTAINER ID   IMAGE          COMMAND             CREATED          STATUS          PORTS                                       NAMES
e5e2f9915c42   77fc61f55a0c   "catalina.sh run"   7 seconds ago    Created                                                     frosty_ellis
ec4b4f0fd45c   4b             "catalina.sh run"   37 minutes ago   Up 37 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg
docker start ec
docker ps
CONTAINER ID   IMAGE          COMMAND             CREATED          STATUS          PORTS                                       NAMES
e5e2f9915c42   77fc61f55a0c   "catalina.sh run"   42 seconds ago   Up 2 seconds    0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   frosty_ellis
ec4b4f0fd45c   4b             "catalina.sh run"   38 minutes ago   Up 38 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg

diff

检查容器文件系统上文件或目录的更改

docker container diff CONTAINER
docker diff CONTAINER

这个命令比较简单,没有其他参数。

docker diff e5e2f9915c42

C /usr
C /usr/local
C /usr/local/tomcat
C /usr/local/tomcat/logs
C /usr/local/tomcat/logs/catalina.2021-08-01.log
C /usr/local/tomcat/logs/localhost.2021-08-01.log
C /tmp
C /tmp/hsperfdata_root
C /tmp/hsperfdata_root/1

exec

进入一个正在运行的容器

docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
or
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
      --env-file list        Read in a file of environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
  -w, --workdir string       Working directory inside the container

通常使用-i -t参数,进入容器

docker exec -it e5 /bin/bas
root@e5e2f9915c42:/usr/local/tomcat# ls

看到root后面跟着容器ID,则表示进入到容器了,如果想退出容器,使用exit命令,就可以退出容器。

export

将容器导出为本地文件

docker container export [OPTIONS] CONTAINER
 or
 docker export [OPTIONS] CONTAINER
Options:
  -o, --output string   Write to a file, instead of STDOUT

这个必须要带参数

docker export -o "tomcat_user_v1.gz.tar" e5

输出的文件路径支持绝对路径,如果不填绝对路径,则默认就是在当前执行导出命令的目录下,使用llls查看文件。

-rw-------. 1 root root 588863488 Aug  1 11:51 tomcat_user_v1.gz.tar

从结果上看出已经导出成功。

inspect

查看一个或多个容器详细信息

docker container inspect [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes

命令:docker container inspect e5e2f9915c42,输出的内容太多了,就不贴出来了,可以自行验证查看。

kill

杀死一个或多个正在运行的容器

docker container kill [OPTIONS] CONTAINER [CONTAINER...]
or
docker kill [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -s, --signal string   Signal to send to the container (default "KILL")

命令操作:

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND             CREATED          STATUS          PORTS                                       NAMES
e5e2f9915c42   77fc61f55a0c   "catalina.sh run"   21 minutes ago   Up 20 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   frosty_ellis
ec4b4f0fd45c   4b             "catalina.sh run"   59 minutes ago   Up 59 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg
[root@localhost ~]# docker kill e5
e5
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
ec4b4f0fd45c   4b        "catalina.sh run"   59 minutes ago   Up 59 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg

logs

获取容器日志

docker container logs [OPTIONS] CONTAINER
or 
docker logs [OPTIONS] CONTAINER
Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

启动一个tomcat容器,然后使用命令查看tomcat启动日志。

docker logs 8efed1c23ff5
02-Aug-2021 07:19:06.894 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/8.5.69
02-Aug-2021 07:19:06.895 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jun 30 2021 18:00:00 UTC
02-Aug-2021 07:19:06.895 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.69.0
02-Aug-2021 07:19:06.895 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
02-Aug-2021 07:19:06.895 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.18.0-147.el8.x86_64
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/openjdk-8/jre
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_302-b08
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.30] using APR version [1.6.5].
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
02-Aug-2021 07:19:06.900 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d  10 Sep 2019]
02-Aug-2021 07:19:06.976 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
02-Aug-2021 07:19:06.990 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
02-Aug-2021 07:19:07.001 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 582 ms
02-Aug-2021 07:19:07.030 INFO [main] org.apache.tomcat.util.digester.FactoryCreateRule.begin [FactoryCreateRule] Create exception ignored: java.lang.NullPointerException
02-Aug-2021 07:19:07.034 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
02-Aug-2021 07:19:07.035 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/8.5.69]
02-Aug-2021 07:19:07.045 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/ROOT]
02-Aug-2021 07:19:07.267 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/ROOT] has finished in [221] ms
02-Aug-2021 07:19:07.267 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/docs]
02-Aug-2021 07:19:07.281 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/docs] has finished in [14] ms
02-Aug-2021 07:19:07.281 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/examples]
02-Aug-2021 07:19:07.504 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [223] ms
02-Aug-2021 07:19:07.504 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager]
02-Aug-2021 07:19:07.524 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [19] ms
02-Aug-2021 07:19:07.524 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]
02-Aug-2021 07:19:07.539 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [15] ms
02-Aug-2021 07:19:07.544 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
02-Aug-2021 07:19:07.573 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 571 ms

从结果可以看出日志输出了tomcat的启动日志,如果需要时时查看日志,可以增加参数-f,就可以时时跟踪日志。

ls

获取容器列表

docker container ls [OPTIONS]
or 
docker ps [OPTIONS]

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display container IDs
  -s, --size            Display total file sizes

不加任何参数情况下,列出正在运行的容器:

docker container ls
or 
docker ps
两种命令效果一样
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS                                       NAMES
8efed1c23ff5   4b        "catalina.sh run"   6 minutes ago   Up 6 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

增加-a参数,可以查看所有的容器。

dokcer ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS                       PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   20 seconds ago   Up 17 seconds                0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   7 minutes ago    Exited (143) 3 seconds ago                                               charming_pike

pause

暂停一个或多个容器

docker container pause CONTAINER [CONTAINER...]
or
docker pause CONTAINER [CONTAINER...]

查看容器运行情况

docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS         PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   5 minutes ago    Up 5 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   12 minutes ago   Up 6 seconds   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

暂停上面列出来的容器

docker pause 30c33d778412 8efed1c23ff5
暂停成功输出容器ID
30c33d778412
8efed1c23ff5

查看所有容器

docker ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS                       PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   6 minutes ago    Up 6 minutes (Paused)        0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   13 minutes ago   Up About a minute (Paused)   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

从结果的STATUS列可以看到,后面多了(Paused),表示容器已经暂停。

unpause

取消一个或多个暂停的容器

docker container unpause CONTAINER [CONTAINER...]
or
docker unpause CONTAINER [CONTAINER...]

上面我们已经暂停了两个容器,现在使用unpause取消暂停

docker unpause 30c33d778412 8efed1c23ff5
30c33d778412
8efed1c23ff5

使用ps -a命令查看

CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS         PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   9 minutes ago    Up 9 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   17 minutes ago   Up 4 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

port

列出容器的端口映射或特定映射

docker container port CONTAINER [PRIVATE_PORT[/PROTO]]
or
docker port CONTAINER [PRIVATE_PORT[/PROTO]]

查看容器端口映射

docker container port 30c33d778412
8080/tcp -> 0.0.0.0:8089
8080/tcp -> :::8089

prune

删除全部停止的容器

docker container prune [OPTIONS]
Options:
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation
docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

删除的时候会提示是否继续,如果不想提示可以加-f参数,最终会输出删除结果。

rename

重命名一个容器

docker container rename CONTAINER NEW_NAME
or 
docker rename CONTAINER NEW_NAME

查看容器

CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   23 minutes ago   Up 23 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   30 minutes ago   Up 18 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

将容器30c33d778412重命名

docker rename 30c33d778412 test_name

CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   24 minutes ago   Up 24 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   test_name
8efed1c23ff5   4b        "catalina.sh run"   31 minutes ago   Up 18 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

从结果的NAMES看到已经修改成test_name的容器名了。

restart

重启一个或多个容器

docker container restart [OPTIONS] CONTAINER [CONTAINER...]
or
docker restart [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -t, --time int   Seconds to wait for stop before killing the container (default 10)

重启上面列出的两个容器30c33d778412,8efed1c23ff5

docker restart 30c33d778412 8efed1c23ff5
30c33d778412
8efed1c23ff5

如果重启容器想隔一会再重启,可以增加参数-t,单位:秒。

rm

删除一个或多个容器

docker container rm [OPTIONS] CONTAINER [CONTAINER...]
or
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
  -l, --link      Remove the specified link
  -v, --volumes   Remove anonymous volumes associated with the container

删除容器时,容器必须是未运行的。

docker rm 30c33d778412
Error response from daemon: You cannot remove a running container 30c33d7784121fc46beba6a3097ac9dda9a47e37c5bc6ccc4f2c2b7eb9b5b0cf. Stop the container before attempting removal or force remove

如果实在是想删除,可以添加-f参数,强制删除运行的容器。

run

运行一个新的容器,使用方式与创建容器差不多,参数也差不多。

docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
or
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

运行tomcat容器,后台运行并将端口映射为8087

docker run -d -p 8087:8080 4b6a6e0cf6c1

start

启动一个或多个停止的容器

docker container start [OPTIONS] CONTAINER [CONTAINER...]
or 
docker start [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -a, --attach               Attach STDOUT/STDERR and forward signals
      --detach-keys string   Override the key sequence for detaching a container
  -i, --interactive          Attach container's STDIN

启动容器30c33d778412

docker start 30c33d778412
30c33d778412

如果想看到启动的过程,可以增加-a参数,不过退出启动过程,容器也会跟着停止。

stats

显示容器资源使用统计信息的实时流

docker container stats [OPTIONS] [CONTAINER...]
or
ocker stats [OPTIONS] [CONTAINER...]
Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Pretty-print images using a Go template
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output

如果不跟任何容器,则输出所有容器的实时信息

docker stats
CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT    MEM %     NET I/O       BLOCK I/O   PIDS
30c33d778412   test_name       0.10%     102.7MiB / 3.67GiB   2.73%     866B / 0B     0B / 0B     33
8efed1c23ff5   charming_pike   0.07%     97.89MiB / 3.67GiB   2.61%     1.01kB / 0B   0B / 0B     33

stop

停止一个或多个正在运行的容器

docker container stop [OPTIONS] CONTAINER [CONTAINER...]
docker stop [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -t, --time int   Seconds to wait for stop before killing it (default 10)

从参数上看,停止命令与重启命令一样,可以设置时间。

docker stop 30c33d778412

docker ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS                        PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   46 minutes ago   Exited (143) 13 seconds ago                                               test_name
8efed1c23ff5   4b        "catalina.sh run"   53 minutes ago   Up 18 minutes                 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

从结果看出PORTS列,停止掉的容器已经是空的,表示容器已经停止成功。

update

更新一个或多个容器配置

docker container update [OPTIONS] CONTAINER [CONTAINER...]
or
docker update [OPTIONS] CONTAINER [CONTAINER...]
Options:
      --blkio-weight uint16        Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --cpu-period int             Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int              Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int          Limit the CPU real-time period in microseconds
      --cpu-rt-runtime int         Limit the CPU real-time runtime in microseconds
  -c, --cpu-shares int             CPU shares (relative weight)
      --cpus decimal               Number of CPUs
      --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)
      --kernel-memory bytes        Kernel memory limit
  -m, --memory bytes               Memory limit
      --memory-reservation bytes   Memory soft limit
      --memory-swap bytes          Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --pids-limit int             Tune container pids limit (set -1 for unlimited)
      --restart string             Restart policy to apply when a container exits

从参数上看,可以更新容器的cpu内存配置,需要注意的是,--kernel-memoryDocker 20.10起,该选项已被弃用。

wait

等待到一个或多个容器停止,然后打印容器的退出代码

docker container wait CONTAINER [CONTAINER...]
or
docker wait CONTAINER [CONTAINER...]

如果是一个已经停止的容器,则直接输出退出的代码

docker wait 30c33d778412
143

指定一个正在运行的容器,会一直等待容器停止,然后输出退出代码。

docker wait 8efed1c23ff5

启动另外一个窗口,停止8efed1c23ff5容器,这个时候之前wait命令的窗口就输出143的容器退出代码。