Podman设置容器开机自启
步骤总结:
1.启动一个容器
# podman run -d --name web httpd
2.查看该容器
# podman ps
3.每次都启动新容器方式创建servcie //--new参数,每次启动都删除旧容器,启动一个新容器
# podman generate systemd -n --new -f web
4.查看启动文件
# cat /root/container-web.service
5.将server文件保存在/etc/systemd/system/
# mv /root/container-web.service /etc/systemd/system/
6.刷新配置文件,让其生效
# systemctl daemon-reload
7.修改selinux
# cat config |grep SELINUX=disabled
8.设置容器开机自启,并且现在启动
# systemctl enable --now /etc/systemd/system/container-web.service
9.测试,重启虚拟机
# reboot
10.查看容器是否在运行,并查看container-web.service是否开机自启和运行
# podman ps
1. //启动一个容器
[root@SYL2 ~]# podman run -d --name web httpd
WARN[0000] Ignoring global metacopy option, not supported with booted kernel
a1e07f4e884a7553aee1ea04bff6813d2440316287f6e7a62b4b7a5a884bad1a
[root@SYL2 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1e07f4e884a docker.io/library/httpd:latest httpd-foreground 12 seconds ago Up 12 seconds ago web
2. //每次都启动新容器方式创建servcie //--new参数,每次启动都删除旧容器,启动一个新容器
[root@SYL2 ~]# podman generate systemd -n --new -f web
/root/container-web.service
[root@SYL2 ~]# cat container-web.service
# container-web.service
# autogenerated by Podman 4.0.2
# Tue May 10 17:38:39 CST 2022
[Unit]
Description=Podman container-web.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 --cgroups=no-conmon --rm --sdnotify=conmon --replace -d --name web httpd
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=default.target
3. //将server文件保存在/etc/systemd/system/
[root@SYL2 ~]# mv container-web.service /etc/systemd/system/
4. //刷新配置文件,让其生效
[root@SYL2 ~]# systemctl daemon-reload
[root@SYL2 ~]#
5. //修改selinux
[root@SYL2 ~]# setenforce 0
[root@SYL2 ~]# cd /etc/selinux/
[root@SYL2 selinux]# ls
config semanage.conf targeted
[root@SYL2 selinux]# vim config
[root@SYL2 selinux]# cat config |grep SELINUX=disabled
SELINUX=disabled
6. //设置容器开机自启,并且现在启动
[root@SYL2 ~]# cd /etc/systemd/system
[root@SYL2 system]# systemctl enable --now container-web.service
Created symlink /etc/systemd/system/default.target.wants/container-web.service → /etc/systemd/system/container-web.service.
[root@SYL2 system]# systemctl status container-web.service
● container-web.service - Podman container-web.service
Loaded: loaded (/etc/systemd/system/container-web.service; ena>
Active: active (running) since Tue 2022-05-10 17:54:20 CST; 25>
Docs: man:podman-generate-systemd(1)
Process: 246294 ExecStartPre=/bin/rm -f /run/container-web.serv>
Main PID: 246464 (conmon)
Tasks: 3 (limit: 11175)
Memory: 3.1M
CGroup: /system.slice/container-web.service
├─246413 /usr/bin/fuse-overlayfs -o lowerdir=/var/lib/>
└─246464 /usr/bin/conmon --api-version 1 -c 14105dc4b4>
May 10 17:54:18 SYL2 systemd[1]: Starting Podman container-web.se>
May 10 17:54:20 SYL2 podman[246295]: a1e07f4e884a7553aee1ea04bff6>
May 10 17:54:20 SYL2 podman[246295]: time="2022-05-10T17:54:20+08>
May 10 17:54:20 SYL2 systemd[1]: Started Podman container-web.ser>
May 10 17:54:20 SYL2 podman[246295]: 14105dc4b4d1b76aa754acf7bc3c>
[root@SYL2 system]#
7. //测试,重启虚拟机
[root@SYL2 system]# reboot
8. //查看容器是否在运行,并查看container-web.service是否开机自启和运行
[root@SYL2 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ebc4b868781 docker.io/library/httpd:latest httpd-foreground About a minute ago Up About a minute ago web
[root@SYL2 ~]# reboot
连接断开
连接主机...
连接主机成功
Last login: Tue May 10 17:59:50 2022 from 192.168.232.1
[root@SYL2 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12340a3083c4 docker.io/library/httpd:latest httpd-foreground 54 seconds ago Up 54 seconds ago web
[root@SYL2 ~]# cd /etc/systemd/system
[root@SYL2 system]# systemctl status container-web.service
● container-web.service - Podman container-web.service
Loaded: loaded (/etc/systemd/system/container-web.service; ena>
Active: active (running) since Tue 2022-05-10 18:00:24 CST; 1m>
Docs: man:podman-generate-systemd(1)
Process: 1116 ExecStartPre=/bin/rm -f /run/container-web.servic>
Main PID: 1447 (conmon)
Tasks: 3 (limit: 11175)
Memory: 73.9M
CGroup: /system.slice/container-web.service
├─1330 /usr/bin/fuse-overlayfs -o lowerdir=/var/lib/co>
└─1447 /usr/bin/conmon --api-version 1 -c 12340a3083c4>
May 10 18:00:24 SYL2 systemd[1]: Starting Podman container-web.se>
May 10 18:00:24 SYL2 podman[1121]: time="2022-05-10T18:00:24+08:0>
May 10 18:00:24 SYL2 systemd[1]: Started Podman container-web.ser>
May 10 18:00:24 SYL2 podman[1121]: 12340a3083c4dfda5fc0124dcbf4e9>
[root@SYL2 system]#
[root@SYL2 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12340a3083c4 docker.io/library/httpd:latest httpd-foreground 7 minutes ago Up 7 minutes ago web
[root@SYL2 ~]# podman inspect web|grep -i ipaddr
"IPAddress": "10.88.0.2",
"IPAddress": "10.88.0.2",
[root@SYL2 ~]# curl 10.88.0.2
<html><body><h1>It works!</h1></body></html>
[root@SYL2 ~]#