【Docker】Docker的镜像

一、镜像的获取

1、通过 docker pull --help 命令的显示结果:

dell@dell:~$ docker pull --help

Usage:	docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
  -q, --quiet                   Suppress verbose output

可以看到docker的拉取镜像的命令为

docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  • OPTIONS:可选配置项,-a代表下载指定镜像的所有标签的版本,-q代表简略输出信息。

  • NAME:完整格式为<域名/IP>[:端口号]/仓库名,<域名/IP>[:端口号]这部分默认是docker hub(docker的官方仓库地址);
    而仓库名一般是两段式:<用户名>/<软件名>,用户名不指定的话默认是library,即官方仓库。

  • [:TAG|@DIGEST]:可选项,镜像的版本,默认是latest(最新版)。

举个例子,比如从官网上拉一个ubuntu的镜像 ,可以简写为:

docker pull ubuntu

这个命令的全写为:

sudo docker pull registry.hub.docker.com/library/ubuntu:latest

执行后会显示如下结果:

latest: Pulling from library/ubuntu
7ddbc47eeb70: Pull complete 
c1bbdc448b72: Pull complete 
8c3b70e39044: Pull complete 
45d437916d57: Pull complete 
Digest: sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d
Status: Downloaded newer image for registry.hub.docker.com/library/ubuntu:latest
registry.hub.docker.com/library/ubuntu:latest

从中可以看到镜像由多层存储构成,下载时会一层一层下载,并在最后给出完整的sha256摘要。

2、使用这个刚pull的镜像

docker run -it --rm  ubuntu   bash

参数:

  • i: 交互式操作
  • t:一个终端
  • –rm:容器退出后就删除
  • bash:创建容器后执行bash命令
    然后执行:
exit;

退出容器。

二、镜像的查看

1、列出已下载到本地的镜像:

docker image ls
# 或使用
docker images

显示:

REPOSITORY                               TAG                 IMAGE ID            CREATED             SIZE
registry.hub.docker.com/library/ubuntu   latest              775349758637        3 weeks ago         64.2MB
ubuntu                                   latest              7698f282e524        6 months ago        69.9MB
hello-world                              latest              fce289e99eb9        10 months ago       1.84kB
ubuntu                                   15.10               9b9cb95443b5        3 years ago         137MB

可以查看到本地镜像的名称、标签、ID、创建时间以及大小。

一个镜像可以打上N个标签,一个镜像ID也可以对应多个标签。

使用q参数可以只显示镜像的ID:

 docker image ls -q

2、列出某个镜像

docker image ls  ubuntu

3、 过滤器(-f)

docker image ls -f since=ubuntu:14.04


docker image ls -f before=ubuntu:18.04

4、查看镜像的大小
docker的大小比在docker hub上保存的会小,因为docker hub上是压缩版本。

由于docker的多层结构的特性、且可继承和复用,多个镜像可能拥有共同的基础镜像,从而有共同的层,由于 Docker 使用 Union FS,相同的层只需要保存一份即可,所以多个镜像的总和大小比实际大小可能会小很多。

可以通过如下命令查看docker的镜像、容器和卷所占用的空间大小。

docker system df

结果:

dell@dell:~$  docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              4                   3                   271.3MB             64.19MB (23%)
Containers          6                   1                   123B                8B (6%)
Local Volumes       0                   0                   0B                  0B
Build Cache         0                   0                   0B                  0B

5、< none > 镜像
在用docker images命令时,可能会出现名称和标签均为< none >的镜像,这种镜像是在官方对该镜像更新后,镜像的名称和标签转移到新更新的镜像上,旧的版本的镜像就被“虚空悬挂”了。。。因此也叫虚玄镜像(dangling image)

可以通过如下命令列出虚玄镜像:

docker image ls -f dangling=true

这类镜像没啥用,可以直接删掉,通过命令:

docker image prune

6、中间层镜像

使用命令:

docker image ls -a

列出所有镜像,里头可能会含有无标签的镜像,这些镜像是顶层镜像的复用镜像,叫做中间层镜像

三、镜像的删除

1、删除命令

docker image rm [选项] <镜像1> [<镜像2> ...]

# 或使用

docker rmi [选项] <镜像1> [<镜像2> ...]

可以在一条命令中删除一个或多个镜像;

<镜像>可以使用镜像ID或镜像ID前几位或镜像名或镜像摘要(sha256)来删除。

2、批量删除
docker image ls -q 配合 docker image rm 批量删除镜像:

docker image rm $(docker image ls -q -f before=ubuntu:18.04)

docker image rm $(docker image ls -q redis)

3、特性

  • 有tag的镜像只会删除tag;
  • 无tag才会删除镜像本身;
  • 删除镜像的时候会自顶向下逐层删除,如果某一层别的镜像也有引用,则会停止删除。
  • 如果该镜像上有启动的容器,则无法删除。

四、制作镜像

单独写。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值