1、一个分层存储的文件
2、一个软件的环境
3、一个镜像可以创建N个容器
4、一种标准化的交付
5、一个不包含Linux内核而又精简的Linux操作系统
镜像不是一个单一的文件,而是有多层构成。我们可以通过docker history <ID/NAME>查看镜像中各层内容及大小,每层对应着Dockerfile中的一条指令。Docker镜像默认存储在/var/lib/docker/\<storage-driver\>中。
Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。
命令检索centos镜像:docker search centos
地址: https://hub.docker.com/explore
镜像加速:
很多镜像都在国外。比如 gcr 。国内下载很慢,需要加速。https://github.com/DaoCloud/public-image-mirror
使用方法
增加前缀 (推荐方式)。比如:
docker.io/library/busybox
|
V
m.daocloud.io/docker.io/library/busybox
或者 支持的镜像仓库 的 前缀替换 就可以使用。比如:
docker.io/library/busybox
|
V
docker.m.daocloud.io/library/busybox
前缀替换的 Registry 的规则, 这是人工配置的, 有需求提 Issue.
除了这里每一个源站, 内容都是不同的, 不要把 docker.io 之外的站点配置给 registry-mirrors
| 源站 | 替换为 | 备注 |
|---|---|---|
docker.elastic.co |
elastic.m.daocloud.io |
|
docker.io |
docker.m.daocloud.io |
|
gcr.io |
gcr.m.daocloud.io |
|
ghcr.io |
ghcr.m.daocloud.io |
|
k8s.gcr.io |
k8s-gcr.m.daocloud.io |
k8s.gcr.io 已被迁移到 registry.k8s.io |
registry.k8s.io |
k8s.m.daocloud.io |
|
mcr.microsoft.com |
mcr.m.daocloud.io |
|
nvcr.io |
nvcr.m.daocloud.io |
|
quay.io |
quay.m.daocloud.io |
|
registry.ollama.ai |
ollama.m.daocloud.io |
实验内测中,使用方法 |
Usage: docker image COMMAND
docker image —help
| 指令 | 描述 |
|---|---|
ls |
列出镜像 |
build |
构建镜像来自 Dockerfile |
history |
查看镜像历史 |
pull |
从镜像仓库拉取镜像 |
push |
推送一个镜像到镜像仓库 |
rm |
移除一个或多个镜像 |
prune |
移除未使用的镜像。没有被标记或被任何容器引用的。 |
tag |
创建一个引用源镜像标记目标镜像 |
export |
导出容器文件系统到 tar 归档文件 |
import |
导入容器文件系统 tar 归档文件创建镜像 |
save |
保存一个或者多个镜像到 tar 归档文件 |
load |
加载镜像来自 tar 归档或标准输入 |
# 基础拉取(默认从 Docker Hub) docker pull nginx:1.25 # 指定 Registry(如daocloud) docker pull m.daocloud.io/docker.io/library/nginx:1.25 # 拉取时禁用缓存(确保获取最新层) docker pull --no-cache nginx:1.25 # 拉取私有镜像(需先登录) docker login registry.example.com docker pull registry.example.com/team/app:1.0
关键参数:
--no-cache:避免使用本地缓存,强制拉取最新镜像(适合 CI/CD)。--platform:指定平台(如 linux/arm64,多架构场景)。# 基础构建(Dockerfile 在当前目录) docker build -t myapp:1.0 . # 指定 Dockerfile 路径和构建上下文 docker build -t myapp:1.0 -f ./path/Dockerfile /build/context # 多阶段构建(优化镜像大小) docker build -t myapp:1.0 --target=prod-stage . # 构建时传递变量(ARG) docker build -t myapp:1.0 --build-arg ENV=prod .
关键参数:
-f:指定非默认的 Dockerfile路径。--target:多阶段构建时选择阶段(如 dev/prod)。--build-arg:传递构建时变量(如环境配置)。# 查看本地镜像(含大小排序)
docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}" | sort -k4 -h
# 删除镜像(强制删除悬空镜像)
docker rmi $(docker images -f "dangling=true" -q) # 清理 <none> 镜像
docker rmi -f nginx:1.25 # 强制删除(即使容器在使用)
# 导出/导入镜像(离线迁移)
docker save -o nginx.tar nginx:1.25
docker load -i nginx.tar
# 打标签(用于推送私有仓库)
docker tag nginx:1.25 registry.example.com/team/nginx:1.25
SRE 建议:
docker image prune -a),但需排除生产镜像。--format自定义输出,便于脚本处理。