Docker 使用以下几种方式标识镜像,每种方式各有特点和适用场景:
sha256:a3b5...
),实际使用时通常缩写为前12位(如 a3b5c80d4f2a
)。docker run a3b5c80d4f2a
)。<repository>:<tag>
(如 ubuntu:20.04
或 nginx:latest
)。1.0
)、环境(如 prod
)或别名(如 latest
)。latest
是默认标签,但不推荐生产环境使用(可能意外更新)。docker pull python:3.9
)。backend:v1
vs frontend:debug
)。<repository>@sha256:<哈希值>
(如 ubuntu@sha256:45b23...
)。<image>:<tag>
(如 redis
)。<registry>/<project>/<image>:<tag>
(如 myregistry.example.com/prod/api:1.0
)。docker push myregistry/app:latest
)。标识方式 | 示例 | 可变性 | 适用场景 |
---|---|---|---|
镜像ID | a3b5c80d4f2a |
❌ 不可变 | 精确操作特定镜像 |
标签 | nginx:1.23-alpine |
✅ 可覆盖 | 版本管理、开发环境 |
摘要 | ubuntu@sha256:45b23... |
❌ 不可变 | 生产环境、安全敏感场景 |
仓库路径 | myregistry/app:v2 |
✅ 可修改 | 私有仓库或自定义镜像源 |
# 通过标签拉取镜像
docker pull nginx:1.23
# 通过镜像ID运行容器
docker run -d a3b5c80d4f2a
# 查看镜像摘要
docker inspect --format='{{.RepoDigests}}' nginx:1.23
# 使用摘要拉取镜像
docker pull ubuntu@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269e113568c5cff48b9...
v1.2.3
),避免 latest
。