在平时的工作中,docker 接触得很多,除了经常使用的 docker run ,docker stop 等命令,docker 还有很多十分有用但是却不经常使用的命令,下面就来总结一下:

docker top

这个命令是用来查看一个容器里面的进程信息的,比如你想查看一个 nginx 容器里面有几个 nginx 进程的时候,就可以这么做:

[[email protected] ~]# docker top fe2b5a947561
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2318                2298                0                   Oct17               ?                   00:00:00            nginx: master process /usr/local/openresty/bin/openresty -g daemon off;
65534               2367                2318                0                   Oct17               ?                   00:00:04            nginx: worker process

docker load && docker save

我一般使用这两个命令去下载打包docker的镜像,因为国内的网速并不像国外那么快。docker save 可以把一个镜像保存到 tar 文件中,你可以这么做:

[[email protected] ~]# docker save alpine:3.12 >alpine:3.12.tar
[[email protected] ~]# ll
total 5724
-rw-r--r--  1 root root 5854208 Oct 19 02:23 alpine:3.12.tar

同时 docker load 可以把镜像从 tar 文件导入到 docker 中

[[email protected] ~]# docker load < alpine:3.12.tar

docker search

这个命令可以帮助你在命令行中方便的搜索 DockerHub 中的镜像,比如:

[[email protected] ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                              Official build of Nginx.                        13880     [OK]       
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1897                 [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   791                  [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   127                  
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   98                   
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   98                   [OK]
bitnami/nginx                      Bitnami nginx Docker Image                      89                   [OK]
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   76                   [OK]
nginxdemos/hello                   NGINX webserver that serves a simple page co…   62                   [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        61                   [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         42                   
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   38                   [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  19                   
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   19                   [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       15                   
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   15                   
staticfloat/nginx-certbot          Opinionated setup for automatic TLS certs lo…   13                   [OK]
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   13                   [OK]
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                   
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                    [OK]
mailu/nginx                        Mailu nginx frontend                            7                    [OK]
flashspys/nginx-static             Super Lightweight Nginx Image                   6                    [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   6                    [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                    [OK]
wodby/nginx                        Generic nginx    

docker events

这个命令可以帮助你实时获取 docker 的各种事件信息,比如创建了一个容器什么的。

# 参数
-f :根据条件过滤事件;
--since :从指定的时间戳后显示所有事件;
--until :流水时间显示到指定的时间为止;

[[email protected] ~]# docker events  --since="1603088994"
2020-10-19T02:30:40.031422094-04:00 container exec_create: /healthcheck.sh  5f5cc61abc206152e2d56a6ffe9429d5eddf846a08ca8db8e78b7de7ee6796f0 (com.docker.compose.config-hash=decd5a094f8090e9da6f072b9b62bf94e850a2ac4480649539e79d74e78938e2, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=ob, com.docker.compose.project.config_files=docker-compose.yml, com.docker.compose.project.working_dir=/data/scripts/ob, com.docker.compose.service=bitwarden, com.docker.compose.version=1.27.4, execID=51cdd01af59c2bd3ef228f15a2e465ebed02f6918a4ff2fd47287d2ad8fe3f3d, image=bitwardenrs/server, name=bitwardenrs)
2020-10-19T02:30:40.031444660-04:00 container exec_start: /healthcheck.sh  5f5cc61abc206152e2d56a6ffe9429d5eddf846a08ca8db8e78b7de7ee6796f0 (com.docker.compose.config-hash=decd5a094f8090e9da6f072b9b62bf94e850a2ac4480649539e79d74e78938e2, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=ob, com.docker.compose.project.config_files=docker-compose.yml, com.docker.compose.project.working_dir=/data/scripts/ob, com.docker.compose.service=bitwarden, com.docker.compose.version=1.27.4, execID=51cdd01af59c2bd3ef228f15a2e465ebed02f6918a4ff2fd47287d2ad8fe3f3d, image=bitwardenrs/server, name=bitwardenrs)
2020-10-19T02:30:40.163816870-04:00 container exec_die 5f5cc61abc206152e2d56a6ffe9429d5eddf846a08ca8db8e78b7de7ee6796f0 (com.docker.compose.config-hash=decd5a094f8090e9da6f072b9b62bf94e850a2ac4480649539e79d74e78938e2, com.docker.compose.container-number=1, com.docker.compose.oneoff=False, com.docker.compose.project=ob, com.docker.compose.project.config_files=docker-compose.yml, com.docker.compose.project.working_dir=/data/scripts/ob, com.docker.compose.service=bitwarden, com.docker.compose.version=1.27.4, execID=51cdd01af59c2bd3ef228f15a2e465ebed02f6918a4ff2fd47287d2ad8fe3f3d, exitCode=0, image=bitwardenrs/server, name=bitwardenrs)

docker update

当你 docker run 了之后却发现里面有一些参数并不是你想要的状态比如你设置的 nginx 容器 cpu 或者内存太小,这个时候你就可以使用 docker update 去修改这些参数。

[[email protected] ~]# docker update ob_nginx_1  --cpus 2

docker history

当你修改了一个镜像,但是忘记了每一层的修改命令,或者你想查看一个镜像是怎么构建的时候就可以使用这个命令,比如:

[[email protected] ~]# docker history e1152e363593
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
e1152e363593   42 hours ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B        
f89149214392   42 hours ago   /bin/sh -c #(nop)  CMD ["/usr/local/openrest…   0B        
b5fd45b3c8ae   42 hours ago   /bin/sh -c #(nop) COPY file:290b28dbe3f8ab14…   1.59kB    
d13cc0fd71da   42 hours ago   /bin/sh -c #(nop) COPY file:476e52e5e1cbf47e…   1.84kB    
1f0fa4277a71   42 hours ago   /bin/sh -c #(nop)  ENV PATH=/usr/local/sbin:…   0B        
7f25882884ce   42 hours ago   |16 RESTY_ADD_PACKAGE_BUILDDEPS= RESTY_ADD_P…   104MB     
9901f3176c82   43 hours ago   /bin/sh -c #(nop)  LABEL resty_eval_post_mak…   0B        
a8deba2782a5   43 hours ago   /bin/sh -c #(nop)  LABEL resty_eval_pre_conf…   0B        
a5ff733dfa4d   43 hours ago   /bin/sh -c #(nop)  LABEL resty_add_package_r…   0B        
85b0786abdcb   43 hours ago   /bin/sh -c #(nop)  LABEL resty_add_package_b…   0B        
be9af64aed24   43 hours ago   /bin/sh -c #(nop)  LABEL resty_config_deps=-…   0B        
845cbb98c926   43 hours ago   /bin/sh -c #(nop)  LABEL resty_config_option…   0B        
de15adc5fa00   43 hours ago   /bin/sh -c #(nop)  LABEL resty_config_option…   0B      

docker wait

这个命令可以查看容器的退出状态,比如:

[[email protected] ~]# docker wait fe2b5a947561

这样你就可以知道这个容器是正常退出的还是异常退出的了。

docker pause && docker unpause

当你运行了一个容器但是想要暂停它运行的时候,你就可以使用这个命令。

[[email protected] ~]# docker pause fe2b5a947561

docker diff

当你运行了一个容器,但是你不知道容器里修改了哪一些文件的时候可以使用这个命令,比如:

[[email protected] ~]# docker diff  fe2b5a947561
C /usr
C /usr/local
C /usr/local/openresty
C /usr/local/openresty/nginx
A /usr/local/openresty/nginx/proxy_temp
A /usr/local/openresty/nginx/proxy_temp/0
A /usr/local/openresty/nginx/proxy_temp/0/01
A /usr/local/openresty/nginx/proxy_temp/0/02
A /usr/local/openresty/nginx/proxy_temp/1
A /usr/local/openresty/nginx/proxy_temp/1/01
A /usr/local/openresty/nginx/proxy_temp/1/02

docker stats

这个是 docker 内置的监控命令,当你想要查看当前主机下所有容器占用内存和 cpu 的情况的时候就可以使用这个命令。

[[email protected] ~]# docker stats
CONTAINER ID   NAME          CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O    PIDS
fe2b5a947561   ob_nginx_1    0.00%     26.41MiB / 991.1MiB   2.66%     69.6MB / 18.9MB   0B / 0B      2

参考资料:民工哥技术之路

文章目录