第六章 Docker---镜像的命令详解

1. 下载与查看镜像:

[root@centos_7_1]:[~]# docker search alpine
NAME                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
alpine                                 A minimal Docker image based on Alpine Linux…   4598                [OK]
mhart/alpine-node                      Minimal Node.js built on Alpine Linux           401
anapsix/alpine-java                    Oracle Java 8 (and 7) with GLIBC 2.28 over A…   368                  [OK]

#-----------------------------分割线,篇幅有限,中间已省略-----------------------------
spotify/alpine                         Alpine image with `bash` and `curl`.            5                  [OK]
functions/alpine                       Alpine Linux / BusyBox with the OpenFaaS wat…   4
govuk/gemstash-alpine                  Gemstash server running on Alpine               3                  [OK]
smartentry/alpine                      alpine with smartentry                          0
#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]# docker image pull alpine:3.8
3.8: Pulling from library/alpine
4fe2ade4980c: Pull complete
Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Status: Downloaded newer image for alpine:3.8
#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              3.8                 196d12cf6ab1        2 months ago        4.41MB
#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]# docker image ls --no-trunc
REPOSITORY          TAG                 IMAGE ID                                                                  CREATED       SIZE
alpine              3.8                 sha256:196d12cf6ab19273823e700516e98eb1910b03b17840f9d5509f03858484d321   2 months ago       4.41MB

上述命令详细说明:

  • docker search alpine
    • 查看互联网远程仓库中的apine所有镜像。
  • docker image ls
    • 列出本地主机镜像
    • Linux中的ls命令通用与Docker容器中
  • docker image pull alpine:3.8
    • 从互联网上的Registry仓库下载Docker镜像,必须确认,已设置Docker镜像加速,如阿里云的镜像加速。
    • alpine:3.8 镜像格式说明:镜像名:标签
  • 显示镜像时,显示各种属性的含义:
    • REPOSITORY 镜像所属仓库
    • TAG 镜像标签
    • IMAGE ID 镜像ID(每个镜像都有一个标识符)
    • CREATED 镜像创建时间
    • SIZE 镜像大小
  • docker image ls --no-trunc
    • 这里显示完整image ID,如不加--no-trunc,则默认显示简写ID。
  • docker image ls --digests
    • 查看 image(镜像)的完整的哈希值:

2. 镜像文件的标签TAG

  • 镜像文件可以不加标签,默认是最新版本,如:
[root@centos_7_1]:[~]# docker image pull alpine
Using default tag: latest   # 不加标签的情况下,默认标签为:latest
latest: Pulling from library/alpine
Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Status: Downloaded newer image for alpine:latest
#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              3.8                 196d12cf6ab1        2 months ago        4.41MB
alpine              latest              196d12cf6ab1        2 months ago        4.41MB
  • 在上述案例中,可以看到镜像的ID(IMAGE ID);
  • 表示这两个镜像文件其实是一个文件;
  • 只是用两个标签显示,而且下载第二个文件时速度特别快;
  • 这代表系统命令执行时,会扫码本地镜像文件,与互联网文件作对比,如果本地存在,只是做加标签处理。
  • 如果下载镜像时不注明标签,则默认下载最新版。
  • 最新版默认标签为:latest。

3. 删除镜像:

[root@centos_7_1]:[~]# docker image rm alpine:3.8
Untagged: alpine:3.8
[root@centos_7_1]:[~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              196d12cf6ab1        2 months ago        4.41MB
#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]# docker image rm alpine:latest
Untagged: alpine:latest
Untagged: alpine@sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Deleted: sha256:196d12cf6ab19273823e700516e98eb1910b03b17840f9d5509f03858484d321
Deleted: sha256:df64d3292fd6194b7865d7326af5255db6d81e9df29f48adde61a918fbd8c332
[root@centos_7_1]:[~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  • 删除镜像时,加标签,则删除相应标签的镜像文件;
  • 如果不加标签,默认删除标签为latest的镜像文件;
  • 如果一个镜像文件有多个标签;
  • 则删除最后一个标签时,才是真正删除了镜像文件,以前的删除只是删除了标签而已。

4. 镜像的详细信息(元数据)

[root@centos_7_1]:[~]# docker image pull alpine:3.8
3.8: Pulling from library/alpine
4fe2ade4980c: Already exists
Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Status: Downloaded newer image for alpine:3.8
#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]#  docker image inspect alpine:3.8
[
    {
        "Id": "sha256:196d12cf6ab19273823e700516e98eb1910b03b17840f9d5509f03858484d321",
        "RepoTags": [
            "alpine:3.8"
        ],
        "RepoDigests": [
            "alpine@sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528"
        ],
#-----------------------------分割线,篇幅有限,中间已省略-----------------------------

上述命令详细说明

  • docker image pull alpine:3.8
    • inspect命令:显示镜像的详细信息(镜像的元数据)。
  • 这里需要特别说明中间的一段代码:
"Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/sh\"]"
            ],

  • 这段代码表示:以这个镜像创建的容器,容器启动时默认启动的应用进程为/bin/sh。

5. Docker镜像命令的所有子命令

#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]# docker image ls --digests
REPOSITORY          TAG                 DIGEST                                                                    IMAGE ID       CREATED             SIZE
alpine              3.8                 sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528   196d12cf6ab1       2 months ago        4.41MB
#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]# docker image ls --help

Usage:  docker image ls [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  ls, images, list

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs
#-----------------------------分割线-----------------------------
[root@centos_7_1]:[~]# docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.
  • docker image ls --help
    • 查看关于docker镜像命令中子命令ls的详细使用方法
  • docker image --help
    • 显示所有Docker镜像的所有子命令

6. Docker新版本与老版本命令对比

[root@centos_7_1]:[~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              3.8                 196d12cf6ab1        2 months ago        4.41MB
[root@centos_7_1]:[~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              3.8                 196d12cf6ab1        2 months ago        4.41MB
  • Docker镜像命令:
    • 老版本:
      • docker images
    • 新版本:
      • docker images
        Docker分类---文章目录:

第一章 初识Docker | 点击此处
第二章 安装Docker以及简单配置 | 点击此处
第三章 Docker容器的生命周期 | 点击此处
第四章 Docker命令汇总 | 点击此处
第五章 Docker基础命令详解 | 点击此处
第六章 Docker---镜像的命令详解 | 点击此处
第七章 Docker---容器的命令详解 | 点击此处
第八章 Docker与Alpine不解之缘 | 点击此处
未完待续

你可能感兴趣的:(第六章 Docker---镜像的命令详解)