Docker pull拉取镜像命令的入门教程

1. 基础概念

Docker镜像 是构建容器的只读模板,存储在仓库(如Docker Hub)中。docker pull用于从仓库下载镜像到本地。

2. 命令语法

bash 复制代码
docker pull [选项] <镜像名:标签>
  • 镜像名 :格式为[仓库地址/]命名空间/镜像名(默认仓库为Docker Hub)
  • 标签 :指定版本(如latest, 1.0.0),默认latest

3. 基础操作

拉取官方镜像
bash 复制代码
docker pull nginx  # 等价于 docker pull library/nginx:latest
拉取指定版本
bash 复制代码
docker pull nginx:1.25.3
拉取私有仓库镜像
bash 复制代码
# 登录私有仓库
docker login registry.example.com

# 拉取镜像
docker pull registry.example.com/myapp:v2

4. 高级用法

多架构镜像
bash 复制代码
# 拉取支持多种架构的镜像(如amd64/arm64)
docker pull --platform linux/amd64 nginx:latest
禁用缓存
bash 复制代码
docker pull --disable-content-trust=false nginx  # 强制校验镜像签名
进度条控制
bash 复制代码
docker pull --quiet nginx  # 静默模式

5. 验证镜像

bash 复制代码
# 查看本地镜像列表
docker images

# 检查镜像详细信息
docker inspect nginx:latest

6. 常见问题

问题1:镜像拉取失败
log 复制代码
Error response from daemon: manifest for nginx:latest not found

解决方案:

  • 检查镜像名拼写
  • 尝试指定完整路径:docker pull library/nginx
  • 清除本地缓存:docker system prune -a
问题2:权限不足
log 复制代码
Error response from daemon: Get https://registry.example.com/v2/: unauthorized

解决方案:

bash 复制代码
docker login registry.example.com
# 或使用--username参数
docker pull --username=yourname registry.example.com/myapp
问题3:网络超时
log 复制代码
Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout

解决方案:

  • 配置镜像加速器(如阿里云):

    json 复制代码
    {
      "registry-mirrors": ["https://<你的ID>.mirror.aliyuncs.com"]
    }

    保存至/etc/docker/daemon.json后重启服务:

    bash 复制代码
    sudo systemctl restart docker

7. 最佳实践

  1. 明确指定标签 :避免使用latest标签,改用语义化版本(如v1.2.3)

  2. 定期清理无用镜像 :

    bash 复制代码
    docker image prune -a  # 删除未使用的镜像
  3. 使用镜像扫描 :

    bash 复制代码
    docker scan nginx:latest  # 检查漏洞

8. 示例流程

bash 复制代码
# 1. 搜索可用镜像
docker search nginx

# 2. 拉取指定版本
docker pull nginx:1.25.3

# 3. 验证镜像
docker images | grep nginx

# 4. 运行容器测试
docker run -d --name web nginx:1.25.3
相关推荐
fb_123451 小时前
MySQL运维实战:备份恢复+主从复制+读写分离+MHA高可用(超详细手把手教程)
运维·mysql·oracle
..Dauntless..2 小时前
【Linux】进程地址空间初步理解
linux·运维·服务器
Julien20042 小时前
Docker 网络(一)
linux·运维·服务器·ssh·学习方法
字节跳动的猫2 小时前
LikeShop 积分体系全链路改造:获取规则、订单抵扣与积分商品兑换二开
运维·数据结构
杨云龙UP2 小时前
一次数据库查询缓慢故障复盘:大表数据增长、SQL全表扫描导致系统响应异常
linux·运维·服务器·数据库·sql·mysql
I'mChloe3 小时前
Windows部署BiliNote:Docker安装、AI视频转写、Markdown笔记与cpolar远程访问
人工智能·windows·docker
PellyKoo3 小时前
【linux运维】ubuntu+samba 用户组独占目录权限配置踩坑记
linux·运维·ubuntu
wtblszn3 小时前
空压机在线监测物联网方案
大数据·运维·物联网·自动化·能源
AR-26710-3 小时前
Linux Day15——系统管理复习
linux·运维
吴声子夜歌4 小时前
Nginx应用与运维——Nginx HTTP模块详解(动态赋值功能模块)
运维·nginx·http