14.6. 使用 Podman 自动生成一个 systemd 单元文件
默认情况下,Podman 为现有容器或 pod 生成一个单元文件。您可以使用 podman generate systemd --new
生成更多可移植的 systemd
单元文件。--new
标志指示 Podman 生成创建、启动和删除容器的单元文件。
从 Podman v4.6 开始,您可以使用描述如何以类似于常规 systemd
单元文件的格式运行容器的 Quadlets,并在 systemd
下隐藏运行容器的复杂性。
先决条件
-
container-tools
模块已安装。
流程
拉取您要在系统中使用的镜像。例如,要拉取
httpd-24
镜像:# podman pull registry.access.redhat.com/ubi8/httpd-24
可选:列出系统上所有可用的镜像:
# podman images REPOSITORY TAG IMAGE ID CREATED SIZE registry.access.redhat.com/ubi8/httpd-24 latest 8594be0a0b57 2 weeks ago 462 MB
创建
httpd
容器:# podman create --name httpd -p 8080:8080 registry.access.redhat.com/ubi8/httpd-24 cdb9f981cf143021b1679599d860026b13a77187f75e46cc0eac85293710a4b1
可选:验证容器是否已创建:
# podman ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cdb9f981cf14 registry.access.redhat.com/ubi8/httpd-24:latest /usr/bin/run-http... 5 minutes ago Created 0.0.0.0:8080->8080/tcp httpd
为
httpd
容器生成一个systemd
单元文件:# podman generate systemd --new --files --name httpd /root/container-httpd.service
显示生成的
container-httpd.service
systemd
单元文件的内容:# cat /root/container-httpd.service # container-httpd.service # autogenerated by Podman 3.3.1 # Wed Sep 8 20:41:44 CEST 2021 [Unit] Description=Podman container-httpd.service Documentation=man:podman-generate-systemd(1) Wants=network-online.target After=network-online.target RequiresMountsFor=%t/containers [Service] Environment=PODMAN_SYSTEMD_UNIT=%n Restart=on-failure TimeoutStopSec=70 ExecStartPre=/bin/rm -f %t/%n.ctr-id ExecStart=/usr/bin/podman run --cidfile=%t/%n.ctr-id --sdnotify=conmon --cgroups=no-conmon --rm -d --replace --name httpd -p 8080:8080 registry.access.redhat.com/ubi8/httpd-24 ExecStop=/usr/bin/podman stop --ignore --cidfile=%t/%n.ctr-id ExecStopPost=/usr/bin/podman rm -f --ignore --cidfile=%t/%n.ctr-id Type=notify NotifyAccess=all [Install] WantedBy=multi-user.target default.target
使用 --new
选项生成的单元文件不会期望容器和 pod 存在。因此,它们会在启动服务时执行 podman run
命令(请参阅 ExecStart
行),而不是 podman start
命令。例如,请参阅使用 Podman 生成 systemd 单元文件一节。
podman run
命令使用以下命令行选项:-
--conmon-pidfile
选项指向存储主机上运行的conmon
进程的进程 ID 的路径。conmon
进程以与容器相同的退出状态终止,允许systemd
报告正确的服务状态并在需要时重启容器。 -
--cidfile
选项指向存储容器 ID 的路径。 -
%t
是运行时间目录根目录的路径,例如/run/user/$UserID
。 %n
是该服务的全名。将单元文件复制到
/etc/systemd/system
中,以便以 root 用户身份安装它们:# cp -Z container-httpd.service /etc/systemd/system
启用并启动
container-httpd.service
:# systemctl daemon-reload # systemctl enable --now container-httpd.service Created symlink /etc/systemd/system/multi-user.target.wants/container-httpd.service
/etc/systemd/system/container-httpd.service. Created symlink /etc/systemd/system/default.target.wants/container-httpd.service /etc/systemd/system/container-httpd.service.
-
验证
检查
container-httpd.service
的状态:# systemctl status container-httpd.service ● container-httpd.service - Podman container-httpd.service Loaded: loaded (/etc/systemd/system/container-httpd.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2021-08-24 09:53:40 EDT; 1min 5s ago Docs: man:podman-generate-systemd(1) Process: 493317 ExecStart=/usr/bin/podman run --conmon-pidfile /run/container-httpd.pid --cidfile /run/container-httpd.ctr-id --cgroups=no-conmon -d --repla> Process: 493315 ExecStartPre=/bin/rm -f /run/container-httpd.pid /run/container-httpd.ctr-id (code=exited, status=0/SUCCESS) Main PID: 493435 (conmon) ...