Podman 教程

Podman 为镜像添加标签

在 Podman 中,可以使用 podman tag 命令为镜像添加标签,为镜像添加标签的核心目的是精准定位、管理镜像版本,避免使用时混淆。总结起来有如下四个原因:

  • 区分镜像版本:同一镜像的不同构建版本(如 v1.0、v2.0)可通过标签区分,避免误用旧版本或不稳定版本。

  • 简化镜像引用:标签(如“latest”、“prod”)比冗长的镜像 ID 更易记忆和输入,使用时无需手动复制完整 ID。

  • 明确镜像用途:可通过标签标注镜像场景(如 “test”、“dev”、“prod”),快速识别镜像的使用环境。

  • 支持镜像分发:发布镜像时,标签是版本管理的核心,方便他人按需求拉取指定版本,而非默认最新版。

  

基础语法

podman tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

参数说明:

  • SOURCE_IMAGE[:TAG]:源镜像的标识符,可以是:

    • 镜像名称 + 标签(如 nginx:latest)

    • 镜像的完整 ID(如 56bab49eef2ef72e477c3beeb357a0c2a3bc3a1c8e6c18db69109e34a866c862)

    • 镜像的短 ID(如 56bab49eef2e)

  • TARGET_IMAGE[:TAG]:目标标签,可以是:

    • 新的名称和标签(如 my-nginx:v1.0)

    • 用于仓库推送的完整地址(如 docker.io/username/my-nginx:v1.0)

点击查看 podman tag 命令手册

  

常用示例

(1)为镜像添加版本标签:

C:\Users\Administrator> podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
docker.io/library/nginx  latest      60adc2e137e7  28 hours ago  155 MB

# 基于 nginx:latest 创建标签 nginx:v1.25
C:\Users\Administrator> podman tag nginx:latest nginx:v1.25

C:\Users\Administrator> podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
localhost/nginx          v1.25       60adc2e137e7  28 hours ago  155 MB
docker.io/library/nginx  latest      60adc2e137e7  28 hours ago  155 MB

(2) 为镜像添加仓库地址标签,用于推送:

C:\Users\Administrator> podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
docker.io/library/nginx  latest      60adc2e137e7  28 hours ago  155 MB

# 为本地镜像添加远程仓库标签,准备推送
C:\Users\Administrator> podman tag nginx:latest docker.io/myusername/nginx:prod

C:\Users\Administrator> podman images
REPOSITORY                  TAG         IMAGE ID      CREATED       SIZE
docker.io/myusername/nginx  prod        60adc2e137e7  28 hours ago  155 MB
docker.io/library/nginx     latest      60adc2e137e7  28 hours ago  155 MB

(3)通过镜像 ID 添加标签:

# 使用镜像ID(部分ID即可)创建标签
C:\Users\Administrator> podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
docker.io/library/nginx  latest      60adc2e137e7  28 hours ago  155 MB

C:\Users\Administrator> podman tag 60adc2e137e7 my-nginx:test

C:\Users\Administrator> podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
localhost/my-nginx       test        60adc2e137e7  28 hours ago  155 MB
docker.io/library/nginx  latest      60adc2e137e7  28 hours ago  155 MB

(4)省略标签时默认使用 latest:

# 等同于 nginx:latest → my-nginx:latest
C:\Users\Administrator> podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
docker.io/library/nginx  latest      60adc2e137e7  28 hours ago  155 MB

C:\Users\Administrator> podman tag nginx my-nginx

C:\Users\Administrator> podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
localhost/my-nginx       latest      60adc2e137e7  28 hours ago  155 MB
docker.io/library/nginx  latest      60adc2e137e7  28 hours ago  155 MB

更多内容请参考官方文档。

  

说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
其他应用
公众号