docker pull 命令介绍

docker pull 命令 : 从注册表拉取一个镜像或镜像仓库。

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

参数说明:

  • TAG:标签(版本);
  • DIGEST:摘要;
  • OPTIONS:
    • -a :下载镜像仓库中所有的指定镜像;
    • –disable-content-trust:跳过镜像验证(默认值是true);
    • –platform: 如果服务具有多平台功能,则设置平台;
    • -q:一直详细输出;

从Docker Hub拉取一个镜像

使用 docker pull 命令下载一个指定的镜像或一组镜像(例如,一个存储库),如果没有提供标签,Docker Engine 默认使用:latest标签。这个命令拉出最新的发行版镜像

[root@zhuzicc ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
69692152171a: Pull complete
30afc0b18f67: Pull complete
596b1d696923: Pull complete
febe5bd23e98: Pull complete
8283eee92e2f: Pull complete
351ad75a6cfa: Pull complete
Digest: sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest # nginx:latest 最新发行版

从以上示例代码中可以看出,Docker镜像分层存储的概念,镜像是由多层存储构成,下载过程也是一层层的进行操作。

下载结束后,给出了nginx镜像完整的sha256的摘要,以确保下载的一致性。

指定镜像tag标签拉取

image镜像tag标签版本可以通过Doker Hub进行查阅;

[root@zhuzicc ~]# docker pull nginx:1.20.1
1.20.1: Pulling from library/nginx
69692152171a: Already exists
94d185f62f0a: Pull complete
da9d3d3df3e3: Pull complete
1c11b8f3980d: Pull complete
541b1d41bb91: Pull complete
d8f6ef04dfa8: Pull complete
Digest: sha256:ba7669e39b8ec8b3ec9a7fba50a56dfc05c456388ee180ab263b498ee0b8724f
Status: Downloaded newer image for nginx:1.20.1
docker.io/library/nginx:1.20.1

以上示例中,通过指定nginx的tag标签来获取指定镜像。

在拉取过程中,因为nginx已下载过latest版本,所以 69692152171a 层已存在,下载 nginx:1.20.1 时就不需要重新下载,直接复用latest版本的69692152171a 层。

Docker官网的Examples中也详细的介绍了有关image(镜像)、 layers(层)、containers(容器)和可寻址存储的更多信息。

查看已下载镜像列表

可通过docker imagesdocker image ls 命令查看已下载镜像列表

[root@zhuzicc ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        1.20.1    993ef3592f66   3 weeks ago   133MB
nginx        latest    d1a364dc548d   3 weeks ago   133MB
[root@zhuzicc ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        1.20.1    993ef3592f66   3 weeks ago   133MB
nginx        latest    d1a364dc548d   3 weeks ago   133MB

已下载的镜像一般是存放在/var/lib/docker/image/overlay2目录下的 repositories.json文件中,不同Docker版本可能会有些不同,具体在这个/var/lib/docker目录下的什么位置还得根据Docker具体版本情况来看:

[root@zhuzicc ~]# cat /var/lib/docker/image/overlay2/repositories.json
{"Repositories":{"nginx":{"nginx:1.20.1":"sha256:993ef3592f664cf081693f642b93fa65e57d11183f93abc3bed6c274974e3df8","nginx:latest":"sha256:d1a364dc548d5357f0da3268c888e1971bbdb957ee3f028fe7194f1d61c6fdee","nginx@sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750":"sha256:d1a364dc548d5357f0da3268c888e1971bbdb957ee3f028fe7194f1d61c6fdee","nginx@sha256:ba7669e39b8ec8b3ec9a7fba50a56dfc05c456388ee180ab263b498ee0b8724f":"sha256:993ef3592f664cf081693f642b93fa65e57d11183f93abc3bed6c274974e3df8"}}}

JSON解析查看一下:

在这里插入图片描述

通过摘要拉取镜像

如果不希望镜像更新到新的版本,如1.20.1->1.21.1,而是想继续使用当前版本镜像的最新版,比如:当前使用的是nginx:1.20.1镜像,想把nginx:1.20.1镜像升级到1.20.1这个版本的最新版。就可以通过镜像DIGEST来拉取镜像,通过摘要拉取镜像时,需要指定镜像版本来锁定目标版本镜像。

我们可以通过 docker image inspect 镜像id 命令来查看已下载镜像的详情信息,以此来获取镜像摘要。
在这里插入图片描述

# 1.查询镜像列表
# 2.查看指定镜像详情信息,获取镜像摘要
# 3.通过摘要下载更新当前镜像

# ############ 测试示例 ############
[root@iZbp1953kwfhu01b3ehohdZ ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        1.20.1    993ef3592f66   3 weeks ago   133MB
nginx        latest    d1a364dc548d   3 weeks ago   133MB
[root@iZbp1953kwfhu01b3ehohdZ ~]# docker image inspect 993ef3592f66
[
    {
        "Id": "sha256:d1a364dc548d5357f0da3268c888e1971bbdb957ee3f028fe7194f1d61c6fdee",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750"
        ],
# ...省略
[root@iZbp1953kwfhu01b3ehohdZ ~]# docker pull nginx@sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750
docker.io/library/nginx@sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750: Pulling from library/nginx
Digest: sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750
Status: Image is up to date for nginx@sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750
docker.io/library/nginx@sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750

操作完成,可以看到下载提示当前镜像已是最新镜像

关于摘要的使用,也可以在Dockerfile的FROM子句后,例如:

[root@zhuzicc]# vim Dockerfile
[root@zhuzicc]# cat Dockerfile
FROM nginx@sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750
LABEL maintainer="some maintainer <zhuzicc@example.com>"

从其它注册中心拉取

docker pull命令默认从Docker Hub中获取镜像。同时,Docker也可以手动指定要拉取的镜像注册中心路径。例如,如果已经设置了一个本地注册中心,就可以指定本地注册中心的路径。注册中心路径类似于URL,但不包含协议说明符(https://)。下面的命令从监听5000端口(myregistry.local:5000)的本地注册中心中获取testing/test-image映像:

docker pull myregistry.local:5000/testing/test-image
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐