Skip to content

Manage Podman Container With Systemd

1. Problem Intro

Podman与Docker不同,在架构上无需docker daemon控制容器的运行,因此容器的重启需要由systemd接管。

2. Generate systemd service file

podman generate systemd -t 30 -n --new -f web
  • -t: 超时时间
  • -n: 表示使用容器名代替容器id
  • -f: 表示生成服务文件
  • --new: 每次启动都删除旧容器,启动一个新的容器

3. Manage Pod with Systemd

(1). Create a Pod

podman pod create --name POD_NAME

(2). Create Container in Pod

podman container run --pod POD_NAME --name CONTAINER_NAME1
podman container run --pod POD_NAME --name CONTAINER_NAME2

(3). Generate systemd service files for Pod and Containers inside

podman generate systemd -f --new --restart-policy always --name POD_NAME
- -f: 表示生成服务文件 - --new: 每次启动都删除旧容器,启动一个新的容器 - POD_NAME: pod名称

4. Config Systemd

(1). Move service file to /lib/systemd/system

(2). Restore SELinux Label with command below

restorecon /lib/systemd/system/container-***.service

(3). Reload daemon with command below

systemctl daemon-reload

(4). Set Service Enabled

systemctl enable container-***.service

REF

[1]. https://blog.51cto.com/omaidb/4531846

[2]. https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/9/html/building_running_and_managing_containers/assembly_porting-containers-to-systemd-using-podman_building-running-and-managing-containers#doc-wrapper

[3]. https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/9/html/building_running_and_managing_containers/proc_auto-starting-pods-using-systemd_assembly_porting-containers-to-systemd-using-podman