podman/docker重启后自动运行容器

发布时间 2023-10-27 11:11:22作者: motorao

背景
今天发现云主机 oom 重启之后podman 的容器没有自动运行,大概看了一下,是因为 Podman 不试用 Daemon 守护进城,所以 podman 启动后不自动启动容器。
这里试用 systemd 进行自启动管理。

方案

可以使用podman generate systemd生成并管理

生成 systemd 文件

# podman generate systemd --restart-policy=always -t 1 --name -f vaultwarden
WARN[0000] Container ad911cfea63127a434d135106d89ccbe5158d169a5f787596cbc93968d84e676 has restart policy "always" which can lead to issues on shutdown: consider recreating the container without a restart policy and use systemd's restart mechanism instead
/root/container-vaultwarden.service

这里有提示使用restart=always可能会导致 pod 关闭失败。

配置 systemd

将文件复制到/etc/systemd/system/目录,并使能生效

# mv /root/container-vaultwarden.service /etc/systemd/system/
# systemctl daemon-reload
# systemctl enable container-vaultwarden
Created symlink /etc/systemd/system/default.target.wants/container-vaultwarden.service → /etc/systemd/system/container-vaultwarden.service.

至此,容器重启后的 pod 会通过 systemd自动拉起并运行。