首页 Docker教程docker本地镜像管理

docker-镜像加速配置

docker-容器启动入门

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

查看本地镜像

docker images 可以查看下载至本地的镜像

格式:

docker images [OPTIONS] [REPOSITORY[:TAG]]
常用选项:
-q, --quiet     Only show numeric IDs
-a, --all       Show all images (default hides intermediate images)
--digests       Show digests
--no-trunc      Don't truncate output

执行结果的显示信息说明:

REPOSITORY      #镜像所属的仓库名称
TAG          #镜像版本号(标识符),默认为latest
IMAGE ID       #镜像唯一ID标示
CREATED        #镜像创建时间
VIRTUAL SIZE     #镜像的大小

范例:

[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              3.11.3              e7d92cdc71fe        7 days ago          5.59MB
centos              centos8.1.1911      470671670cac        7 days ago          237MB
busybox             latest              6d5fcfe5ff17        4 weeks ago         1.22MB
hello-world         latest              fce289e99eb9        12 months ago       1.84kB
[root@ubuntu1804 ~]#docker images -q
e7d92cdc71fe
470671670cac
6d5fcfe5ff17
fce289e99eb9

#只查看指定REPOSITORY的镜像
[root@centos8 ~]#podman images localhost/alpine-base
REPOSITORY              TAG    IMAGE ID       CREATED          SIZE
localhost/alpine-base   3.11   69abbff7bed9   37 minutes ago   5.87 MB

#查看指定镜像的信息
[root@centos8 ~]#podman image inspect alpine
[
    {
        "Id": "e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a",
        "Digest": "sha256:ddba4d27a7ffc3f86dd6c2f92041af252a1f23a8e742c90e6e1297bfa1bc0c45",
        "RepoTags": [
            "docker.io/library/alpine:latest"
        ],
        "RepoDigests": [
            "docker.io/library/alpine@sha256:ddba4d27a7ffc3f86dd6c2f92041af252a1f23a8e742c90e6e1297bfa1bc0c45"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-01-18T01:19:37.187497623Z",
        "Config": {
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh"
            ]
        },
        "Version": "18.06.1-ce",
        "Author": "",
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 5859847,
        "VirtualSize": 5859847,
        "GraphDriver": {
            "Name": "overlay",
            "Data": {
                "MergedDir": "/var/lib/containers/storage/overlay/5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10/merged",
                "UpperDir": "/var/lib/containers/storage/overlay/5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10/diff",
                "WorkDir": "/var/lib/containers/storage/overlay/5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10/work"
            }
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"
            ]
        },
        "Labels": null,
        "Annotations": {},
        "ManifestType": "application/vnd.docker.distribution.manifest.v2+json",
        "User": "",
        "History": [
            {
                "created": "2020-01-18T01:19:37.02673981Z",
                "created_by": "/bin/sh -c #(nop) ADD file:e69d441d729412d24675dcd33e04580885df99981cec43de8c9b24015313ff8e in / "
            },
            {
                "created": "2020-01-18T01:19:37.187497623Z",
                "created_by": "/bin/sh -c #(nop)  CMD [\"/bin/sh\"]",
                "empty_layer": true
            }
        ]
    }
]

[root@centos8 ~]#
镜像导出

利用docker save命令可以将从本地镜像导出问为一个压缩文件,然后复制到其他服务器进行导入使用。

格式:

docker save [OPTIONS] IMAGE [IMAGE...]
选项:
-o, --output string   Write to a file, instead of STDOUT

常见用法:

docker save IMAGE -o /path/file.tar.gz
docker save IMAGE > /path/file.tar.gz

范例:

[root@ubuntu1804 ~]#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              5ad3bd0e67a9        3 days ago          127MB
alpine              3.11.3              e7d92cdc71fe        7 days ago          5.59MB
centos              centos8.1.1911      470671670cac        7 days ago          237MB
centos              latest              470671670cac        7 days ago          237MB
mysql               5.6.47              742f7d5a4104        10 days ago         302MB
mysql               5.7.29              b598110d0fff        10 days ago         435MB
busybox             latest              6d5fcfe5ff17        4 weeks ago         1.22MB
hello-world         latest              fce289e99eb9        12 months ago       1.84kB
[root@ubuntu1804 ~]#docker save mysql:5.7.29 -o /data/mysql5.7.29.tar.gz
[root@ubuntu1804 ~]#docker save alpine:3.11.3 > /data/alpine.tar.gz
[root@ubuntu1804 ~]#scp /data/mysql5.7.29.tar.gz /data/alpine.tar.gz   10.0.0.7:/data
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:5bc0+A46C5m0zxT3WKIBg/i+oVUq9avVRjO9dy9gz2k.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7's password: 
mysql5.7.29.tar.gz                                                                                  100%  420MB  32.2MB/s   00:13    
alpine.tar.gz                                                                                       100% 5728KB  39.1MB/s   00:00    
镜像导入

利用docker load命令可以将镜像导出的压缩文件再导入

格式:

docker load [OPTIONS]
选项
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output

范例:

[root@centos7 ~]#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@centos7 ~]#docker load -i /data/alpine.tar.gz 
5216338b40a7: Loading layer  5.857MB/5.857MB
Loaded image ID: sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a
[root@centos7 ~]#docker load < /data/mysql5.7.29.tar.gz 
814c70fdae62: Loading layer  58.51MB/58.51MB
25575e327c84: Loading layer  338.4kB/338.4kB
61cb1c0dec27: Loading layer  10.44MB/10.44MB
955b4c88a6e8: Loading layer  4.472MB/4.472MB
fef9e518b701: Loading layer  1.536kB/1.536kB
cf6a13051478: Loading layer  46.15MB/46.15MB
2a60eb850753: Loading layer   34.3kB/34.3kB
265cbb07b679: Loading layer  3.584kB/3.584kB
5e2692f04e32: Loading layer    320MB/320MB
569004ea94bf: Loading layer  15.87kB/15.87kB
a5d0d78cfb76: Loading layer  1.536kB/1.536kB
Loaded image: mysql:5.7.29
[root@centos7 ~]#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              3.11.3              e7d92cdc71fe        7 days ago          5.59MB
mysql               5.7.29              b598110d0fff        10 days ago         435MB
[root@centos7 ~]#

范例:一次导出多个镜像

[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              e7d92cdc71fe        7 days ago          5.59MB
busybox             latest              6d5fcfe5ff17        4 weeks ago         1.22MB
[root@ubuntu1804 ~]#docker save busybox alpine > /all.tar.gz
[root@ubuntu1804 ~]#ll -h /opt/all.tar.gz
-rw-r--r-- 1 root root 7.0M Jan 25 22:12 /opt/all.tar.gz
[root@ubuntu1804 ~]#docker rmi -f docker images -q
Untagged: alpine:latest
Deleted: sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a
Deleted: sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10
Untagged: busybox:latest
Deleted: sha256:6d5fcfe5ff170471fcc3c8b47631d6d71202a1fd44cf3c147e50c8de21cf0648
Deleted: sha256:195be5f8be1df6709dafbba7ce48f2eee785ab7775b88e0c115d8205407265c5
[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@ubuntu1804 ~]#docker load -i /opt/all.tar.gz 
5216338b40a7: Loading layer [==================================================>]  5.857MB/5.857MB
Loaded image: alpine:latest
195be5f8be1d: Loading layer [==================================================>]  1.437MB/1.437MB
Loaded image: busybox:latest
[root@ubuntu1804 ~]#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              e7d92cdc71fe        7 days ago          5.59MB
busybox             latest              6d5fcfe5ff17        4 weeks ago         1.22MB
删除镜像

docker rmi 命令可以删除本地镜像

格式

docker rmi [OPTIONS] IMAGE [IMAGE...]
选项:
 -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents

范例:

[root@centos7 ~]#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              3.11.3              e7d92cdc71fe        7 days ago          5.59MB
mysql               5.7.29              b598110d0fff        10 days ago         435MB
[root@centos7 ~]#dockr rmi mysql
-bash: dockr: command not found
[root@centos7 ~]#docker rmi mysql
Error: No such image: mysql
[root@centos7 ~]#docker rmi b59811
Untagged: mysql:5.7.29
Deleted: sha256:b598110d0fffc42862269a25d8767dd95764d0740f318fd6c0a097f8a22de5bf
Deleted: sha256:908aaab4c4b8d0cbbc68c96a0e3820aa74fd1dee5499e2ca326bc8fd7312f689
Deleted: sha256:e2f2e83f295186b00e3c0d119ef3204b509d552972694e34cee7c1675d157b8a
Deleted: sha256:99b4e48b1be76f50741db02a38c783bf698b1b76808cc6bb5e3fdd65ee2897c6
Deleted: sha256:79c1efa7bde3ac754af64779452ca913fa1f281b44c9dbad25cc322a51ac69b1
Deleted: sha256:5f7c68324b959d2c806db18d02f153bc810f9842722415e077351bc834cc8578
Deleted: sha256:338fc0cd3fb4b87a2b83d274e8fbf475fbde19947c4ac5c5eb6e981a6fb0e8f0
Deleted: sha256:f7a4ccab931f1d1e861961eb951a7806d91ccb375e737fe1f84282f6bbafd2be
Deleted: sha256:f388e1092f8fb931a3cd07a7381bd9707d19526ff81f8b624e932f4919c27a3e
Deleted: sha256:e209b7a884b4d2e9d56bbac40ced48f2caa6a19e7ad6eb6dd20ff754f3af2c5d
Deleted: sha256:2401cf11c5455d505ef49657afcc709197ffcdfc9bd732508e9b62578a30b3a5
Deleted: sha256:814c70fdae62bc26c603bfae861f00fb1c77fc0b1ee8d565717846f4df24ae5d
[root@centos7 ~]#docker rmi alpine:3.11.3
Untagged: alpine:3.11.3
Deleted: sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a
Deleted: sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10
[root@centos7 ~]#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

范例:强制删除正在使用的镜像,也会删除对应的容器

[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                        PORTS                     NAMES
b5a0d2e1e1d0        centos:centos8.1.1911   "bash"                   41 minutes ago      Up 41 minutes                                           jolly_burnell
[root@ubuntu1804 ~]#docker rmi centos:centos8.1.1911
Error response from daemon: conflict: unable to remove repository reference "centos:centos8.1.1911" (must force) - container b5a0d2e1e1d0 is using its referenced image 470671670cac
[root@ubuntu1804 ~]#docker rmi -f centos:centos8.1.1911
Untagged: centos:centos8.1.1911
Untagged: centos@sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                     NAMES
[root@ubuntu1804 ~]#

范例:删除所有镜像

[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              470671670cac        7 days ago          237MB
mysql               5.6.47              742f7d5a4104        10 days ago         302MB
[root@ubuntu1804 ~]#docker rmi  -f docker images -q
Deleted: sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee
Deleted: sha256:0683de2821778aa9546bf3d3e6944df779daba1582631b7ea3517bb36f9e4007
Untagged: mysql:5.6.47
Untagged: mysql@sha256:9527bae58991a173ad7d41c8309887a69cb8bd178234386acb28b51169d0b30e
Deleted: sha256:742f7d5a4104969fcac8054cf9201f5656096f0a58d10947a4a41a8e1d7d9f91
Deleted: sha256:62530ddc9fe5f85609da4397d9e0a88b422d15dbc42664d7477d1deccb51a0d9
Deleted: sha256:41309d62590858b6375bd3c4e9d07bd73e4ea5062343a81641453033424e7aba
Deleted: sha256:43cb5cbfcce71f7fdd80b121aa4d0c5eea9ed0bfcf4a2a0e55a8c5ddb78d4368
Deleted: sha256:56806995b55c0e80ee0daec20f32f8293a3d8ccf151895cd1767a1d27eab1977
Deleted: sha256:7dab204c1f667de5090d809019fab7d6e2323e371407b38e8d311ba458009234
Deleted: sha256:1a7c2d06fdee90c4b06a55793f9e45120a82ff476e0fea5618efd8a268f7d303
Deleted: sha256:f7a4ccab931f1d1e861961eb951a7806d91ccb375e737fe1f84282f6bbafd2be
Deleted: sha256:f388e1092f8fb931a3cd07a7381bd9707d19526ff81f8b624e932f4919c27a3e
Deleted: sha256:e209b7a884b4d2e9d56bbac40ced48f2caa6a19e7ad6eb6dd20ff754f3af2c5d
Deleted: sha256:2401cf11c5455d505ef49657afcc709197ffcdfc9bd732508e9b62578a30b3a5
Deleted: sha256:814c70fdae62bc26c603bfae861f00fb1c77fc0b1ee8d565717846f4df24ae5d
[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
镜像打标签

docker tag 可以给镜像打标签,类似于起别名

格式

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

TAG默认为latest

范例:

[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              e7d92cdc71fe        11 days ago         5.59MB
centos              centos7.7.1908      08d05d1d5859        2 months ago        204MB
[root@ubuntu1804 ~]#docker tag  alpine alpine:3.11
[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              3.11                e7d92cdc71fe        11 days ago         5.59MB
alpine              latest              e7d92cdc71fe        11 days ago         5.59MB
centos              centos7.7.1908      08d05d1d5859        2 months ago        204MB

总结:企业使用镜像及常见操作:搜索、下载、导出、导入、删除

命令总结:

docker search centos

docker pull alpine

docker images

docker save > /opt/centos.tar #centos #导出镜像

docker load -i centos-latest.tar.xz #导入本地镜像

docker rmi 镜像ID/镜像名称 #删除指定ID的镜像,此镜像对应容器正启动镜像不能被删除,除非将容器全部关闭

本文链接:http://www.yunweipai.com/34793.html

docker-镜像加速配置

docker-容器启动入门

网友评论comments

发表评论

邮箱地址不会被公开。

暂无评论

Copyright © 2012-2021 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部