Docker镜像的概念和使用

  1. Docker镜像的概念

  2. 从Docker Hub获取镜像

  3. 列出镜像

  4. 删除镜像

  5. 打标签(创建副本)

  6. 镜像打包tar和从tar包导入

  7. 搜索镜像

  8. 查看镜像

1. Docker镜像的概念

Docker镜像是Docker容器运行的基础,是一个轻量级、可执行的文件,它是构建容器的模板,在Docker中扮演着重要的角色,每一个镜像包含一套预先配置好的文件系统和运行环境。

每一个镜像由多层文件系统组成,每一层有一个唯一的id,id为每一个层计算一个SHA256校验和,镜像本身是只读的,当有安装软件、更新配置的新操作,Docker会基于该容器创建一个可读写。

特点:

  • 分层结构,采用层叠方式构建镜像文件,新操作和变更会生成新层,每一个层有唯一id,在本地只会存储一份,有利于资源重复利用,提高存储效率,缓解磁盘存储上的压力。
  • 便携,镜像文件可以在任何安装了Docker的环境运行,因为其中包含了运行应用所需的所有依赖和配置等。
  • 标签化管理,Docker镜像通过标签可以进行版本控制,例如:redis:latest、redis:bookworm等,方便识别和选择不同的镜像构建我们自己的镜像。

2. 从Docker Hub获取镜像

Docker Hub是一个公共的镜像仓库,可以在其中找到各种各样的镜像,包括官方镜像、社区镜像、用户上传的自定义镜像等。可以使用docker pull命令从Docker获取镜像。

获取镜像:通过在终端中运行docker pull命令,指定镜像名称和标签,即可从Docker Hub获取指定镜像。

例如:使用docker pull ubuntu将会获取ubuntu最新版本的镜像,拉取镜像时,如没有指定标签,默认会拉取标签为latest的镜像,如下为执行docker pull ubuntu结果信息。

vbnet 复制代码
➜  ~ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

也可以从非官方仓库下载,需要在要拉取的镜像名称前指定完整的仓库地址,例如从阿里云容器镜像服务下载ubuntu:jammy镜像,这个时候需要指定完整仓库地址,

registry.cn-beijing.aliyuncs.com/hello/ubuntu:jammy

拉取镜像镜像命填写规则:

[Registry 仓库地址]/[NamesSpace 命名空间]/[Image Name 名称]/[Tag 标签]

  • Registry:是可选的,默认会从Docker Hub拉取。
  • NamesSpace:官方镜像时不需要填写,如自己上传的镜像,需要指定。
  • Image Name:必须指定。
  • Tag:如果不指定,默认为latest

以下是为官方镜像库拉取redis:bookworm镜像的信息。

makefile 复制代码
➜  ~ docker pull redis:bookworm
bookworm: Pulling from library/redis
8a1e25ce7c4f: Pull complete
954e4e27d2f0: Pull complete
db33f581d00e: Pull complete
ea5fd77426f5: Pull complete
a96dff3bfc07: Pull complete
6b9fa9c1b7ec: Pull complete
4f4fb700ef54: Pull complete
e1c78bd7c298: Pull complete
Digest: sha256:8482ee910acbcd8f31c5a08329e3903dbd2a018e2837897bfaf9bafb80b494b2
Status: Downloaded newer image for redis:bookworm
docker.io/library/redis:bookworm

8a1e25ce7c4f、954e4e27d2f0、db33f581d00e...,这些每一行开头的值即每一层的id。每一层本地只会存储一份,可以有效的缓解磁盘存储压力。

3. 列出镜像

查看本机一共有哪些镜像文件。

使用命令docker imagesdocker image ls,如下

复制代码
➜  ~ docker images
REPOSITORY                                      TAG                       IMAGE ID       CREATED        SIZE
ubuntu                                          jammy                     9d28ccdc1fc7   2 years ago    76.3MB
ubuntu                                          latest                    ba6acccedd29   2 years ago    72.8MB

过滤特定名称 比如ubuntu可以,使用docker images ubuntu:jammydocker image ls ubuntu:jammy

4. 删除镜像

删除掉已经不再使用的镜像。

使用命令docker rmi [image name:tag],tag省略为删除tag为latest的镜像文件。

makefile 复制代码
docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Deleted: sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1
Deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b

5. 打标签(创建副本)

如推送,需要打包为合适的名称,这个时候可以重新打一个标签,使其符合要求。

使用命令docker tag [old image name:old tag] [new image name;new tag]

复制代码
➜  ~ docker tag ubuntu:jammy ubuntu:hello
➜  ~ docker images
REPOSITORY                                      TAG                       IMAGE ID       CREATED        SIZE
ubuntu                                          hello                     9d28ccdc1fc7   2 years ago    76.3MB
ubuntu                                          jammy                     9d28ccdc1fc7   2 years ago    76.3MB

docker tag ubuntu:jammy ubuntu:hello创建了一个用ubuntu:jammy镜像,创建了一个新的镜像,标签为ubuntu:hello

6. 镜像打包tar和从tar包导入

有时候由于网络原因无法拉取镜像或备份镜像,可以找一台网络ok的主机进行镜像拉取操作,保存为tar包,通过离线复制到网络不佳的主机,进行导入。

使用命令docker save -o [文件名称] [镜像名称:标签]

复制代码
docker save alpine:3.19.1 -o hello.tar

此时使用ls命令可以看到生成的hello.tar文件,您可以将其复制到另一台已安装Docker的主机上并导入镜像:

使用命令从tar文件中导入镜像:

arduino 复制代码
➜  ~ docker load -i hello.tar
Loaded image: alpine:3.19.1

执行后,输出信息将显示已成功加载的镜像名称和标签。通过运行docker images命令,可以验证导入的镜像ID是否与之前导出的一致。

这样,您就能高效地管理和迁移Docker镜像,在不同环境中快速部署稳定一致的应用程序环境,并便于与他人共享镜像资源。

7. 检索镜像

可以用过Docker Hub网页,在线输入关键字进行检索,同时也可以通过终端,使用命令的形式进行检索。

通过Docker Hub检索

通过终端检索,使用命令docker search [镜像名称]。会显示与debian相关的镜像。

rust 复制代码
➜  ~ docker search debian
NAME                                  DESCRIPTION                                     STARS     OFFICIAL
ubuntu                                Ubuntu is a Debian-based Linux operating sys...   16983     [OK]
debian                                Debian is a Linux distribution that's compos...   4980      [OK]
neurodebian                           NeuroDebian provides neuroscience research s...   107       [OK]
bitnami/debian-base-buildpack         Debian base compilation image                   2
kasmweb/debian-bullseye-desktop       Debian Bullseye desktop for Kasm Workspaces     6
kasmweb/debian-bookworm-desktop       Debian Bookworm desktop for Kasm Workspaces     3
mirantis/debian-build-ubuntu-xenial                                                   0
rancher/debianconsole                                                                 1
mirantis/debian-build-ubuntu-trusty                                                   0
osrf/debian_arm64                     Debian arm64 Base Images                        1
dokken/debian-10                      Debian 10 image for use with kitchen-dokken     0
dokken/debian-9                       EOL: Debian 9 image for kitchen-dokken          0
dokken/debian-11                      Debian 11 image for use with kitchen-dokken     1
pihole/debian-base                    Debian docker with a custom pam build           5
ustclug/debian                        Official Debian Image with USTC Mirror          2
jitesoft/debian                       Debian base image.                              0
dokken/debian-8                       EOL: Debian 8 image for kitchen-dokken          1
dockette/debian                       My Debian Sid | Jessie | Wheezy Base Images     2
dokken/debian-12                      Debian 12 image for use with Test Kitchen's ...   0
corpusops/debian-bare                 https://github.com/corpusops/docker-images/     0
corpusops/debian                      debian corpusops baseimage                      0
pihole/debian-debootstrap             fork of multiarch/debian-debootstrap            0
homebrew/debian7                      Homebrew maintainer image for building patch...   0
osrf/debian_armhf                     Debian Armhf Base Images                        1
treehouses/debian

8. 查看镜像

docker inspect 用于获取Docker对象(如容器、镜像、网络、卷等)的详细信息。当你想要查看某个Docker镜像或容器的详细配置、元数据以及底层的JSON表示时,这个命令非常有用。

json 复制代码
➜  ~ docker inspect ubuntu:jammy
[
    {
        "Id": "sha256:9d28ccdc1fc782ec635c98e55ff68b05e6de1df2c7fcbbb4385f023368eec716",
        "RepoTags": [
            "ubuntu:hello",
            "ubuntu:jammy",
            "registry.cn-beijing.aliyuncs.com/ff755/ubuntu:jammy"
        ],
        "RepoDigests": [
            "ubuntu@sha256:f154feaf13b51d16e2b4b5575d69abc808da40c4b80e3a5055aaa4bcc5099d5b",
            "registry.cn-beijing.aliyuncs.com/ff755/ubuntu@sha256:3c3de9608507804525ff4303874525760ea36d62606e8105f515adaa761b80cb"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-12-04T02:21:12.903923136Z",
        "DockerVersion": "20.10.7",
        "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": [
                "bash"
            ],
            "Image": "sha256:c0ffde4acd3f60443d26c66a72e3f18f9da32f570a45ab54ef0d56a301a50150",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 76337050,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/c7b298e0facc365f18ee8feda4263232ea84026d65b57f35913c9b1e6d165722/merged",
                "UpperDir": "/var/lib/docker/overlay2/c7b298e0facc365f18ee8feda4263232ea84026d65b57f35913c9b1e6d165722/diff",
                "WorkDir": "/var/lib/docker/overlay2/c7b298e0facc365f18ee8feda4263232ea84026d65b57f35913c9b1e6d165722/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:950d1cd211572857ec798f05767ad7614025ec505d4e85df9d5e62662ed4fea9"
            ]
        },
        "Metadata": {
            "LastTagTime": "2024-04-10T03:53:58.389604988-04:00"
        }
    }
]

通过这些基本操作,大家可以高效地管理Docker镜像,搭建稳定、一致的应用环境,并方便的与他人协作分享。

忍不住要加个关注!不是我吹,但你会后悔没关注的!

相关推荐
Yiyi_Coding12 分钟前
在Windows系统安装Docker
windows·docker·容器
砍材农夫17 分钟前
番外篇-ollama一步一步docker安装部署
运维·docker·容器
江湖有缘17 分钟前
Docker部署EnBizCard教程:从零搭建专属开源数字名片平台
docker·容器·开源
问简9 小时前
docker 镜像相关
运维·docker·容器
Benszen10 小时前
Docker容器化技术实战指南
运维·docker·容器
Hommy8811 小时前
【开源剪映小助手】Docker 部署
docker·容器·开源·github·aigc
斯普信云原生组12 小时前
Prometheus 环境监控虚机 Redis 方案(生产实操版)
运维·docker·容器
喵了几个咪13 小时前
如何在 Superset Docker 容器中安装 MySQL 驱动
mysql·docker·容器·superset
工具罗某人13 小时前
docker compose部署kafka集群搭建
docker·容器·kafka
sbjdhjd19 小时前
Docker | 核心概念科普 + 保姆级部署
linux·运维·服务器·docker·云原生·面试·eureka