07_dockerfile/7.14_label.md
LABEL <key>=<value> <key>=<value> ...
LABEL 指令以键值对的形式给镜像添加元数据。这些数据不会影响镜像的功能,但可以帮助用户理解镜像,或被自动化工具使用。
LABEL version="1.0"
LABEL description="这是一个 Web 应用服务器"
LABEL maintainer="[email protected]" \
version="1.2.0" \
description="My App Description" \
org.opencontainers.image.authors="Yeasy"
💡 包含空格的值需要用引号括起来。
为了标准和互操作性,推荐使用 OCI Image Format Specification 定义的标准标签:
| 标签 Key | 说明 | 示例 |
|---|---|---|
org.opencontainers.image.created | 构建时间(RFC 3339) | 2024-01-01T00:00:00Z |
org.opencontainers.image.authors | 作者/维护者 | [email protected] |
org.opencontainers.image.url | 项目主页 | https://example.com |
org.opencontainers.image.documentation | 文档地址 | https://example.com/docs |
org.opencontainers.image.source | 源码仓库 | https://github.com/user/repo |
org.opencontainers.image.version | 版本号 | 1.0.0 |
org.opencontainers.image.licenses | 许可证 | MIT |
org.opencontainers.image.title | 镜像标题 | My App |
org.opencontainers.image.description | 描述 | Production ready web server |
LABEL org.opencontainers.image.authors="yeasy" \
org.opencontainers.image.documentation="https://yeasy.gitbooks.io" \
org.opencontainers.image.source="https://github.com/yeasy/docker_practice" \
org.opencontainers.image.licenses="MIT"
旧版本的 Dockerfile 中常看到 MAINTAINER 指令:
## ❌ 已弃用
MAINTAINER [email protected]
现在推荐使用 LABEL:
## ✅ 推荐
LABEL maintainer="[email protected]"
## 或
LABEL org.opencontainers.image.authors="[email protected]"
配合 ARG 使用,可以在构建时动态注入标签:
ARG BUILD_DATE
ARG VCS_REF
LABEL org.opencontainers.image.created=$BUILD_DATE \
org.opencontainers.image.revision=$VCS_REF
构建命令:
$ docker build \
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
--build-arg VCS_REF=$(git rev-parse --short HEAD) \
.
查看镜像的标签信息:
$ docker inspect nginx --format '{{json .Config.Labels}}' | jq
{
"maintainer": "NGINX Docker Maintainers <[email protected]>"
}
可以使用标签过滤镜像:
## 列出作者是 yeasy 的所有镜像
$ docker images --filter "label=org.opencontainers.image.authors=yeasy"
## 删除所有带有特定标签的镜像
$ docker rmi $(docker images -q --filter "label=stage=builder")