基础命令
最基本、最常用的命令格式如下:

docker pull [镜像名]:[标签]
[镜像名]:通常是用户名/软件名的格式,对于官方镜像,用户名可以省略,直接是软件名。[标签]:这是可选的,用于指定镜像的版本,如果不指定,Docker 默认会拉取latest标签的镜像。
示例
-
拉取官方
nginx镜像的latest版本 如果不写标签,默认就是latest。docker pull nginx
等同于:
docker pull nginx:latest
-
拉取指定版本的
nginx镜像 标签通常是版本号,如25.1。强烈推荐使用具体版本号,而不是latest,以确保环境的可复现性。docker pull nginx:1.25.1
-
拉取非官方(社区或个人)镜像 拉取一个用户
myuser发布的myapp镜像。
(图片来源网络,侵删)docker pull myuser/myapp:v2.3
命令详解
我们可以使用 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 false)
--platform string Set the platform if the server is multi-platform capable
--progress string Set type of progress output (auto, tty, plain, quiet) (default "auto")
-q, --quiet Suppress verbose output
主要参数解析
-
-a, --all-tags- 作用:拉取指定仓库中的所有标签(所有版本)的镜像。
- 示例:拉取官方
nginx仓库中的所有镜像。docker pull -a nginx
注意:这会下载大量数据,请谨慎使用,确保你的磁盘空间足够。
-
--platform string- 作用:指定拉取的镜像平台,这对于多架构镜像(如同时支持
linux/amd64和linux/arm64)非常有用。 - 示例:明确拉取
linux/amd64(x86 架构)版本的镜像。docker pull --platform linux/amd64 nginx:latest
如何查看本地支持的平台?
docker version
在输出中找到
Server和Client的Platform信息。
- 作用:指定拉取的镜像平台,这对于多架构镜像(如同时支持
-
-q, --quiet- 作用:静默模式,只输出镜像 ID,不显示下载的详细过程,非常适合在脚本中使用。
- 示例:
docker pull -q nginx:1.25.1
输出可能类似于:
sha256:a9d65a43b5e61be...
-
--progress string- 作用:控制进度条的显示方式。
- 可选值:
auto(默认): 自动选择,在终端中显示进度条,在非交互式环境中静默。tty: 强制显示进度条。plain: 以纯文本形式显示进度。quiet: 静默模式,与-q类似,只输出最终结果。
- 示例:以纯文本形式显示下载进度。
docker pull --progress plain nginx:1.25.1
进阶用法与技巧
从私有仓库拉取镜像
如果你的镜像存储在私有仓库(如 Harbor, AWS ECR, Google GCR 等),你需要先登录到该仓库。
# 1. 登录到私有仓库 docker login your-private-registry.com -u your_username -p your_password # 2. 拉取镜像 docker pull your-private-registry.com/your-project/your-image:v1.0
拉取镜像后重命名/打标签
有时候你拉取的镜像名字太长,或者想在本地给它一个别名,可以使用 docker tag 命令。
# 1. 先拉取一个长名字的镜像 docker pull library/ubuntu:22.04 # 2. 为它创建一个本地别名 "my-ubuntu" docker tag library/ubuntu:22.04 my-ubuntu:latest # 3. 验证 docker images | grep my-ubuntu
docker tag的语法是docker tag [源镜像] [新镜像]。
使用镜像摘要拉取
为了确保你拉取的镜像是完全一致的版本(不受标签被覆盖的影响),可以使用镜像摘要。
- 摘要:是镜像内容的唯一哈希值,一旦生成就不会改变。
操作步骤:
-
首先拉取一次镜像,以便获取其摘要信息。
docker pull ubuntu:22.04
-
查看镜像的详细信息,找到
Digest。docker image inspect ubuntu:22.04
在输出中找到类似这样的行:
"RepoDigests": [ "ubuntu@sha256:5b4c4caedd5b015ca8d9d0841a1435194c3b75fb0b4214f9bae0b520c7340fe1" ]sha256:...这部分就是摘要。 -
使用摘要拉取。
docker pull ubuntu@sha256:5b4c4caedd5b015ca8d9d0841a1435194c3b75fb0b4214f9bae0b520c7340fe1
常见问题与解决方案
问题 1:Error response from daemon: Get "https://registry-1.docker.io/v2/...": denied: access denied public
- 原因:通常是由于网络问题或 Docker Hub 的访问限制,在中国大陆地区,直接访问 Docker Hub 可能会很慢或失败。
- 解决方案:配置国内镜像加速器。
- 创建或编辑配置文件
/etc/docker/daemon.json(Linux/macOS) 或%PROGRAMDATA%\docker\config\daemon.json(Windows)。 - 添加以下内容(这里以阿里云、网易云、中科大等为例):
{ "registry-mirrors": [ "https://<你的加速器地址>.mirror.aliyuncs.com", "http://hub-mirror.c.163.com", "https://mirror.baidubce.com" ] } - 保存文件后,重启 Docker 服务。
# Linux sudo systemctl restart docker
- 创建或编辑配置文件
问题 2:Error: No such image: ...
- 原因:镜像名或标签拼写错误,或者该镜像确实不存在。
- 解决方案:
- 检查拼写是否正确,包括大小写。
- 访问 Docker Hub 搜索确认镜像是否存在。
- 如果是私有仓库,确认你有权限访问。
问题 3:Error: remote image ... does not exist or no pull access
- 原因:
- 镜像仓库中不存在该标签。
- 你正在尝试拉取一个私有镜像,但没有先登录 (
docker login)。
- 解决方案:
- 检查标签是否正确。
- 如果是私有仓库,先执行
docker login。
问题 4:如何查看本地已有的镜像?
使用 docker images 或 docker image ls 命令。
docker images
希望这份详细的指南能帮助您熟练掌握 Docker 镜像的下载!
