Featured image of post Podman 镜像管理完全指南 📦

Podman 镜像管理完全指南 📦

Podman 镜像管理完全指南 📦 🔍 全面掌握 Podman 镜像管理的专业技巧,从基础操作到

Podman 镜像管理完全指南 📦

Podman 容器管理

🔍 全面掌握 Podman 镜像管理的专业技巧,从基础操作到高级优化


📖 目录导航


✨ Podman 简介与特点

Podman 是一个开源的容器运行时工具,旨在替代 Docker,具有以下优秀特性:

  • 🔒 无守护进程:不需要后台守护进程,更加安全可靠
  • 🐧 Rootless 运行:支持非特权用户运行容器,提升安全性
  • 🔄 兼容 Docker:大部分命令与 Docker 兼容,学习成本低
  • 🌐 开源免费:完全开源,无商业限制
  • ⚡ 轻量高效:资源占用少,启动速度快
  • 🛡️ 安全可靠:采用传统的 fork-exec 模型,减少攻击面
  • 📦 镜像兼容:完全支持 OCI 标准镜像格式

🎯 适用场景

  • 开发环境容器化
  • 生产环境容器部署
  • CI/CD 流水线
  • 本地开发和测试
  • 多平台容器构建

📋 一、镜像管理基础命令

1️⃣ 查看本地镜像列表

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 查看所有本地镜像
podman images

# 查看镜像的详细信息
podman images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"

# 按仓库名过滤查看
podman images --filter reference="docker.io/*"

# 按日期排序查看
podman images --sort created

# 只显示镜像ID
podman images -q

2️⃣ 搜索远程镜像

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 从仓库搜索镜像
podman search nginx

# 搜索并显示官方镜像
podman search --filter is-official=true nginx

# 搜索并限制显示数量
podman search --limit 5 nginx

# 搜索并显示所有标签
podman search --list-tags nginx

# 按星级搜索
podman search --filter stars=100 nginx

3️⃣ 拉取远程镜像

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 拉取最新版本的镜像
podman pull docker.io/openlistteam/openlist:latest

# 拉取指定版本的镜像
podman pull docker.io/library/nginx:1.23

# 拉取镜像但不验证证书(适用于内部仓库)
podman pull --tls-verify=false localhost:5000/myimage

# 拉取所有标签的镜像
podman pull --all-tags docker.io/library/nginx

# 显示拉取进度详情
podman pull --progress=true docker.io/library/nginx

4️⃣ 删除本地镜像

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 删除指定镜像(通过ID)
podman rmi 70bbf14f6340

# 强制删除镜像(即使有容器使用)
podman rmi -f 70bbf14f6340

# 删除指定仓库的镜像
podman rmi docker.io/openlistteam/openlist:latest

# 删除所有未使用的镜像
podman image prune -af

# 删除指定标签的镜像
podman rmi docker.io/library/nginx:1.23

🧹 二、镜像清理与维护

1️⃣ 查看磁盘使用情况

1
2
3
4
5
6
7
8
# 查看镜像、容器、卷的磁盘使用情况
podman system df

# 查看详细信息
podman system df -v

# 以JSON格式查看
podman system df --format json

输出示例:

类型(TYPE) 总数(TOTAL) 活动数(ACTIVE) 占用空间(SIZE) 可回收空间(RECLAIMABLE)
Images(镜像) 28 17 10.71GB 4.376GB (40%)
Containers(容器) 17 17 569MB 0B (0%)
Local Volumes(本地卷) 3 0 0B 0B
Build Cache(构建缓存) 0 0 0B 0B

2️⃣ 管理悬空镜像

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 查看所有悬空镜像(无标签镜像)
podman images --filter "dangling=true"

# 清理所有悬空镜像
podman image prune --force --filter "dangling=true"

# 创建日志目录并记录清理操作
mkdir -pm 755 /mnt/podman-rmi-log && \
podman image prune --force --filter "dangling=true" > "/mnt/podman-rmi-log/$(date +'%Y-%m-%d')_podman_rmi.log"

# 清理指定时间前的镜像
podman image prune --filter "until=24h"  # 清理24小时前的镜像

3️⃣ 查看清理日志

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 查看最近的清理日志
ls -lt /mnt/podman-rmi-log/*_podman_rmi.log | head -5

# 查看最新5个日志文件的内容
ls -lt /mnt/podman-rmi-log/*_podman_rmi.log | head -5 | awk '{print $9}' | xargs cat

# 实时查看日志更新
tail -f /mnt/podman-rmi-log/$(date +'%Y-%m-%d')_podman_rmi.log

# 查找包含特定信息的日志
grep -r "deleted" /mnt/podman-rmi-log/

⚙️ 三、高级镜像操作

1️⃣ 镜像导入导出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 将镜像导出为tar文件
podman save -o myimage.tar docker.io/openlistteam/openlist:latest

# 从tar文件导入镜像
podman load -i myimage.tar

# 导出多个镜像
podman save -o multiple-images.tar image1:tag1 image2:tag2

# 导出时压缩镜像
podman save --compress docker.io/openlistteam/openlist:latest | gzip > myimage.tar.gz

# 从标准输入导入镜像
cat myimage.tar | podman load

2️⃣ 镜像标签管理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 给镜像添加新标签
podman tag docker.io/openlistteam/openlist:latest myregistry.com/openlist:1.0

# 删除镜像标签
podman untag docker.io/openlistteam/openlist:latest

# 查看镜像的所有标签
podman image inspect --format "{{.RepoTags}}" image_id

# 批量重命名标签
for image in $(podman images -q); do
    podman tag $image myregistry.com/$(podman image inspect --format "{{.RepoTags}}" $image | cut -d':' -f1 | cut -d'/' -f2-)
done

3️⃣ 镜像检查与分析

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 查看镜像的详细信息
podman image inspect docker.io/openlistteam/openlist:latest

# 查看镜像历史
podman history docker.io/openlistteam/openlist:latest

# 查看镜像的层级信息
podman image inspect --format "{{.RootFS.Layers}}" image_id

# 查看镜像创建时间
podman image inspect --format "{{.Created}}" docker.io/openlistteam/openlist:latest

# 查看镜像架构信息
podman image inspect --format "{{.Architecture}}" docker.io/openlistteam/openlist:latest

4️⃣ 镜像构建相关

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 从Dockerfile构建镜像
podman build -t myimage:1.0 .

# 构建时指定Dockerfile路径
podman build -f Dockerfile.dev -t myimage:dev .

# 查看构建缓存
podman build --cache-from localhost/buildcache:latest

# 不使用缓存构建
podman build --no-cache -t myimage:latest .

# 设置构建参数
podman build --build-arg HTTP_PROXY=http://proxy.example.com -t myimage:latest .

🔍 四、镜像查询与过滤

1️⃣ 高级过滤查询

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 按创建时间过滤
podman images --filter "before=2023-01-01"

# 按大小过滤
podman images --filter "since=1GB"

# 按标签过滤
podman images --filter label=maintainer=admin@example.com

# 组合多个过滤器
podman images --filter "dangling=true" --filter "before=2023-01-01"

# 按仓库名称过滤
podman images --filter reference="*nginx*"

# 排除特定仓库
podman images --filter reference="!docker.io/library/*"

2️⃣ 格式化输出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 自定义格式输出
podman images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.CreatedSince}}"

# JSON格式输出
podman images --format json

# 只显示镜像ID
podman images -q

# Go模板格式化输出
podman images --format "{{.ID}}: {{.Repository}}"

# 显示完整镜像信息
podman images --no-trunc

3️⃣ 镜像统计信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 统计镜像数量
podman images | wc -l

# 统计各仓库的镜像数量
podman images --format "{{.Repository}}" | sort | uniq -c | sort -nr

# 计算镜像总大小
podman images --format "{{.Size}}" | sed 's/GB//g' | awk '{sum += $1} END {print sum "GB"}'

# 按大小排序镜像
podman images --sort size

# 统计各标签的镜像数量
podman images --format "{{.Repository}}:{{.Tag}}" | cut -d':' -f2 | sort | uniq -c

🛡️ 五、安全最佳实践

1️⃣ 镜像签名验证

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 启用镜像签名验证
podman pull --signature-policy /etc/containers/policy.json docker.io/library/nginx

# 检查镜像签名
podman image trust show

# 设置信任策略
podman image trust set --type accept docker.io/library/nginx

# 拒绝特定仓库的镜像
podman image trust set --type reject docker.io/untrusted/

# 查看信任策略详情
podman image trust show --verbose

2️⃣ 漏洞扫描

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 使用trivy扫描镜像漏洞(需要先安装trivy)
trivy image docker.io/openlistteam/openlist:latest

# 扫描并输出JSON报告
trivy image -f json -o report.json docker.io/openlistteam/openlist:latest

# 按严重级别过滤漏洞
trivy image --severity HIGH,CRITICAL docker.io/openlistteam/openlist:latest

# 忽略特定漏洞
trivy image --ignore-unfixed docker.io/openlistteam/openlist:latest

# 使用grype扫描漏洞(替代方案)
grype docker.io/openlistteam/openlist:latest

3️⃣ 镜像来源验证

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 检查镜像的来源信息
podman image inspect --format "{{.Digest}}" docker.io/openlistteam/openlist:latest

# 验证镜像的完整性
podman image verify docker.io/openlistteam/openlist:latest

# 检查镜像的发布者
podman image inspect --format "{{.Author}}" docker.io/openlistteam/openlist:latest

# 查看镜像的构建历史
podman history --no-trunc docker.io/openlistteam/openlist:latest

# 检查镜像的配置
podman image inspect --format "{{.Config}}" docker.io/openlistteam/openlist:latest

4️⃣ 安全扫描集成

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 使用Podman内置安全检查
podman scan docker.io/openlistteam/openlist:latest

# 集成到CI/CD流程
#!/bin/bash
IMAGE="docker.io/openlistteam/openlist:latest"
podman pull $IMAGE
trivy image --exit-code 1 --severity CRITICAL $IMAGE

# 定期安全扫描脚本
#!/bin/bash
for image in $(podman images -q); do
    echo "Scanning image: $image"
    trivy image --exit-code 0 --severity HIGH,CRITICAL $image
done

📊 六、实用脚本示例

1️⃣ 定期清理脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# cleanup-podman-images.sh

LOG_DIR="/mnt/podman-rmi-log"
mkdir -p $LOG_DIR

echo "==========================================" >> "$LOG_DIR/cleanup.log"
echo "$(date): 开始Podman镜像清理" >> "$LOG_DIR/cleanup.log"
echo "==========================================" >> "$LOG_DIR/cleanup.log"

# 清理悬空镜像
echo "$(date): 开始清理悬空镜像" >> "$LOG_DIR/cleanup.log"
podman image prune --force --filter "dangling=true" >> "$LOG_DIR/cleanup.log"

# 清理超过30天的未使用镜像
echo "$(date): 开始清理老旧镜像" >> "$LOG_DIR/cleanup.log"
podman image prune -a --force --filter "until=720h" >> "$LOG_DIR/cleanup.log"

# 清理构建缓存
echo "$(date): 开始清理构建缓存" >> "$LOG_DIR/cleanup.log"
podman builder prune --force --all >> "$LOG_DIR/cleanup.log"

# 显示清理后的磁盘使用情况
echo "$(date): 清理后的磁盘使用情况" >> "$LOG_DIR/cleanup.log"
podman system df >> "$LOG_DIR/cleanup.log"

echo "$(date): 清理完成" >> "$LOG_DIR/cleanup.log"
echo "==========================================" >> "$LOG_DIR/cleanup.log"

2️⃣ 镜像备份脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
# backup-podman-images.sh

BACKUP_DIR="/mnt/podman-backup/$(date +%Y%m%d)"
mkdir -p $BACKUP_DIR
LOG_FILE="$BACKUP_DIR/backup.log"

echo "开始备份Podman镜像 - $(date)" | tee -a $LOG_FILE

# 备份所有镜像
for image in $(podman images --format "{{.ID}}"); do
    image_name=$(podman image inspect --format "{{index .RepoTags 0}}" $image)
    
    if [ -n "$image_name" ] && [ "$image_name" != "<none>" ]; then
        # 清理镜像名称中的特殊字符
        safe_name=$(echo $image_name | tr '/:' '_')
        backup_file="$BACKUP_DIR/${safe_name}.tar"
        
        echo "正在备份: $image_name -> $backup_file" | tee -a $LOG_FILE
        podman save -o "$backup_file" $image 2>> $LOG_FILE
        
        if [ $? -eq 0 ]; then
            echo "备份成功: $backup_file" | tee -a $LOG_FILE
        else
            echo "备份失败: $image_name" | tee -a $LOG_FILE
        fi
    fi
done

# 创建备份清单
ls -la $BACKUP_DIR/*.tar > "$BACKUP_DIR/manifest.txt"
echo "备份完成,总计: $(ls -1 $BACKUP_DIR/*.tar | wc -l) 个镜像" | tee -a $LOG_FILE

3️⃣ 镜像同步脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
# sync-podman-images.sh

SOURCE_REGISTRY="docker.io"
TARGET_REGISTRY="myprivate.registry.com"
LOG_FILE="/var/log/podman-sync-$(date +%Y%m%d).log"

# 需要同步的镜像列表
IMAGES=(
    "nginx:1.23"
    "redis:7.0"
    "postgres:15"
    "node:18"
    "python:3.11"
)

echo "开始镜像同步 - $(date)" | tee -a $LOG_FILE

for image in "${IMAGES[@]}"; do
    echo "处理镜像: $image" | tee -a $LOG_FILE
    
    # 拉取镜像
    if podman pull $SOURCE_REGISTRY/library/$image >> $LOG_FILE 2>&1; then
        echo "拉取成功: $image" | tee -a $LOG_FILE
        
        # 重新打标签
        podman tag $SOURCE_REGISTRY/library/$image $TARGET_REGISTRY/$image
        
        # 推送到目标仓库
        if podman push $TARGET_REGISTRY/$image >> $LOG_FILE 2>&1; then
            echo "推送成功: $image" | tee -a $LOG_FILE
        else
            echo "推送失败: $image" | tee -a $LOG_FILE
        fi
        
        # 清理本地镜像
        podman rmi $SOURCE_REGISTRY/library/$image $TARGET_REGISTRY/$image
    else
        echo "拉取失败: $image" | tee -a $LOG_FILE
    fi
    
    echo "----------------------------------------" | tee -a $LOG_FILE
done

echo "镜像同步完成 - $(date)" | tee -a $LOG_FILE

4️⃣ 镜像健康检查脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
# healthcheck-podman-images.sh

LOG_FILE="/var/log/podman-healthcheck-$(date +%Y%m%d).log"
ERROR_COUNT=0

echo "开始Podman镜像健康检查 - $(date)" | tee -a $LOG_FILE

# 检查所有镜像的完整性
for image in $(podman images -q); do
    echo "检查镜像: $image" | tee -a $LOG_FILE
    
    # 验证镜像完整性
    if ! podman image inspect $image > /dev/null 2>&1; then
        echo "错误: 镜像 $image 损坏" | tee -a $LOG_FILE
        ((ERROR_COUNT++))
        continue
    fi
    
    # 检查镜像是否有标签
    tags=$(podman image inspect --format "{{.RepoTags}}" $image)
    if [ "$tags" = "[]" ]; then
        echo "警告: 镜像 $image 无标签" | tee -a $LOG_FILE
    fi
    
    # 检查镜像大小
    size=$(podman image inspect --format "{{.Size}}" $image)
    if [ $size -gt 1073741824 ]; then  # 大于1GB
        echo "警告: 镜像 $image 过大 ($((size/1024/1024))MB)" | tee -a $LOG_FILE
    fi
done

echo "健康检查完成,发现 $ERROR_COUNT 个错误 - $(date)" | tee -a $LOG_FILE

if [ $ERROR_COUNT -gt 0 ]; then
    exit 1
else
    exit 0
fi

🚀 七、性能优化技巧

1️⃣ 加速镜像拉取

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 使用国内镜像加速器
podman pull registry.cn-hangzhou.aliyuncs.com/library/nginx:latest

# 配置镜像加速器
sudo mkdir -p /etc/containers
sudo tee /etc/containers/registries.conf.d/mirror.conf <<EOF
unqualified-search-registries = ["docker.io"]

[[registry]]
prefix = "docker.io"
location = "docker.mirrors.ustc.edu.cn"

[[registry]]
prefix = "quay.io"
location = "quay.mirrors.ustc.edu.cn"
EOF

# 使用并发下载
podman pull --concurrent-downloads=5 docker.io/library/nginx:latest

# 使用缓存代理
export HTTP_PROXY="http://proxy.example.com:3128"
export HTTPS_PROXY="http://proxy.example.com:3128"
podman pull docker.io/library/nginx:latest

2️⃣ 减少磁盘占用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 使用 overlayfs 存储驱动(默认)
sudo podman --storage-driver overlay2

# 定期清理构建缓存
podman builder prune -af

# 使用 squash 选项减少镜像层数
podman build --squash -t myimage:squashed .

# 使用多阶段构建减少镜像大小
cat > Dockerfile <<EOF
FROM node:18 as builder
WORKDIR /app
COPY . .
RUN npm install && npm run build

FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json .
RUN npm install --production
CMD ["node", "dist/index.js"]
EOF

podman build -t myapp:optimized .

3️⃣ 网络优化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 使用更快的DNS服务器
sudo tee /etc/containers/containers.conf.d/dns.conf <<EOF
[network]
dns_servers = ["8.8.8.8", "1.1.1.1"]
EOF

# 调整网络超时设置
sudo tee /etc/containers/containers.conf.d/network.conf <<EOF
[engine]
http_timeout = 300
ssh_timeout = 30
EOF

# 使用IPv4优先
sudo tee /etc/containers/containers.conf.d/ipv4.conf <<EOF
[network]
prefer_ipv4 = true
EOF

4️⃣ 存储优化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 使用更高效的存储驱动
sudo podman --storage-driver overlay2

# 调整存储选项
sudo tee /etc/containers/storage.conf <<EOF
[storage]
driver = "overlay"
runroot = "/var/run/containers/storage"
graphroot = "/var/lib/containers/storage"

[storage.options.overlay]
mountopt = "nodev,metacopy=on"
EOF

# 使用磁盘配额限制
sudo podman --storage-opt size=50G

# 定期优化存储
podman system prune -af
podman system reset --force

🎯 总结

Podman 提供了强大的镜像管理功能,通过本指南你可以:

✅ 核心能力掌握

  • 高效管理镜像:拉取、查看、删除镜像的基本操作
  • 智能清理维护:定期清理悬空镜像,释放磁盘空间
  • 高级镜像操作:导入导出、标签管理、镜像分析
  • 安全最佳实践:镜像验证、漏洞扫描、来源检查
  • 自动化脚本:备份、同步、清理的自动化方案

🔧 日常维护建议

  1. 定期清理:设置定时任务每周清理一次未使用的镜像
  2. 安全扫描:集成漏洞扫描到CI/CD流程中
  3. 备份策略:重要镜像定期备份到私有仓库
  4. 性能监控:监控磁盘使用情况,及时清理大体积镜像
  5. 更新策略:定期更新基础镜像以获取安全补丁

🚀 进阶学习方向

  • 学习Podman容器管理命令
  • 掌握Podman Compose编排工具
  • 了解Podman与Kubernetes的集成
  • 探索Podman的根less模式安全性优势
  • 学习使用Buildah进行高级镜像构建

💡 提示:定期执行镜像清理和维护操作,可以保持系统整洁并提高性能。建议设置定时任务自动执行清理脚本。

最后更新于 2025-09-28