Skip to content

Docker image

映像(image)是一个只读模版(read-only template),其中包含了创建 Docker 容器的说明。

Tips

image 是分层构造的,这样能够实现同样层的复用,需要注意 image 每层都是只读的。实际上 Container 就是在 image 的基础上添加了一个可写层。

子命令

通过 docker image 命令能够管理映像,它包含下面一些子命令:

命令 描述
docker image history 显示 image 的历史记录
docker image import 从 tarball 导入内容以创建 filesytem image
docker image inspect 显示 image 的详细信息
docker image load 从 tar 中加载 image
docker image prune 删除没有使用的 image
docker image rm 删除一个 image
docker image save 将一个或多个 image 保存到 tar 中
docker image tag 为 image 创建标记
docker image ls 列出 image
docker image pull 从 registry 中下载 image
docker image push 将 image 上传到 registry 上

image references

映像具有名称和版本:

Bash
image[:TAG|@DIGEST]

其中 tag 表示是映像版本,默认就是 latest。也可以指定特定的版本,例如 ubuntu:23.10

还有一种以 sha256 字符串的形式来指定特定的映像:

Bash
alpine@sha256:9cacb71397b640eca97488cf08582ae4e4068513101088e9f96c9814bfda95e0

拉取映像

使用docker image pull命令来拉取映像:

Bash
# 别名 docker pull
docker image pull [OPTIONS] NAME[:TAG|@DIGEST]

该命令用于从 registery 中拉取镜像,默认是从Docker Hub上拉取:

Bash
$ docker image pull debian

Using default tag: latest
latest: Pulling from library/debian
e756f3fdd6a3: Pull complete
Digest: sha256:3f1d6c17773a45c97bd8f158d665c9709d7b29ed7917ac934086ad96f92e4510
Status: Downloaded newer image for debian:latest
docker.io/library/debian:latest

Docker 中映像可以由多个层(layers)构成,上面的 debian 只包含一层就是 e756f3fdd6a3。该层(layers)能够被重复利用,任何其他使用该层的映像不会在下载改层了。

列出映像

使用docker image ls 来列出映像:

Bash
# 别名 docker images
docker image ls

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
<none>                    <none>              77af4d6b9913        19 hours ago        1.089 GB
committ                   latest              b6fa739cedf5        19 hours ago        1.089 GB
<none>                    <none>              78a85c484f71        19 hours ago        1.089 GB
docker                    latest              30557a29d5ab        20 hours ago        1.089 GB
<none>                    <none>              5ed6274db6ce        24 hours ago        1.089 GB
postgres                  9                   746b819f315e        4 days ago          213.4 MB
postgres                  9.3                 746b819f315e        4 days ago          213.4 MB
postgres                  9.3.5               746b819f315e        4 days ago          213.4 MB
postgres                  latest              746b819f315e        4 days ago          213.4 MB

其中比较特殊的就是名称为 <none> 的映像,他们被称为悬空映像(dangling images)。

参考