【Docker】02 镜像管理


文章目录

  • 一、Images镜像
  • 二、管理操作
    • 2.1 搜索镜像
      • 2.1.1 命令行搜索
      • 2.1.2 页面搜索
      • 2.1.3 搜索条件
    • 2.2 下载镜像
    • 2.3 查看本地镜像
      • 2.3.1 docker images
      • 2.3.2 --help
      • 2.3.3 repository name
      • 2.3.4 --filter
      • 2.3.5 -q
      • 2.3.6 --format
    • 2.4 给镜像打标签
    • 2.5 推送镜像
    • 2.6 删除镜像
    • 2.7 导出导入镜像
  • 三、镜像文件信息
    • 3.1 镜像存储位置
    • 3.2 查看镜像层文件


一、Images镜像

镜像,可理解为一个模板,Docker可根据这些Images来执行生成容器来运行,也可将容器打包成镜像。

二、管理操作

2.1 搜索镜像

2.1.1 命令行搜索

通过docker search命令来搜索相关镜像。

[root@server ~]# docker search alpine
NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
alpine                             A minimal Docker image based on Alpine Linux…   10611     [OK]       
alpinelinux/docker-cli             Simple and lightweight Alpine Linux image wi…   10                   
alpinelinux/alpine-gitlab-ci       Build Alpine Linux packages with Gitlab CI      3                    
alpinelinux/gitlab-runner-helper   Helper image container gitlab-runner-helper …   6                    
alpinelinux/rsyncd                                                                 2         

2.1.2 页面搜索

【Docker】02 镜像管理_第1张图片

2.1.3 搜索条件

[root@server ~]# docker search --help

Usage:  docker search [OPTIONS] TERM

Search Docker Hub for images

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
      --no-trunc        Don't truncate output

增加搜索条件,过滤STARS大于等于3000的镜像:

[root@server ~]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   14779     [OK]       
mariadb   MariaDB Server is a high performing open sou…   5638      [OK]  

2.2 下载镜像

直接拉取(下载)相关镜像的话,默认是最新版本:

[root@server ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
661ff4d9561e: Already exists 
Digest: sha256:51b67269f354137895d43f3b3d810bfacd3945438e94dc5ac55fdac340352f48
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

下载指定Tag版本:

[root@server ~]# docker pull alpine:3.10.3
3.10.3: Pulling from library/alpine
89d9c30c1d48: Pull complete 
Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Status: Downloaded newer image for alpine:3.10.3
docker.io/library/alpine:3.10.3

2.3 查看本地镜像

官方文档:docker images

2.3.1 docker images

通过docker images命令查看本地镜像:

[root@server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
redis         alpine    20658529aaf6   8 days ago     46.1MB
alpine        latest    f8c20f8bbcb6   5 weeks ago    7.38MB
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB
alpine        3.10.3    965ea09ff2eb   4 years ago    5.55MB

2.3.2 --help

[root@server ~]# docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  docker image ls, docker image list, docker images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format output using a custom template:
                        'table':            Print output in table format with column headers
                        (default)
                        'table TEMPLATE':   Print output in table format using the given Go
                        template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go template.
                        Refer to https://docs.docker.com/go/formatting/ for more information
                        about formatting output with templates
      --no-trunc        Don't truncate output    不对镜像ID做截取
  -q, --quiet           Only show image IDs

2.3.3 repository name

# 显示指定仓库Repository的镜像,可带Tag标签
[root@server ~]# docker images alpine
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
alpine       latest    f8c20f8bbcb6   5 weeks ago   7.38MB
alpine       3.10.3    965ea09ff2eb   4 years ago   5.55MB

[root@server ~]# docker images alpine:latest
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
alpine       latest    f8c20f8bbcb6   5 weeks ago   7.38MB

2.3.4 --filter

# 显示未打Tag的镜像,此处是无
[root@server ~]# docker images --filter "dangling=true"
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@server ~]# docker images --filter "dangling=false"
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
pro_omc_ops   1.0       c5f5e39dedbd   2 days ago     309MB
redis         alpine    20658529aaf6   8 days ago     46.1MB
alpine        latest    f8c20f8bbcb6   5 weeks ago    7.38MB
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB
alpine        3.10.3    965ea09ff2eb   4 years ago    5.55MB

# 删除无Tag标签的镜像
[root@server ~]# docker rmi $(docker images --filter "dangling=true" -q) 

# 显示在某个镜像创建之前的所有镜像
[root@server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
alpine        latest    f8c20f8bbcb6   5 weeks ago    7.38MB
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB
[root@server ~]# docker images --filter "before=alpine"
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB
[root@server ~]# docker images --filter "before=f8c20f8bbcb6"
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB

# 显示指定镜像创建时间之后的所有镜像
[root@server ~]# docker images --filter "since=hello-world"
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
alpine       latest    f8c20f8bbcb6   5 weeks ago   7.38MB

# 镜像REPOSITORY的模糊查找,支持多个reference
[root@server ~]# docker images --filter reference='al*' 
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
alpine       latest    f8c20f8bbcb6   5 weeks ago   7.38MB
[root@server ~]# docker images --filter reference='hello*'
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB
[root@server ~]# docker images --filter reference='hello*' --filter reference='al*'
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
alpine        latest    f8c20f8bbcb6   5 weeks ago    7.38MB
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB

2.3.5 -q

# 只显示镜像的ID
[root@server ~]# docker images -q 
c5f5e39dedbd
20658529aaf6
f8c20f8bbcb6
d2c94e258dcb
965ea09ff2eb

2.3.6 --format

--format格式化的相关参数:
【Docker】02 镜像管理_第2张图片

[root@server ~]# docker images --format "{{.ID}}: {{.Repository}}"
f8c20f8bbcb6: alpine
d2c94e258dcb: hello-world

[root@server ~]# docker images --format "table {{.ID}}:\t{{.Repository}}\t{{.Tag}}"
IMAGE ID:       REPOSITORY    TAG
f8c20f8bbcb6:   alpine        latest
d2c94e258dcb:   hello-world   latest

# 以JSON格式列出所有镜像
[root@server ~]# docker images --format json
{"Containers":"N/A","CreatedAt":"2023-12-08 09:20:49 +0800 CST","CreatedSince":"5 weeks ago","Digest":"\u003cnone\u003e","ID":"f8c20f8bbcb6","Repository":"alpine","SharedSize":"N/A","Size":"7.38MB","Tag":"latest","UniqueSize":"N/A","VirtualSize":"7.377MB"}
{"Containers":"N/A","CreatedAt":"2023-05-03 00:49:27 +0800 CST","CreatedSince":"8 months ago","Digest":"\u003cnone\u003e","ID":"d2c94e258dcb","Repository":"hello-world","SharedSize":"N/A","Size":"13.3kB","Tag":"latest","UniqueSize":"N/A","VirtualSize":"13.26kB"}

2.4 给镜像打标签

[root@server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
alpine        latest    f8c20f8bbcb6   5 weeks ago    7.38MB
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB
[root@server ~]# docker tag f8c20f8bbcb6 docker.io/alpine:2.2.2
[root@server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
alpine        2.2.2     f8c20f8bbcb6   5 weeks ago    7.38MB
alpine        latest    f8c20f8bbcb6   5 weeks ago    7.38MB
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB

2.5 推送镜像

[root@server ~]# docker push docker.io/asdfv1929/alpine:v7.31
The push refers to repository [docker.io/asdfv1929/alpine]
72e830a4dff5: Mounted from library/alpine
v7.31: digest: sha256:1775bebec23e1f3ce486989bfc9ff3c4e951690df84aa9f926497d82f2ffca9d size: 528

2.6 删除镜像

[root@server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
alpine        2.2.2     f8c20f8bbcb6   5 weeks ago    7.38MB
alpine        latest    f8c20f8bbcb6   5 weeks ago    7.38MB
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB

[root@server ~]# docker rmi alpine:2.2.2 
Untagged: alpine:2.2.2
[root@server ~]# docker images          
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
alpine        latest    f8c20f8bbcb6   5 weeks ago    7.38MB
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB
[root@server ~]# docker rmi alpine:latest
Untagged: alpine:latest
Untagged: alpine@sha256:51b67269f354137895d43f3b3d810bfacd3945438e94dc5ac55fdac340352f48
Deleted: sha256:f8c20f8bbcb684055b4fea470fdd169c86e87786940b3262335b12ec3adef418
[root@server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB

# -f 强制删除
[root@server ~]# docker rmi -f d2c94e258dcb
Untagged: hello-world:latest
Untagged: hello-world@sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493
Deleted: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a

# 删除所有镜像
[root@server ~]# docker rmi -f $(docker images -aq)
Untagged: hello-world:latest
Untagged: hello-world@sha256:4bd78111b6914a99dbc560e6a20eab57ff6655aea4a80c50b0c5491968cbc2e6
Deleted: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a
[root@server ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@server ~]# 

2.7 导出导入镜像

[root@server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB

# 导出镜像
[root@server ~]# docker save d2c94e258dcb > hello-latest.tar

[root@server ~]# ll hello-latest.tar 
-rw-r--r--. 1 root root 22016 Jan 18 20:05 hello-latest.tar

# 导入镜像
[root@server ~]# docker load -i hello-latest.tar 
Loaded image ID: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a

三、镜像文件信息

3.1 镜像存储位置

[root@server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB

# 文件的前12位即为镜像的ID
[root@server ~]# ll /var/lib/docker/image/overlay2/imagedb/content/sha256/
total 4
-rw-------. 1 root root 581 Jan 18 19:59 d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a

# 查看镜像文件内容
[root@server ~]# cat /var/lib/docker/image/overlay2/imagedb/content/sha256/d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a   
{"architecture":"amd64","config":{"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["/hello"],"WorkingDir":"/","ArgsEscaped":true,"OnBuild":null},"created":"2023-05-02T16:49:27Z","history":[{"created":"2023-05-02T16:49:27Z","created_by":"COPY hello / # buildkit","comment":"buildkit.dockerfile.v0"},{"created":"2023-05-02T16:49:27Z","created_by":"CMD [\"/hello\"]","comment":"buildkit.dockerfile.v0","empty_layer":true}],"os":"linux","rootfs":{"type":"layers","diff_ids":["sha256:ac28800ec8bb38d5c35b49d45a6ac4777544941199075dff8c4eb63e093aa81e"]}}

3.2 查看镜像层文件

[root@server ~]# ll /var/lib/docker/image/overlay2/layerdb/sha256/
total 0
drwx------. 2 root root 85 Jan 10 14:42 03b689971beda84d64b529b31795b90dd04b8101a6cb6eb130401c3b3f789d0a
drwx------. 2 root root 85 Jan 16 09:32 1db87e37572d9033619f854bd3a90343a745eacad89661951c45378532b3a70f
drwx------. 2 root root 85 Jan 10 15:42 2199eb66405a876e223944aa9bf864899a1d7642725867cbad2f987c30a7bd4c
drwx------. 2 root root 85 Jan 10 16:00 31fd5efb2e46f5ec563eeecea425110667b89cab1c91358ec2c2abad895d574f
drwx------. 2 root root 85 Jan 10 14:43 50f5a10ada14518bde74ac2e3d496817ae963780c3465a68d42c6f2253594f29
drwx------. 2 root root 85 Jan 10 14:42 595431852bbc71ca9610933a01959afa4f2c41509ace1bbb54b6ae9af20a4e20
drwx------. 2 root root 71 Jan 10 14:42 5af4f8f59b764c64c6def53f52ada809fe38d528441d08d01c206dfb3fc3b691
drwx------. 2 root root 85 Jan 16 10:53 6048310efaa222cf9cf8d2bc8f3c39835d172a62320426257b08348f98f8f471
drwx------. 2 root root 85 Jan 10 16:20 63342cdfc4c83913172e3ed5644b5b3168d0b8bcbbbc2b29e9cf8ad67d7f4f47
drwx------. 2 root root 85 Jan 10 16:41 6e1739548915b0823ef27e8c5b4b5a38ff8dfe4cad8cbb872f5ab5755aab47f6
drwx------. 2 root root 85 Jan 16 10:43 73e8eba6c99272151a3bc9c95c23ae4e4ee77fdadbd65dbe00976d06c01d79c8
drwx------. 2 root root 85 Jan 10 14:43 7443e951ac7ff82bc91ebee219fe24df641bad668c4dc1eaf0998a81be2fdac8
drwx------. 2 root root 85 Jan 10 15:54 7aabc2f19676e207c21ff591882b3216697cc27813d45816ca64e04b2837a6aa
drwx------. 2 root root 71 Jan 10 13:27 ac28800ec8bb38d5c35b49d45a6ac4777544941199075dff8c4eb63e093aa81e
drwx------. 2 root root 85 Jan 10 16:16 bb464434e5d56949ee8f1080bdae4d2b3eeb57a1451d7c195eb1f71451926057
drwx------. 2 root root 85 Jan 10 14:42 bd6461f4898114060f6d30c2ae3828829c31804375461d304d0baead03333a63
drwx------. 2 root root 85 Jan 10 14:43 c51f246267806c659dc24c9c353ecd22421d44d659ffddb3ab64a4c403eb9204
drwx------. 2 root root 85 Jan 10 14:43 d1fce9f49c0ac97ea93606fa282700fca2f0aa0cdbea829d35c4da12406e1c0c
drwx------. 2 root root 85 Jan 10 17:15 ee5e48bebc436ac28496136d00ace179616ee3c35828f524629ed89718abec4b
drwx------. 2 root root 85 Jan 10 14:42 f7ff6a7ad88e8a59f027e202e5ca77fa4162d8b82ef66724daf26cbb9b5acdeb

镜像层文件夹内的结构:
size:占用大小
tar-split.json.gz:镜像层文件包

[root@server ~]# cd /var/lib/docker/image/overlay2/layerdb/sha256/
[root@server sha256]# ll 03b689971beda84d64b529b31795b90dd04b8101a6cb6eb130401c3b3f789d0a/
total 92
-rw-r--r--. 1 root root    64 Jan 10 14:42 cache-id
-rw-r--r--. 1 root root    71 Jan 10 14:42 diff
-rw-r--r--. 1 root root    71 Jan 10 14:42 parent
-rw-r--r--. 1 root root     8 Jan 10 14:42 size
-rw-r--r--. 1 root root 77504 Jan 10 14:42 tar-split.json.gz
[root@server sha256]# 

你可能感兴趣的:(#,Docker,docker,容器)