1、帮助命令

1)version 版本

显示docker的版本信息

docker version    #显示docker的版本信息。

查看docker镜像的版本 查看docker镜像大小_查看docker镜像的版本

2)info 信息

显示docker的系统信息,包括镜像和容器的数量

docker info       #显示docker的系统信息,包括镜像和容器的数量

查看docker镜像的版本 查看docker镜像大小_查看docker镜像的版本_02

3)--help 帮助

帮助命令;万能命令

  • 帮助文档的地址:https://docs.docker.com/engine/reference/commandline/
docker 命令 --help #帮助命令

查看docker镜像的版本 查看docker镜像大小_运维_03

查看docker镜像的版本 查看docker镜像大小_运维_04

2、镜像命令

1)docker images 查看镜像

查看所有本地主机上的镜像

#! 解释
#REPOSITORY			# 镜像的仓库源
#TAG				# 镜像的标签
#IMAGE ID			# 镜像的id
#CREATED			# 镜像的创建时间
#SIZE				# 镜像的大小
# 可选项
Options:
  -a, --all             Show all images (default hides intermediate images) #列出所有镜像
  -q, --quiet           Only show numeric IDs # 只显示镜像的id

查看docker镜像的版本 查看docker镜像大小_Docker_05

2)docker search 搜索镜像

#! 搜索 mysql 镜像
docker search mysql

#! 搜索 收藏(stars)超过 300 的 mysql 镜像
docker search mysql --filter=stars=300

查看docker镜像的版本 查看docker镜像大小_docker_06

查看docker镜像的版本 查看docker镜像大小_容器_07

查看docker镜像的版本 查看docker镜像大小_Docker_08

3)docker pull 下载镜像

下载镜像(拉取)

查看docker镜像的版本 查看docker镜像大小_查看docker镜像的版本_09

查看docker镜像的版本 查看docker镜像大小_Docker_10

查看docker镜像的版本 查看docker镜像大小_运维_11

查看docker镜像的版本 查看docker镜像大小_查看docker镜像的版本_12

4)docker rmi 删除镜像

rmi:remove image

#! 删除指定的镜像
docker rmi -f 镜像id

#! 删除多个指定的镜像
docker rmi -f 镜像id 镜像id 镜像id 镜像id

#! 删除全部的镜像
docker rmi -f $(docker images -aq)

查看docker镜像的版本 查看docker镜像大小_容器_13

查看docker镜像的版本 查看docker镜像大小_运维_14

查看docker镜像的版本 查看docker镜像大小_容器_15

3、容器命令

说明:有了镜像才可以创建容器,下载centos镜像

docker pull centos

查看docker镜像的版本 查看docker镜像大小_docker_16

1)docker run 启动运行容器

新建并启动容器;启动的是一个新的容器

docker run [可选参数] image [COMMAND] [ARG...]
#! 常用参数
--name="Name"		容器名字 tomcat01 tomcat02 用来区分容器
-d					后台方式运行
-it 				使用交互方式运行,进入容器查看内容
-p					指定容器的端口 -p 8080(宿主机):8080(容器)
		-p ip:主机端口:容器端口
		-p 主机端口:容器端口(常用)
		-p 容器端口
		容器端口
-P(大写) 				随机指定端口

查看docker镜像的版本 查看docker镜像大小_查看docker镜像的版本_17

查看docker镜像的版本 查看docker镜像大小_查看docker镜像的版本_18

  • 启动并运行容器
#! 启动并交互运行容器; /bin/bash 默认交互窗口,可省略
docker run -it centos /bin/bash

查看docker镜像的版本 查看docker镜像大小_Docker_19

WARNING: IPv4 forwarding is disabled. Networking will not work.解决方法

2)exit 退出容器

退出容器

#! 停止容器并退出
exit

#! 快捷键:退出容器,但不停止
CTRL + P + Q

查看docker镜像的版本 查看docker镜像大小_运维_20

3)docker ps 查看容器

列出所有运行的容器

查看docker镜像的版本 查看docker镜像大小_运维_21

  • 显示当前正在运行的容器
docker ps
  • 显示所有运行的容器(包括已退出运行的容器)
docker ps -a
  • 显示最近运行的n个容器(包括所有状态)
#! 显示最近运行的n个容器(包括所有状态)
docker ps -n=2
  • 显示最近运行的1个容器(包括所有状态)
docker ps -l
  • 只显示容器的ID
#! 所有状态
docker ps -aq

#! 所有状态的最近3个
docker ps -qn=3
  • 显示容器的大小
#! 当前正在运行的容器
docker ps -s

#! 所有状态的容器
docker ps -as

#! 所有状态的最近3个容器
docker ps -sn=3

#! 所有状态的最近3个容器;只有ID,没有大小
docker ps -sqn=3

4)docker rm 删除容器

删除容器

#! 删除指定的容器,不能删除正在运行的容器
#! 如果要强制删除正在运行的容器,用 docker rm -f ID;操作后用 docker ps -a 仍可以查到
docker rm 容器id  

#! 删除所有的容器
docker rm -f $(docker ps -aq)

#! (管道符)删除所有的容器;把管道前面的结果作为参数,传给后面的命令执行
docker ps -aq|xargs docker rm

查看docker镜像的版本 查看docker镜像大小_运维_22

5)docker start 启动容器

启动一个已存在的容器

docker start 容器ID

6)docker restart 重启容器

重启一个已存在的容器

docker restart 容器ID

7)docker stop 停止容器

停止当前正在运行的容器

docker stop 容器ID

8)docker kill 强制停止容器

强制停止当前运行的容器

docker kill 容器ID

4、常用其它命令

1)-d 后台启动

docker run -d 镜像名

查看docker镜像的版本 查看docker镜像大小_运维_23

2)logs 查看日志

docker logs --help
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:37) or relative (e.g. 42m for 42 minutes)
*      --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:37) or relative (e.g. 42m for 42 minutes)
➜  ~ docker run -d centos /bin/sh -c "while true;do echo 6666;sleep 1;done" #模拟日志      
#显示日志
-tf		#显示日志信息(一直更新)
--tail number #需要显示日志条数
docker logs -t --tail n 容器id #查看n行日志
docker logs -ft 容器id #跟着日志

查看docker镜像的版本 查看docker镜像大小_docker_24

3)top 查看容器中进程

查看正在运行的容器中的进程信息

docker top 容器ID

查看docker镜像的版本 查看docker镜像大小_查看docker镜像的版本_25

  • UID:用户ID
  • PID:当前进程ID
  • PPID:父进程ID

4)inspect 查看容器的元数据

inspect

英 [ɪnˈspekt] 美 [ɪnˈspekt]
vt.
检查;查看;审视;视察

# 可以查看所有状态:运行、停止
docker inspect 容器ID

查看docker镜像的版本 查看docker镜像大小_Docker_26

5)exec 进入运行中的容器

方式一:docker exec -it 容器ID 交互窗口;不是运行中的容器不能进入

exec:执行

查看docker镜像的版本 查看docker镜像大小_运维_27

方式二:docker attach 容器id;进入正在运行容器的运行界面

attach:英 [əˈtætʃ] 美 [əˈtætʃ] 附上、贴上

查看docker镜像的版本 查看docker镜像大小_Docker_28

两种方式区别:

  • exec:进入当前容器后开启一个新的终端,可以在里面操作。(常用)
  • attach:进入容器正在执行的终端

6)cp 从容器内拷贝到主机上

把容器内的文件拷贝到外部主机中;不管容器是否运行,只要容器存在即可

cp:copy

docker cp 容器ID:源文件 目标位置

查看docker镜像的版本 查看docker镜像大小_查看docker镜像的版本_29

5、命令总结

查看docker镜像的版本 查看docker镜像大小_容器_30

attach      Attach local standard input, output, and error streams to a running container
  #当前shell下 attach连接指定运行的镜像
  build       Build an image from a Dockerfile # 通过Dockerfile定制镜像
  commit      Create a new image from a container's changes #提交当前容器为新的镜像
  cp          Copy files/folders between a container and the local filesystem #拷贝文件
  create      Create a new container #创建一个新的容器
  diff        Inspect changes to files or directories on a container's filesystem #查看docker容器的变化
  events      Get real time events from the server # 从服务获取容器实时时间
  exec        Run a command in a running container # 在运行中的容器上运行命令
  export      Export a container's filesystem as a tar archive #导出容器文件系统作为一个tar归档文件[对应import]
  history     Show the history of an image # 展示一个镜像形成历史
  images      List images #列出系统当前的镜像
  import      Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
  info        Display system-wide information # 显示全系统信息
  inspect     Return low-level information on Docker objects #查看容器详细信息
  kill        Kill one or more running containers # kill指定docker容器
  load        Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
  login       Log in to a Docker registry #
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes