Docker 镜像管理

一、查看镜像有关信息

1.1 查看镜像历史分层

root@lck:~# docker pull nginx
root@lck:~# docker image history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
ae2feff98a0c        2 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB              
<missing>           2 weeks ago         /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB              
<missing>           2 weeks ago         /bin/sh -c #(nop) COPY file:e7e183879c35719c…   1.2kB               
<missing>           2 weeks ago         /bin/sh -c set -x     && addgroup --system -…   63.7MB              
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.5.0        0B                  
<missing>           2 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.19.6     0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:3a7bff4e139bcacc5…   69.2MB 

1.2 获取镜像元数据(JOSN格式)

root@lck:~# docker inspect nginx

1.3 将指定镜像保存成 tar 归档文件

docker save -o nginx nginx.tar

[root@ubuntu1804 ~]#ll -h nginx.tar
-rw------- 1 root root 131M Jul 20 22:33 nginx.tar

二、搜索镜像

格式

docker search [OPTIONS] TERM
Options:
-f, --filter filter   Filter output based on conditions provided        #过滤条件
     --format string   Pretty-print search using a Go template
     --limit int       Max number of search results (default 25)        #指定最大搜索数量,默认25
     --no-trunc       Don't truncate output                             #不截断描述信息
说明:  
OFFICIAL: 官方
AUTOMATED: 使用第三方docker服务来帮助编译镜像,可以在互联网上面直接拉取到镜像,减少了繁琐的编译过程

2.1 搜索 centos 有关镜像

root@lck:~# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   6351                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              132                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   124                                     [OK]
jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   117                                     [OK]
centos/systemd                     systemd enabled base container.                 92                                      [OK]

2.2 搜索点赞100个以上的镜像

docker search -s 100 centos               #旧语法
docker search --filter=stars=100 centos   #新语法
docker search  centos -f stars=100

2.3 搜索官方发行版本

docker search  centos -f is-offical=true

三、下载镜像

格式

docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Options:
-a, --all-tags               Download all tagged images in the repository
     --disable-content-trust   Skip image verification (default true)
     --platform string         Set platform if server is multi-platform capable
-q, --quiet                   Suppress verbose output
NAME: 是镜像名,一般的形式 仓库服务器:端口/项目名称/镜像名称
:TAG: 即版本号,如果不指定 :TAG 则下载最新版镜像

3.1 镜像下载信息说明

root@lck:~# docker pull hello-world
Using default tag: latest                                                        #默认下载版本latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete                                                      #分层下载
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec  #摘要
Status: Downloaded newer image for hello-world:latest                            
docker.io/library/hello-world:latest                                             #下载的完整地址

镜像下载保存的路径

/var/lib/docker/overlay2/镜像ID

3.2 安装指定TAG的特定版本镜像

在这里插入图片描述

docker pull docker.io/library/mysql:5.7.32
docker pull mysql:5.7.32

3.3 指定DIGEST下载特定版本的镜像

先到 hub.docker.com查到指定版本的DIGEST在这里插入图片描述

docker pull alpine:20200626

四、查看本地镜像

格式

docker images [OPTIONS] [REPOSITORY[:TAG]]
常用选项:  
-q, --quiet     Only show numeric IDs                           #只显示镜像ID
-a, --all Show all images (default hides intermediate images)   #默认输出-a
--digests       Show digests                                    #显示摘要
--no-trunc     Don't truncate output                            #列出完整的镜像ID

4.1 执行结果信息说明

root@lck:~# docker images
REPOSITORY    TAG        IMAGE ID       CREATED         SIZE
busybox       latest     a77dce18d0ec   5 days ago      1.24MB
mysql         5.7.32     f07dfa83b528   13 days ago     448MB
alpine        latest     389fef711851   2 weeks ago     5.58MB
alpine        20200626   3c791e92a856   6 months ago    5.57MB
hello-world   latest     bf756fb1ae65   12 months ago   13.3kB

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

4.2 只查找指定镜像

root@lck:~# docker images alpine
REPOSITORY   TAG        IMAGE ID       CREATED        SIZE
alpine       latest     389fef711851   2 weeks ago    5.58MB
alpine       20200626   3c791e92a856   6 months ago   5.57MB

4.3 查看指定镜像的信息

root@lck:~# docker image inspect alpine

五、镜像导出

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

格式

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

常见用法:

docker save IMAGE -o /path/file.tar
docker save IMAGE > /path/file.tar
docker save mysql:5.7.29 -o /data/mysql5.7.29.tar
docker save alpine:3.11.3 > /data/alpine.tar

一次导出多个镜像

docker save busybox alpine > /all.tar

六、删除镜像

格式

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

删除镜像

docker rmi 镜像ID
docker rmi alpine:3.11.3

删除多个镜像

docker rmi nginx tomcat

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

docker rmi -f centos:centos8.1.1911

删除所有镜像

docker rmi -f `docker images -q`

七、镜像打标签

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

格式

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

范例:IMAGE ID不变,只有TAG变

root@lck:/home/lck# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
alpine       latest    389fef711851   2 weeks ago   5.58MB
root@lck:/home/lck# docker tag alpine alpine:3.11
root@lck:/home/lck# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
alpine       3.11      389fef711851   2 weeks ago   5.58MB
alpine       latest    389fef711851   2 weeks ago   5.58MB

八、镜像导入

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

格式

docker load [OPTIONS]
#选项
-i, --input string   Read from tar archive file, instead of STDIN
-q, --quiet         Suppress the load output
docker load -i /data/alpine.tar
docker load < /data/mysql5.7.29.tar.gz

一次导出多个镜像,并导入

docker save busybox alpine > /all.tar
docker load -i /opt/all.tar

一次导出多个镜像,并导入,导出的镜像repository、tag标记为none

docker save `docker images -q` > /all.tar
docker load -i /opt/all.tar

一次导出多个镜像,并导入,导出的镜像repository、tag标记不为空 nusybox:v1.0

docker save `docker images | awk 'NR!=1{print $1":"$2}'` > /all.tar
docker load -i /opt/all.tar

八、阿里云镜像加速

网站:http://cr.console.aliyun.com在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值