Skip to content

Podman自动更新容器镜像

Podman 自动更新容器镜像总结:

Podman 提供了 podman auto-update 命令用于自动更新容器镜像,而 Docker 则使用 Watchtower 来实现类似功能。本文介绍了使用 Podman 自动更新容器的两种方法,并以 miniserve 容器为例进行演示。

方法一:直接使用 podman auto-update

  1. 查看容器配置
  2. 检查容器的 .container 文件,确认其中包含 AutoUpdate=registry 配置。这等价于在 Podman Run 中使用 --label io.containers.autoupdate=registry 标签。

  3. 查看运行中的容器

  4. 使用 podman ps -a 命令列出所有正在运行和已停止的容器。

  5. 干运行自动更新

  6. 使用 podman auto-update --dry-run 命令检查哪些容器有可用更新。

  7. 执行自动更新

  8. 运行 podman auto-update 命令进行实际的容器更新操作。

  9. 查看更新后的容器状态

  10. 再次使用 podman ps -a 查看容器的运行状态,确认更新是否成功。

  11. 修剪旧镜像

  12. 更新完成后,使用 podman image prune -f 命令清理旧的、不再使用的镜像,以节省磁盘空间。

方法二:使用 podman-auto-update.timer

  1. 查看服务和定时器配置
  2. podman-auto-update.service 定义了自动更新的服务,其 ExecStart 字段指定了更新命令,ExecStartPost 用于更新后清理旧镜像。
  3. podman-auto-update.timer 定义了更新的触发时间,OnCalendar=daily 表示每天运行一次,RandomizedDelaySec=900 添加随机延迟避免集中负载,Persistent=true 确保系统重启后错过任务也能执行。

  4. 启用和启动定时器

  5. 使用 systemctl enable podman-auto-update.timer 命令启用定时器,使其在系统启动时自动加载。
  6. 使用 systemctl start podman-auto-update.timer 命令手动启动定时器。

  7. 查看定时器状态

  8. 使用 systemctl status podman-auto-update.timer 查看定时器的当前状态,包括下次触发时间和服务执行情况。

先决条件和工具

  • Podman 版本:需要 Podman v4.6 或更高版本。
  • Quadlet 支持:使用 Quadlet 结合 Systemd 来优雅地管理容器。
  • container-tools 组件:确保已安装该组件以支持 Podman 的完整功能。

总结

通过上述两种方法,用户可以根据需求选择适合的方式来自动更新 Podman 容器镜像。直接使用 podman auto-update 命令适合手动触发更新,而结合 systemdpodman-auto-update.timer 则适合定时自动更新,减少人工干预。无论是哪种方法,更新后都建议使用 podman image prune -f 清理旧镜像,保持系统整洁和高效。

Ref

https://docs.redhat.com/zh-cn/documentation/red_hat_enterprise_linux/9/html/building_running_and_managing_containers/proc_automatically-updating-containers-using-podman_assembly_porting-containers-to-systemd-using-podman?ref=xzllll.com https://xzllll.com/24112101/