使用 Systemd 自动更新 Podman 容器

部署为 Systemd

使用所需的设置启动容器(本文以 web 为例):

podman container run --detach --name=web docker.io/web/web:1.0

设置 Systemd 来处理部署,Podman 将生成必要的文件:

podman generate systemd --new --name --files web

这将在您的当前目录中生成文件 container-web.service

删除当前容器,将文件复制到正确的 systemd 目录,然后启用服务:

# 删除临时容器
podman container rm -f web

# 复制 service 文件到 systemd 目录
cp container-web.service /etc/systemd/system/container-web.service

# 重启 Systemd
systemctl daemon-reload

# 启用并启动该服务
systemctl enable --now container-web

# 检查容器运行状态
podman container ls
systemctl status container-web

# 删除临时 service 文件
rm container-*.service

请注意,容器现在只能通过 Systemd 进行管理,使用 Podman 命令启动和停止容器可能会干扰 Systemd。

手动自动更新

编辑 /etc/systemd/system/container-web.service 文件并添加如下所示的标签:

--label=io.containers.autoupdate=registry

更改后的文件将有一个如下所示的部分:

...snip...

ExecStart=/usr/bin/podman container run 
          --detach 
          --name=web 
          --label=io.containers.autoupdate=registry 
          docker.io/web/web:latest

...snip...

现在重新加载 Systemd 并重新启动容器服务以应用更改。

# 重启 Systemd
systemctl daemon-reload

# 重启服务
systemctl restart container-web

完成此设置后,您可以运行一个简单的命令将正在运行的实例更新为所用标签的最新可用映像。

# 更新 Podman 容器
podman auto-update

有计划的自动更新

为 Podman 启用 Systemd 计时器:

# 启用 Podman 自动更新计时器单元
systemctl enable --now podman-auto-update.timer

或者,可以编辑计时器的时间表。默认情况下,更新将在每天早上运行。使用以下命令编辑计时器模块:

systemctl edit podman-auto-update.timer

其他:取消自动更新

# 禁用服务
systemctl disable container-web

# 删除服务文件
rm -rf /etc/systemd/system/container-web.service

# 重启 Systemd
systemctl daemon-reload

# 停止容器
podman container rm -f web

# (可选:如果全部服务删除)禁用 Podman 自动更新计时器单元
systemctl disable --now podman-auto-update.timer

# 检查容器运行状态
podman container ls

参考资料

podman-run — Podman documentation

添加新评论