Docker命令集锦-镜像操作命令

2、镜像操作命令

2.1、镜像命令汇总

语法:docker image COMMAND
命令:

命令 英文说明&中文说明
build Build an image from a Dockerfile
从Dockerfile构建一个映像
history Show the history of an image
显示镜像的构建历史
import Import the contents from a tarball to create a filesystem image
从tar包导入内容以创建文件系统映像
inspect Display detailed information on one or more images
显示一个或多个镜像的详细信息
load Load an image from a tar archive or STDIN
从tar存档或STDIN加载镜像
ls List images
镜像列表
prune Remove unused images
删除未使用的镜像
pull Pull an image or a repository from a registry
从registry中提取镜像或存储库
push Push an image or a repository to a registry
将镜像或存储库推入registry
rm Remove one or more images
移除一个或多个镜像
save Save one or more images to a tar archive (streamed to STDOUT by default)
将一个或多个镜像保存到tar存档(默认情况下流到STDOUT)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
创建一个引用源镜像的目标镜像标记
2.2、拉取镜像命令

描述(英文):Pull an image or a repository from a registry
描述(中文):从registry中提取镜像或存储库
语法:docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
选项:

选项 英文说明&中文说明
-a, --all-tags Download all tagged images in the repository
下载存储库中所有标记的镜像
--disable-content-trust Skip image verification (default true)
跳过镜像验证(默认为真)
-q, --quiet Suppress verbose output
抑制详细输出
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@Server ~]# docker image pull centos:8.1.1911
8.1.1911: Pulling from library/centos
8a29a15cefae: Pull complete
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:8.1.1911
docker.io/library/centos:8.1.1911
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              8.1.1911            470671670cac        3 months ago        237MB
2.3、查看镜像列表命令

描述(英文):List images
描述(中文):镜像列表
语法:docker image ls [OPTIONS] [REPOSITORY[:TAG]]
别名:ls, images, list
选项:

选项 英文说明&中文说明
-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
使用Go模板打印漂亮的镜像
--no-trunc Don't truncate output
不截断输出
-q, --quiet Only show numeric IDs
只显示数字id
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              8.1.1911            470671670cac        3 months ago        237MB
centos              7.7.1908            08d05d1d5859        5 months ago        204MB
[root@Server ~]# docker image ls -a -q
470671670cac
08d05d1d5859
[root@Server ~]# docker image ls -a --digests
REPOSITORY          TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE
centos              8.1.1911            sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700   470671670cac        3 months ago        237MB
centos              7.7.1908            sha256:50752af5182c6cd5518e3e91d48f7ff0cba93d5d760a67ac140e2d63c4dd9efc   08d05d1d5859        5 months ago        204MB
[root@Server ~]# docker image ls -a --no-trunc
REPOSITORY          TAG                 IMAGE ID                                                                  CREATED             SIZE
centos              8.1.1911            sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee   3 months ago        237MB
centos              7.7.1908            sha256:08d05d1d5859ebcfb3312d246e2082e46cb307f0e896c9ac097185f0b0b19e56   5 months ago        204MB
2.4、查看镜像构建历史

描述(英文):Show the history of an image
描述(中文):显示镜像的构建历史
语法:docker image history [OPTIONS] IMAGE
选项:

选项 英文说明&中文说明
--format string Pretty-print images using a Go template
使用Go模板打印漂亮的镜像
-H, --human Print sizes and dates in human readable format (default true)
以人类可读格式打印大小和日期(默认为true)
--no-trunc Don't truncate output
不截断输出
-q, --quiet Only show numeric IDs
只显示数字id
[root@Server ~]# docker image history centos:7.7.1908
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
08d05d1d5859        5 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
           5 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B
           5 months ago        /bin/sh -c #(nop) ADD file:3e2a127b44ed01afc…   204MB
[root@Server ~]# docker image history --no-trunc centos:7.7.1908
IMAGE                                                                     CREATED             CREATED BY                                                                                                                                                                                                SIZE                COMMENT
sha256:08d05d1d5859ebcfb3312d246e2082e46cb307f0e896c9ac097185f0b0b19e56   5 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]                                                                                                                                                                      0B
                                                                 5 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.schema-version=1.0 org.label-schema.name=CentOS Base Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20191024   0B
                                                                 5 months ago        /bin/sh -c #(nop) ADD file:3e2a127b44ed01afc6e3ee70b4bdaff81d5d9df67a07740debb5c9e7760cd15b in /                                                                                                          204MB
2.5、导出镜像命令

描述(英文):Save one or more images to a tar archive (streamed to STDOUT by default)
描述(中文):将一个或多个镜像保存到tar存档(默认情况下流到STDOUT)
语法:docker image save [OPTIONS] IMAGE [IMAGE...]
选项:

选项 英文说明&中文说明
-o, --output string Write to a file, instead of STDOUT
写入文件,而不是STDOUT
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              8.1.1911            470671670cac        3 months ago        237MB
centos              7.7.1908            08d05d1d5859        5 months ago        204MB
[root@Server ~]# ls -l /mnt/
total 0
# 导出方式(1)
[root@Server ~]# docker image save centos:8.1.1911 -o /mnt/centos8.1.1911.tar
[root@Server ~]# ls -l /mnt/
total 239212
-rw------- 1 root root 244953088 Apr 18 12:52 centos8.1.1911.tar
# 导出方式(2)
[root@Server ~]# docker image save centos:7.7.1908 > /mnt/centos7.7.1908.tar
[root@Server ~]# ls -l /mnt/
total 446036
-rw-r--r-- 1 root root 211785728 Apr 18 12:54 centos7.7.1908.tar
-rw------- 1 root root 244953088 Apr 18 12:52 centos8.1.1911.tar
2.6、导入镜像命令

描述(英文):Load an image from a tar archive or STDIN
描述(中文):从tar存档或STDIN加载镜像
语法:docker image load [OPTIONS]
选项:

选项 英文说明&中文说明
-i, --input string Read from tar archive file, instead of STDIN
从tar存档文件中读取,而不是从STDIN中读取
-q, --quiet Suppress the load output
抑制导入输出
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@Server ~]# ls -l /mnt/
total 446036
-rw-r--r-- 1 root root 211785728 Apr 18 12:54 centos7.7.1908.tar
-rw------- 1 root root 244953088 Apr 18 12:52 centos8.1.1911.tar
# 导入方式(1)
[root@Server ~]# docker image load -i /mnt/centos8.1.1911.tar
0683de282177: Loading layer  244.9MB/244.9MB
Loaded image: centos:8.1.1911
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              8.1.1911            470671670cac        3 months ago        237MB
# 导入方式(2)
[root@Server ~]# docker image load < /mnt/centos7.7.1908.tar
034f282942cd: Loading layer  211.8MB/211.8MB
Loaded image: centos:7.7.1908
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              8.1.1911            470671670cac        3 months ago        237MB
centos              7.7.1908            08d05d1d5859        5 months ago        204MB
2.7、查看镜像详细信息

描述(英文):Display detailed information on one or more images
描述(中文):显示一个或多个镜像的详细信息
语法:docker image inspect [OPTIONS] IMAGE [IMAGE...]
选项:

选项 英文说明&中文说明
-f, --format string Format the output using the given Go template
使用给定的Go模板格式化输出
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.17                e791337790a6        18 hours ago        127MB
debian              10.3                378ca4b1d2fe        2 days ago          114MB
ubuntu              19.10               e9ccb229a23d        4 weeks ago         72.9MB
centos              8.1.1911            470671670cac        3 months ago        237MB
centos              7.7.1908            08d05d1d5859        5 months ago        204MB
[root@Server ~]# docker image inspect centos:8.1.1911
[
    {
        "Id": "sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee",
        "RepoTags": [
            "centos:8.1.1911"
        ],
        "RepoDigests": [],
        "Parent": "",
        "Comment": "",
        "Created": "2020-01-18T00:26:46.850750902Z",
        "Container": "57333b28d93d47fedc0cc5c995092b9266fa4b73b64f3f88d42a08e46f1aab48",
        "ContainerConfig": {
            "Hostname": "57333b28d93d",
            "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 [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:0fff7846fd29d8959680f5c1ed8ee2f30197a479c0f73a88dedc0f0755d3277d",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200114",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS",
                "org.opencontainers.image.created": "2020-01-14 00:00:00-08:00",
                "org.opencontainers.image.licenses": "GPL-2.0-only",
                "org.opencontainers.image.title": "CentOS Base Image",
                "org.opencontainers.image.vendor": "CentOS"
            }
        },
        "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": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:0fff7846fd29d8959680f5c1ed8ee2f30197a479c0f73a88dedc0f0755d3277d",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200114",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS",
                "org.opencontainers.image.created": "2020-01-14 00:00:00-08:00",
                "org.opencontainers.image.licenses": "GPL-2.0-only",
                "org.opencontainers.image.title": "CentOS Base Image",
                "org.opencontainers.image.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 237117212,
        "VirtualSize": 237117212,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/4df82e484eed03776068015b6698ee852a5a24ddfe8ad7d9f974c2e3a07449a3/merged",
                "UpperDir": "/var/lib/docker/overlay2/4df82e484eed03776068015b6698ee852a5a24ddfe8ad7d9f974c2e3a07449a3/diff",
                "WorkDir": "/var/lib/docker/overlay2/4df82e484eed03776068015b6698ee852a5a24ddfe8ad7d9f974c2e3a07449a3/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:0683de2821778aa9546bf3d3e6944df779daba1582631b7ea3517bb36f9e4007"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
2.8、修改镜像的Tag

描述(英文):Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
描述(中文):创建一个引用源镜像的目标镜像标记
语法:docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.17                e791337790a6        18 hours ago        127MB
debian              10.3                378ca4b1d2fe        2 days ago          114MB
ubuntu              19.10               e9ccb229a23d        4 weeks ago         72.9MB
centos              8.1.1911            470671670cac        3 months ago        237MB
centos              7.7.1908            08d05d1d5859        5 months ago        204MB
[root@Server ~]# docker image tag centos:8.1.1911 centos:latest
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.17                e791337790a6        18 hours ago        127MB
debian              10.3                378ca4b1d2fe        2 days ago          114MB
ubuntu              19.10               e9ccb229a23d        4 weeks ago         72.9MB
centos              8.1.1911            470671670cac        3 months ago        237MB
centos              latest              470671670cac        3 months ago        237MB
centos              7.7.1908            08d05d1d5859        5 months ago        204MB
2.9、删除镜像命令

描述(英文):Remove one or more images
描述(中文):移除一个或多个镜像
语法:docker image rm [OPTIONS] IMAGE [IMAGE...]
别名:rm, rmi, remove
选项:

选项 英文说明&中文说明
-f, --force Force removal of the image
强制移除镜像
--no-prune Do not delete untagged parents
不删除未标记的父类镜像
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.17                e791337790a6        21 hours ago        127MB
debian              10.3                378ca4b1d2fe        2 days ago          114MB
ubuntu              20.04               a3282b72a167        4 weeks ago         73.8MB
ubuntu              19.10               e9ccb229a23d        4 weeks ago         72.9MB
centos              8.1.1911            470671670cac        3 months ago        237MB
centos              7.7.1908            08d05d1d5859        5 months ago        204MB
[root@Server ~]# docker image rm centos:8.1.1911
Error response from daemon: conflict: unable to remove repository reference "centos:8.1.1911" (must force) - container 0d9cf07a8636 is using its referenced image 470671670cac
# 删除一个镜像
# docker image rm 470671670cac
[root@Server ~]# docker image rm -f centos:8.1.1911
Untagged: centos:8.1.1911
Deleted: sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.17                e791337790a6        21 hours ago        127MB
debian              10.3                378ca4b1d2fe        2 days ago          114MB
ubuntu              20.04               a3282b72a167        4 weeks ago         73.8MB
ubuntu              19.10               e9ccb229a23d        4 weeks ago         72.9MB
centos              7.7.1908            08d05d1d5859        5 months ago        204MB
# 删除所有的镜像
# docker image rm -f `docker image ls -a -q`
[root@Server ~]# docker image rm -f $(docker image ls -a -q)
Untagged: nginx:1.17
Deleted: sha256:e791337790a6181d5ce870b3bb16de1a4d5aa3a916e8fba6907f57eb409934cf
Deleted: sha256:615a169a3412634ebf75d5f0f5162290fb6042ba36285bd0ddc9ee123165b95e
Deleted: sha256:bd32d67adcec3dba661c5afebc8a2a5413e68a3283b5ad7df134ed86f00b380a
Deleted: sha256:b60e5c3bcef2f42ec42648b3acf7baf6de1fa780ca16d9180f3b4a3f266fe7bc
Untagged: debian:10.3
Deleted: sha256:378ca4b1d2fe114d06668e67b273877159c5bae9e8d8e9ef7c61ff8305e30692
Untagged: ubuntu:20.04
Untagged: ubuntu@sha256:1515a62dc73021e2e7666a31e878ef3b4daddc500c3d031b35130ac05067abc0
Deleted: sha256:a3282b72a167a35ad4c8221217b6fb428d0523e848ca1e4e84aa36ca8044f897
Deleted: sha256:6d79622f1830aa7e036a206957e522cbb5b16276ea782ca4909455ed88665931
Deleted: sha256:9ab246c328c316da183cad9612efdd19c7062b0c1815f2e00fec9d6d4eb79850
Deleted: sha256:c6b6cb7b5361dbcd607e546b9e6f3f4397636fc7157c65bc0294970d6a9fdf91
Deleted: sha256:9c75c22ceed8daa4dc61a5708fc6631e3f083bef7a2cfd6554cd3fd7e2b33013
Untagged: ubuntu:19.10
Deleted: sha256:e9ccb229a23d2d51da337c78c4ba1fd844f476e4639cd0c91099d1b62b8f8a45
Untagged: centos:7.7.1908
Deleted: sha256:08d05d1d5859ebcfb3312d246e2082e46cb307f0e896c9ac097185f0b0b19e56
[root@Server ~]# docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

你可能感兴趣的:(Docker命令集锦-镜像操作命令)