Docker命令-03

Posted

tags:

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

一、Docker常用命令

docker在新版本和老版本的命令有所区别,我们一般混合使用

1.1、常用命令总结

(1)镜像类命令

docker search :搜索镜像 
docker login :登录到registry(仓库)
docker image pull :拉取镜像到本地
docker image push :推送镜像到registry(仓库)
docker image ls :查看本地有多少镜像
docker image rm :删除镜像
docker image build :基于dockerfile构建镜像
docker image inspect :查看镜像详细信息
docker image tag :给镜像打上标签
docker image save :导出镜像
docker image load :导入镜像

(2)容器类命令

docker container run :基于镜像创建并启动一个容器,如果镜像不存在则会使用拉取下来再启动
    -i :交互式与容器
    -t :启动一个虚拟终端
    -d :让容器运行在后台
docker container stop :停止容器
docker container start :启动已经停止的容器
docker container restart :重新启动容器
docker container rm :删除容器
docker container kill :杀死一个容器
docker container exec :对内部容器执行命令
    -i :即使没有附加也保持STDIN 打开
    -t :分配一个伪终端
docker container attach :进入容器内部
docker container ps :查看启动的容器
docker container ps -a :查看所有容器,包括启动停止的容器
docker logs :查看容器内部日志
docker port :查看容器映射的端口
docker top :显示容器进程信息
docker stats :显是容器占用资源信息

二、介绍几个命令具体使用

1.1、镜像类

[[email protected]_134 ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                             Official build of Nginx.                        11488               [OK]
[[email protected]_134 ~]# docker search mariadb
NAME                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mariadb                                MariaDB is a community-developed fork of MyS…   2804                [OK]
[[email protected]_134 ~]# docker search busybox

(2)docker image ls,查看本地镜像
[[email protected]_134 ~]# docker image ls
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
phpdockerio/php56-fpm      latest              8196b837dbcf        5 weeks ago         202MB
nginx                      latest              27a188018e18        6 weeks ago         109MB
richarvey/nginx-php-fpm    latest              49774adafa34        8 weeks ago         334MB
jumpserver/jms_all         latest              40a84d6be0ba        2 months ago        1.33GB
million12/varnish          latest              d4b511cda342        13 months ago       309MB
alivv/elasticsearch-head   latest              1f41026a8812        14 months ago       75.4MB

(3)docker image inspect,查看此镜像的信息
[[email protected]_134 ~]# docker image inspect nginx | head -10
[

"Id": "sha256:53f3fd8007f76bd23bf663ad5f5009c8941f63828ae458cef584b5f85dc0a7bf",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"[email protected]:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68"
],
"Parent": "",

(4)docker image rm,删除镜像                  
[[email protected]_134 ~]# docker image rm nginx
Untagged: nginx:latest
Untagged: [email protected]:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
Deleted: sha256:53f3fd8007f76bd23bf663ad5f5009c8941f63828ae458cef584b5f85dc0a7bf
Deleted: sha256:50183b8336fcc9552a55c86895cdfdfb6f1bb349a951da638f22f645ce235926
Deleted: sha256:093a0ead7cedbef266292a1b08e478489ed6584170f0d82127c5ac9a10fd8303
Deleted: sha256:6270adb5794c6987109e54af00ab456977c5d5cc6f1bc52c1ce58d32ec0f15f4

1.2、容器类

(1)docker container run,创建并启动容器
[[email protected]_134 ~]# docker container run --name "nginx-web2" -d nginx:latest
9d9e155db3199b01930c98c8efe17196eba71f4ff2edcea2b66a7265a87d3c65
[[email protected]_134 ~]# docker run --name "b1" -dit busybox
d2cc7b34daa0d53880c4e8212f3391e1b4a86d7e8584a356230c5d4a362bd127

(2)docker container ps,查看启动的容器
[[email protected]_134 ~]# docker container ps
CONTAINER ID   IMAGE                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
9d9e155db319        nginx:latest               "nginx -g ‘daemon of…"   56 seconds ago      Up 53 seconds       80/tcp                   nginx-web2
313d9d578bf5        alivv/elasticsearch-head   "/bin/sh -c ‘node_mo…"   2 months ago        Up 9 days           0.0.0.0:9100->9100/tcp   es-head

(3)docker container inspect,查看容器详细信息
[[email protected]_134 ~]# docker container inspect  nginx-web2 | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",

(4)docker container exec,docker container  attach ,进入容器内部,nginx容器第一个进程ID为nginx所以需要exec执行/bin/bash解释器进入内部
而b1容器第一个进程为sh进程如果exit直接退出容器就会终止(容器内部程序要工作在前台)
[[email protected]_134 ~]# docker container exec -it nginx-web2 /bin/bash
[email protected]:/# ls /usr/share/nginx/html/
50x.html  index.html
[[email protected]_134 ~]# docker container  attach  b1
/ #
/ #
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ #
/ # ps
PID   USER     TIME  COMMAND
1 root      0:00 sh
7 root      0:00 ps
ctrl + P,ctrl + Q退出

(5)docker container stop,关闭容器
[[email protected]_134 ~]# docker container  stop nginx-web2
nginx-web2

(6)docker container start,启动容器          
[[email protected]_134 ~]# docker container start nginx-web2
nginx-web2

(7)docker container top,查看容器内部进程信息
[[email protected]_134 ~]# docker container top nginx-web2
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                100084              100060              0                   21:13               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 100145              100084              0                   21:13               ?                   00:00:00            nginx: worker process

(8)docker container  stats,查看容器资源信息
[[email protected]_134 ~]# docker container stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
d2cc7b34daa0        b1                  0.00%               56KiB / 15.56GiB      0.00%               648B / 0B           111kB / 594kB       1
9d9e155db319        nginx-web2          0.00%               1.359MiB / 15.56GiB   0.01%               648B / 0B           446kB / 0B          2
313d9d578bf5        es-head             0.00%               12.92MiB / 15.56GiB   0.08%               27.5kB / 687kB      86.9MB / 0B         11

(9)docker container rm,删除容器,需要先停止容器才可删除
[[email protected]_134 ~]# docker container stop nginx-web2
nginx-web2
[[email protected]_134 ~]# docker container rm nginx-web2
nginx-web2

一步一步来!!!!!!!!!!!!!!!!!!!!!!
其实很简单哒!!!!!!!!!!!!!!!!!!!!!

以上是关于Docker命令-03的主要内容,如果未能解决你的问题,请参考以下文章

Docker命令-03

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段——cli的终端命令大全

VSCode自定义代码片段4——cli的终端命令大全

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程