Docker学习笔记(三)——Docker经常使用命令

经常使用命令及其详解

  1. 帮助命令
  • docker version:版本信息
  • docker info:查看docker详细信息
  • docker --help:查看命令帮助信息
  1. 镜像命令
  • docker images:列出本地全部的镜像(含中间印象层)
参数 说明
REPOSITORY 表示镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像ID
CREATED 镜像建立时间
SIZE 镜像大小

同一仓库源能够有多个 TAG,表明这个仓库源的不一样个版本,咱们使用 REPOSITORY:TAG 来定义不一样的镜像。linux

若是你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像web

docker images [OPTIONS]经常使用OPTIONS说明:redis

  • -a:列出本地的全部镜像,包括中间印象层
  • -q:只显示镜像的ID
  • --digests:显示镜像的摘要信息
  • --no-trunc:显示完整的镜像信息
  • docker search [某镜像的名字]:查找某个镜像docker

    注意:即便本地镜像源配置的是阿里云,可是这个search也是去docker hub上去查找,只不过在pull的时候从阿里云上pull。shell

docker search [OPTIONS] [某镜像的名字]经常使用OPTIONS说明:ubuntu

  • -s:列出收藏数(stars)很多于指定值的镜像
  • --automated:只列出automated build类型的镜像
  • --no-trunc:显示完整的镜像信息 注意:在使用-s时,会出现Flag --stars has been deprecated, use --filter=stars=3 instead。也就是说-s已经被启用了,可使用 docker search --filter stars=x [某个镜像的名字]来代替。一样--automated可使用--filter=is-automated=true 来代替。
  • docker pull [镜像名字][:TAG]:下载某个镜像centos

    注意:docker pull tomcat 至关因而docker pull tomcat:latest,不加后面的TAG的话默认是latest tomcat

    pull redis example

  • docker rmi [某个镜像的名字/ID]:删除单个某个镜像bash

    注意:若是守护进程 daemon正在使用该镜像,就会出现如下错误: 服务器

    删除出错
    这种状况下,可使用docker rmi -f [某个镜像的名字/ID]来强制删除。

删除多个:docker rmi -f [镜像名1][:TAG] [镜像名2][:TAG]

删除所有(组合命令):docker rmi -f $(docker images -qa)

说明:$()是shell脚本中的变量返回值的赋值

  • docker commit:提交容器副本,使之成为一个新的镜像

    此命令就是把一个镜像作修改以后使用副本从新提交成一个新的镜像。至关于改装。具体的使用方式为docker commit -m="提交的描述信息" -a="做者" [容器ID] [要建立的目标镜像名]:[标签名]

  1. 容器命令
  • docker run + [镜像名称]

docker run hello-world
根据下面打印的信息,能够看出,运行 docker run首先会在本地containers中找hello-world容器,若是没有,再在本地找hello-world的镜像(images),若是仍是没有,就去远程镜像源(远程仓库)去pull。把镜像pull到本地以后再生产容器去运行。当第二次再运行该命令时,直接运行本地的hello-world容器。
第二次运行
docker run 流程

接下来,运行docker pull centos获取centos的镜像,下面部分所有使用该镜像演示容器命令。

  • docker run [OPTIONS] IMAGE [COMMAND] [ARG...]:新建并启动容器

    若是本地有,就直接启动;若是本地没有,先pull再run。

相关OPTIONS说明:

  • --name="容器新名字": 为容器指定一个名称,至关于别名
  • -d: 后台运行容器,并返回容器ID,也即启动守护式容器
  • -i以交互模式运行容器,一般与 -t 同时使用
  • -t为容器从新分配一个伪输入终端,一般与 -i 同时使用
  • -P: 随机端口映射
  • -p: 指定端口映射,有如下四种格式       ip:hostPort:containerPort       ip::containerPort       hostPort:containerPort       containerPort

命令参数说明: docker run -it centos是启动交互式容器,运行以后能够看到下图中shell的交互由docker变成了centos。

docker 里面启动 centos
docker run -d启动后centos后再用docker ps -a 发现容器已经退出 很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程。容器运行的命令若是不是那些一直挂起的命令(好比运行top,tail),就是会自动退出的。

docker run -d centos

  • docker ps:列出全部当前正在运行的容器

上图没有指定name熟悉,那么docker会随机分配给其一个name,linux下输入ps是查看那些进程在运行,输入docker ps是查看那些容器在运行。

docker ps

相关OPTIONS说明(经常使用):

  • -a:列出当前全部正在运行的容器+历史上运行过
  • -l:显示最近建立的容器
  • -n:显示最近n个建立的容器
  • -q :静默模式,只显示容器编号
  • --no-trunc :不截断输出
  • exit :容器中止退出

    ctrl + P +Q :容器不中止退出 当中止不退出时,使用docker ps能够查看到centos容器正在运行

    中止不退出时centos的状态

  • docker start [容器ID/容器名]:启动容器

  • docker restart [容器ID/容器名]:从新启动容器

  • docker stop[容器ID/容器名]:中止容器(正常关机)

  • docker rm[容器ID]:删除已中止的容器

    使用docker ps -n 3查看运行过的近三个容器:

    查看近期的三个运行的容器
    能够看到centos正在运行,还有其余两个已经中止。下面使用docker rm 删除其中一个已中止的容器:
    删除后再查看近期运行的容器
    能够看到,ID为b78aeba89c42 的容器已经被删除。

  • docker rm -f $(docker ps -a -q):一次性删除多个容器

    docker ps -a -q | xargs docker rm:一次性删除多个容器(Linux管道)

  • docker logs -f -t -tail [tail numbers] [容器ID]:显示容器的日志

相关OPTIONS说明(经常使用):

  • -t:显示时加入时间戳
  • -f:跟随最新的日志打印
  • -tail [numbers]:显示最后的多少条
  • docker top [容器ID]:从查看容器内运行的进程

    docker top示例

  • docker inspect [容器ID]:从查看容器内部细节

  • docker attach [容器ID]:从新进入正在运行的容器中,并以命令行进行交互,配合上面启动容器后ctrl + P + Q退出后从新进入。

    docker exec -it [容器ID] [shell command]:能够不进入容器,直接在宿主机上运行容器里的shell command,并返回结果。

    命令示例

能够看到,在宿主机上执行centos 里面的ls命令,返回centos的文件列表以后交互的shell仍是宿主机。

两种命令的区别: attach是直接进入容器启动终端命令,不会启动新的进程,而exec是在容器中打开新的终端,并能够启动新的进程。也就是说,使用docker attach [容器ID]进入容器以后再使用exit退出,容器就关闭了。而使用docker exec -it [容器ID] /bin/bash以bash的方式进入容器以后使用exit退出关闭的只是一个进程,容器还在启动。

attach示例
exec示例

  • docker cp [容器ID]:[容器内的路径] [目的主机的路径]:从容器中拷贝文件到主机
    docker cp示例

命令汇总(阳哥太贴心啦)

attach :Attach to a running container # 当前 shell 下 attach 链接指定运行镜像

build :Build an image from a Dockerfile # 经过 Dockerfile 定制镜像

commit :Create a new image from a container changes # 提交当前容器为新的镜像

cp : Copy files/folders from the containers filesystem to the host path #从容器中拷贝指定文件或者目录到宿主机中

create :Create a new container # 建立一个新的容器,同 run,但不启动容器

diff :Inspect changes on a container's filesystem # 查看 docker 容器变化

events :Get real time events from the server # 从 docker 服务获取容器实时事件

exec :Run a command in an existing container # 在已存在的容器上运行命令

export :Stream the contents of a container as a tar archive # 导出容器的内容流做为一个 tar 归档文件[对应 import ]

history :Show the history of an image # 展现一个镜像造成历史

images :List images # 列出系统当前镜像

import :Create a new filesystem image from the contents of a tarball # 从tar包中的内容建立一个新的文件系统映像[对应export]

info :Display system-wide information # 显示系统相关信息

inspect :Return low-level information on a container # 查看容器详细信息

kill :Kill a running container # kill 指定 docker 容器

load :Load an image from a tar archive # 从一个 tar 包中加载一个镜像[对应 save]

login :Register or Login to the docker registry server # 注册或者登录一个 docker 源服务器

logout :Log out from a Docker registry server # 从当前 Docker registry 退出

logs :Fetch the logs of a container # 输出当前容器日志信息

port :Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口对应的容器内部源端口

pause :Pause all processes within a container # 暂停容器

ps :List containers # 列出容器列表

pull Pull an image or a repository from the docker registry server # 从docker镜像源服务器拉取指定镜像或者库镜像

push :Push an image or a repository to the docker registry server # 推送指定镜像或者库镜像至docker源服务器

restart Restart a running container # 重启运行的容器

rm :Remove one or more containers # 移除一个或者多个容器

rmi :Remove one or more images # 移除一个或多个镜像[无容器使用该镜像才可删除,不然需删除相关容器才可继续或 -f 强制删除]

run :Run a command in a new container # 建立一个新的容器并运行一个命令

save :Save an image to a tar archive # 保存一个镜像为一个 tar 包[对应 load]

search :Search for an image on the Docker Hub # 在 docker hub 中搜索镜像

start :Start a stopped containers # 启动容器

stop :Stop a running containers # 中止容器

tag :Tag an image into a repository # 给源中镜像打标签

top :Lookup the running processes of a container # 查看容器中运行的进程信息

unpause :Unpause a paused container # 取消暂停容器

version :Show the docker version information # 查看 docker 版本号

wait :Block until a container stops, then print its exit code # 截取容器中止时的退出状态值

相关文章
相关标签/搜索