Docker 镜像(image)

在Docker中,一切都基于镜像,镜像本质上是一个文件系统及其参数配置的组合。

通常一个镜像对应一个应用程序,例如:nginx镜像只提供nginx应用,运行中的镜像实例被称为容器(镜像包含了应用程序),一个镜像可以有多个实例。

Docker 程序支持一系列的命令,可通过docker help命令查看。

以如下命令为例:

docker run hello-world 
  • run命令告诉Docker运行某个镜像的一个实例,即hellow-world应用容器,如果镜像不存在,将从Docker hub下载。
  • hello-world指定镜像

让我们看一下运行Busybox镜像的例子:

$ sudo docker run -it --rm busybox
  • docker命令需要在root权限下执行,所以使用sudo
  • busybox是镜像名
  • -it 表示在交互模式下运行
  • --rm 容器停止后自动删除

此条命令将下载并运行busybox镜像,然后进入busybox系统的sh终端。

显示安装的Docker镜像

要查看系统上安装的Docker镜像,执行以下命令。

简单用法

docker images

选项

  • -q – 只输出镜像ID

返回值

打印系统上安装的镜像列表

例子

[root@qikegu ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              64f5d945efcc        10 days ago         1.2MB
php                 7-fpm               d330e525cad6        6 weeks ago         367MB
nginx               latest              2bcb04bdb83f        7 weeks ago         109MB
hello-world         latest              fce289e99eb9        4 months ago        1.84kB
jenkins             latest              cd14cecfdb3a        10 months ago       696MB

如上所示,镜像有以下属性

  • TAG − 镜像的标签,类似版本,可以指定标签安装镜像,每个镜像在docker hub上有详细的标签说明
  • Image ID − 镜像ID,唯一标志镜像
  • Created − 镜像创建时间
  • Virtual Size − 镜像大小

以下例子使用-q参数,只输出镜像ID:

[root@qikegu ~]# docker images -q
64f5d945efcc
502d06d3bfdf
d330e525cad6
2bcb04bdb83f
fce289e99eb9
cd14cecfdb3a

运行Docker镜像

可以使用docker run命令运行镜像,如果没有安装,会先从Docker Hub下载镜像。

简单用法

docker run image 

选项

  • image – 指定镜像名称/ID。

返回值

下载并运行指定镜像。

例子

[root@qikegu ~]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

删除 Docker 镜像

docker rmi命令删除系统上安装的docker镜像。

简单用法

 docker rmi image

选项

  • image – 要删除的镜像(名称或ID)

返回值

删除指定镜像

例子

删除node镜像

[root@qikegu ~]# docker rmi 502d06d3bfdf
Untagged: node:latest
Untagged: node@sha256:3d4411d1933dc93e78b9e0de556a22178269952b98cbbea7ca935d890527ae27
Deleted: sha256:502d06d3bfdf8b91c5b0d6db2f36a6da816a374807cdab01edcd1602ec0e8572
Deleted: sha256:bdd7d668ca8f7724e211e1773eb2ff8c4875a525d31eaf74abe3b9d3c2f70d2a
Deleted: sha256:57d08b934ce43d2861a7a44f4136b244b1bca7ecbe5904ab4d04ddae703aa4af
Deleted: sha256:8d8dabce32d508dfcd126ab43cde7b690764c4bd06800e7cac816d367473ad3e
Deleted: sha256:addff6454b75bcccc23ba0cdbf9ff5d83f6df71523cd3428261d1785c3e3c592
Deleted: sha256:dae4f674403b331cb8c19b2c238ffa7d5a7249a1b4fafc4725054201dce3341a
Deleted: sha256:a9a9c8853295275070975beba78ec0f573172e6e41b30232a00d8af0d49b8ef3
Deleted: sha256:ddf0293e8e23246803d265b158ffbb9453d925fe392b43515984815853e9121b
Deleted: sha256:f94641f1fe1f5c42c325652bf55f0513c881c86b620b912b15460e0bca07cc12

docker inspect

此命令可查看镜像的详细信息。

简单用法

docker inspect image 

选项

  • image – 镜像名称或ID。

返回值

输出镜像的详细信息。

例子

[root@qikegu ~]# docker inspect busybox
[
    {
        "Id": "sha256:64f5d945efcc0f39ab11b3cd4ba403cc9fefe1fa3613123ca016cf3708e8cafb",
        "RepoTags": [
            "busybox:latest"
        ],
        "RepoDigests": [
            "busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-05-09T21:19:58.308508481Z",
        "Container": "8af67ba15d5afe424a7de8f00dba68d886256ff687e8c01b539d79fcc4a5eb31",
        "ContainerConfig": {
            "Hostname": "8af67ba15d5a",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"sh\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:6d548ae6d7d617ec5ce4cd94c61d0df45d5eeeab5d717398736d14437bb0faa5",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "18.06.1-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "sh"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:6d548ae6d7d617ec5ce4cd94c61d0df45d5eeeab5d717398736d14437bb0faa5",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 1199418,
        "VirtualSize": 1199418,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/ca7aa9c486b89ae8b6dce263d7d94702e2597f9e8a873461182                                    8d7c4a480170c/merged",
                "UpperDir": "/var/lib/docker/overlay2/ca7aa9c486b89ae8b6dce263d7d94702e2597f9e8a8734611828                                    d7c4a480170c/diff",
                "WorkDir": "/var/lib/docker/overlay2/ca7aa9c486b89ae8b6dce263d7d94702e2597f9e8a8734611828d                                    7c4a480170c/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:d1156b98822dccbb924b4e5fe16465a7ecac8bfc81d726177bed403a8e70c972"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

命令的用法详情,使用docker help command查看,例如:docker help rmi



浙ICP备17015664号 浙公网安备 33011002012336号 联系我们 网站地图  
@2019 qikegu.com 版权所有,禁止转载