docker pull

命令

docker pull

描述

docker pull 从镜像仓库拉取镜像到本地。

使用的大多数镜像都是基于Docker Hub上的基础镜像制作的。

Docker Hub上包含许多预先构建好的镜像,你可以下载并使用它们,而不需要自己去定义和配置镜像。

docker pull 的镜像如果不是公共开放的,还需要使用 docker login 提供认证信息。

默认docker使用https://的方式访问镜像仓库,如果不是,需要配置 ` insecure registries` 。

  • 代理配置 如果您的项目处于一些企业服务器中,您可能需要使用环境变量:HTTP_PROXY, HTTPS_PROXY, and NO_PROXY 设置Docker守护进程的服务代理。可以通过systemd来设置这些环境变量,参考:systemd config https://docs.docker.com/config/daemon/systemd/

  • 并行下载 默认情况下,Docker守护程序将一次下载三层镜像。 如果您的带宽比较低,可能会导致超时问题。您可以通过选项 --max-concurrent-downloads 来提高下载速度。

帮助

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)
      --platform string Set platform if server is multi-platform capable

选项

  • –all-tags, a 下载一个仓库中所有镜像tag。即下载一个镜像的所有版本。

  • –disable-content-trust true 跳过镜像校验

  • –platform 设置镜像所属平台, 如果有多个镜像服务.

示例

拉取一个或多个镜像使用docker pull,如果没有指定镜像标签,docker默认使用当前最新的tag标记 :latest,下面的示例命令就会拉取最新的镜像文件,等同于:docker pull debian:latest

$ docker pull debian

Using default tag: latest
latest: Pulling from library/debian
fdd5d7827f33: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:e7d38b3517548a1c71e41bffe9c8ae6d6d29546ce46bf62159837aad072c90aa
Status: Downloaded newer image for debian:latest

docker镜像基本都是由多层组成(分层存储),上面的镜像文件就是由两层构成:fdd5d7827f33和a3ed95caeb02.

每一层都可以被不同镜像共同使用,例如下面这个镜像debian:jessie就是与上述debian:latest共用两个相同的镜像层。在拉取debian:jessie的时候只会拉取其自身的一些元数据信息,而不会再去拉取镜像分层信息,因为在本地已经存在了这两个镜像层。

$ docker pull debian:jessie

jessie: Pulling from library/debian
fdd5d7827f33: Already exists
a3ed95caeb02: Already exists
Digest: sha256:a9c958be96d7d40df920e7041608f2f017af81800ca5ad23e327bc402626b58e
Status: Downloaded newer image for debian:jessie

要查看本地存在哪些图像,使用 docker images 命令:

$ docker images

REPOSITORY   TAG      IMAGE ID        CREATED      SIZE
debian       jessie   f50f9524513f    5 days ago   125.1 MB
debian       latest   f50f9524513f    5 days ago   125.1 MB

Docker使用 内容寻址图像存储,并且图像ID是SHA256摘要,覆盖了图像的配置和层。在上面的示例中, debian:jessiedebian:latest 具有相同的图像ID,因为它们实际上是用不同名称标记的同一图像。因为它们是同一映像,所以它们的层仅存储一次,并且不占用额外的磁盘空间。

通过sha256数字摘要拉取镜像

可以图像名称(和 tag )提取了图像,使用名称和标签是一种便捷方法。使用标签时,您可以再次docker pull 镜像以确保您具有该图像的最新版本。例如,docker pull ubuntu:14.04 提取最新版本的Ubuntu 14.04映像。

在某些情况下,您不希望将图像更新为较新的版本,而是希望使用镜像的固定版本。Docker允许按其摘要提取映像 。当按摘要下载镜像时,需要明确指定要下载的镜像版本。这样做可以将镜像“固定”到该版本,并确保所使用的图像始终相同。

要了解镜像的sha256摘要,可以先下载镜像。 让我们ubuntu:14.04从Docker Hub中获取最新 映像:

$ docker pull ubuntu:14.04

14.04: Pulling from library/ubuntu
5a132a7e7af1: Pull complete
fd2731e4c50c: Pull complete
28a2f68d1120: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2
Status: Downloaded newer image for ubuntu:14.04

Docker下载完成后会显示出摘要,上面这个示例的摘要就是

sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2

下载镜像时也可以使用摘要进行下载

$ docker pull ubuntu@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2

sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2: Pulling from library/ubuntu
5a132a7e7af1: Already exists
fd2731e4c50c: Already exists
28a2f68d1120: Already exists
a3ed95caeb02: Already exists
Digest: sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2
Status: Downloaded newer image for ubuntu@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2

摘要也可以在FROMDockerfile中使用,例如:

FROM ubuntu@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2
MAINTAINER some maintainer <maintainer@example.com>

重其他镜像仓库拉取镜像

默认情况 docker pull 是从 Docker Hub 镜像仓库下载镜像,也可以使用一个其他的镜像仓库。 例如,从 myregistry.local:5000 下载镜像

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

下载镜像的多个tag

默认情况下,docker pull 只会下载一个仓库中的一个镜像版本。如果有需要,可以使用参数 -a--all-tags 下载镜像的所有的版本。

例如,下载一个 fedora 的所有镜像

$ docker pull --all-tags fedora

Pulling repository fedora
ad57ef8d78d7: Download complete
105182bb5e8b: Download complete
511136ea3c5a: Download complete
73bd853d2ea5: Download complete
....

Status: Downloaded newer image for fedora

镜像下载完成后,可以使用 docker images 命令查看镜像。

$ docker images fedora

REPOSITORY TAG IMAGE ID CREATED SIZE
fedora rawhide ad57ef8d78d7 5 days ago 359.3 MB
fedora 20 105182bb5e8b 5 days ago 372.7 MB
fedora heisenbug 105182bb5e8b 5 days ago 372.7 MB
fedora latest 105182bb5e8b 5 days ago 372.7 MB

终止下载

终止 docker pull 的过程,可以使用 CTRL-c 快捷键。

$ docker pull fedora

Using default tag: latest
latest: Pulling from library/fedora
a3ed95caeb02: Pulling fs layer
236608c7b546: Pulling fs layer
^C

从技术上讲,当Docker Engine守护程序与发起该拉取的Docker Engine客户端之间的连接丢失时,引擎会终止拉取操作。如果与Engine守护程序的连接由于手动交互以外的其他原因而丢失,则拉取操作也会中止。