Docker的常用命令

112 阅读4分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

帮助命令

 docker version  # 显示docker的版本信息
 ​
 docker info   # 显示docker的系统信息,包括镜像个容器的数量
 ​
 docker 命令 --help  # 帮助命令

帮助文档的地址Reference documentation | Docker Documentation

镜像命令

 docker images #显示已经下载的镜像
 ​
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker images 
 REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
 hello-world   latest    d1165f221234   5 months ago   13.3kB
 REPOSITORY   # 镜像的仓库源
 TAG          # 镜像的标签
 IMAGE ID     # 镜像的ID
 CREATED      # 镜像的创建时间
 SIZE         # 镜像的大小
 ​
 # 可选项
   -a, --all             # 列出所有的镜像
   -q, --quiet           # 只显示镜像的id
 ​

docker search 搜索镜像

 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker search mysql
 NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
 mysql                             MySQL is a widely used, open-source relation…   11289     [OK]       
 mariadb                           MariaDB Server is a high performing open sou…   4284      [OK]      
 # 可选项,通过搜索来过滤
 --filter=STARS=3000 # 搜索出来的镜像收藏数就是大于3000的
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker search mysql  --filter=STARS=3000
 NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
 mysql     MySQL is a widely used, open-source relation…   11289     [OK]       
 mariadb   MariaDB Server is a high performing open sou…   4284      [OK]       
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker search mysql  --filter=STARS=5000
 NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
 mysql     MySQL is a widely used, open-source relation…   11289     [OK] 
 ​

docker pull 下载镜像

 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker pull mysql
 Using default tag: latest  # 如果不写tag,默认是lastest
 latest: Pulling from library/mysql
 33847f680f63: Pull complete # 分层下载,docker image的核心  联合文件系统
 5cb67864e624: Pull complete 
 1a2b594783f5: Pull complete 
 b30e406dd925: Pull complete 
 48901e306e4c: Pull complete 
 603d2b7147fd: Pull complete 
 802aa684c1c4: Pull complete 
 715d3c143a06: Pull complete 
 6978e1b7a511: Pull complete 
 f0d78b0ac1be: Pull complete 
 35a94d251ed1: Pull complete 
 36f75719b1a9: Pull complete 
 Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd   # 签名
 Status: Downloaded newer image for mysql:latest
 docker.io/library/mysql:latest   #真实地址
 
 #下面两个命令等价
 docker pull mysql
 docker io/library/mysql:latest
 
 
 # 指定版本下载
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker pull mysql:5.7
 5.7: Pulling from library/mysql
 33847f680f63: Already exists 
 5cb67864e624: Already exists 
 1a2b594783f5: Already exists 
 b30e406dd925: Already exists 
 48901e306e4c: Already exists 
 603d2b7147fd: Already exists 
 802aa684c1c4: Already exists 
 5b5a19178915: Pull complete 
 f9ce7411c6e4: Pull complete 
 f51f6977d9b2: Pull complete 
 aeb6b16ce012: Pull complete 
 Digest: sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
 Status: Downloaded newer image for mysql:5.7
 docker.io/library/mysql:5.7
 
 

docker rmi 删除镜像

 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker rmi -f 容器id   # 删除指定的容器
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker rmi -f 容器id 容器id 容器id 容器id   # 删除多个指定的容器
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker rmi -f $(docker images -aq)  # 删除所有的容器

容器命令

说明l;:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习

docker pull centos

新建容器并启动

docker run [可选参数] image

 # 参数说明
 --name="Name"   容器名字  tomcat01  tomcat02  用来区分容器
 -d              后台方式运行
 -it             使用交互方式运行  进入容器查看内容
 -p(小写)        指定容器端口  -p  8080:8080
             -p 主机端口:容器端口(常用)
             -p 容器端口
             容器端口
 -P(大写)              随机指定端口
 ​
 ​
 ​
 # 启动并进入容器
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker run -it centos /bin/bash
 [root@96467cf46116 /]# ls # 查看容器内的centos,基础版本,很多命令不完善
 bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
 ​
 # 退出容器回到主机
 [root@96467cf46116 /]# exit
 exit
 ​

列出所有运行的容器

 # docekr ps 当前正在运行的容器
 # docker ps -a 所有运行过的容器 + 正在运行的容器
 # docker ps -n=?  显示最近创建的容器
 # docker ps -aq  只显示容器的编号
 ​
 ​
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker ps -a
 CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
 96467cf46116   centos         "/bin/bash"   7 minutes ago   Exited (0) 5 minutes ago             awesome_taussig
 329f9d6ebe8a   d1165f221234   "/hello"      2 hours ago     Exited (0) 2 hours ago               optimistic_shaw

退出容器

 exit # 直接容器停止并退出
 ctrl+P+Q # 容器不停止退出

删除容器

 docker rm 容器id  #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f
 docker rm -f $(docker ps -aq) # 删除所有的容器

启动和停止容器的操作

 docker start 容器id   # 启动容器
 docker restart 容器id     #重启容器
 docker stop 容器id   # 停止当前正在运行的容器
 docker kill 容器id   # 强制停止当前正在运行的容器

常用的其他命令

后台启动容器

 docker run -d 镜像名
 # 问题:docker ps 发现 centos 停止了
 ​
 #常见的坑:docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
 #nginx 容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

查看日志

 docker logs -f -t --tail 容器,没有日志
 ​
 # 自己编写一段shell脚本
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker run -d centos /bin/sh -c "while true;do echo chuanqi;sleep 1;done"
 ​
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker ps 
 CONTAINER ID   IMAGE     
 ab0e1f3411fc   centos    
 ​
 # 显示日志
 -tf   #显示日志
 --tail number  #要显示的日志条数
 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker logs -tf --tail 10 ab0e1f3411fc
 ​

查看容器中的进程信息

 [root@iZuf623v1zllnm7x4j76fuZ ~]# docker top ab0e1f3411fc 
 UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
 root                14588               14538               0                   19:14               ?                   00:00:00 
 root                15563               14588               0                   19:22               ?                   00:00:00 

查看镜像的元数据

#命令
docker inspect 容器id

[root@iZuf623v1zllnm7x4j76fuZ ~]# docker inspect ab0e1f3411fc