Podman 容器设置开机启动

当你使用podman的时候,已经知道他没有额外的守护进程,这个时候需要通过systemd实现容器的开机自动启动。

假设你的服务器目前已经生成了3个容器,容器的名称分别是nginx1、nginx2、nginx3。注意三个容器一定要逐个启动,否则就会报错。

1.创建service文件

1
2
3
4
cd /usr/lib/systemd/system/
touch podman-container1.service
touch podman-container2.service
touch podman-container3.service

1.1 三个service文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
###podman-container1.service###
[Unit]
Description=Podman container1

[Service]
Type=simple
ExecStart=/usr/bin/podman start -a nginx1
ExecStop=/usr/bin/podman stop -t 10 nginx1

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
###podman-container2.service###
[Unit]
Description=Podman container2
After=network.target
After=network-online.target
After=podman-container1.service

[Service]
Type=simple
ExecStart=/usr/bin/podman start -a  nginx2
ExecStop=/usr/bin/podman stop -t 20 nginx2

[Install]
WantedBy=multi-user.target

1
2
3
4
5
6
7
8
9
10
11
12
13
14
###podman-container3.service###
[Unit]
Description=Podman container3
After=network.target
After=network-online.target
After=podman-container2.service

[Service]
Type=simple
ExecStart=/usr/bin/podman start -a nginx3
ExecStop=/usr/bin/podman stop -t 22 nginx3

[Install]
WantedBy=multi-user.target

2.设置开机启动:

1
2
3
systemctl enable podman-container1.service
systemctl enable podman-container2.service
systemctl enable podman-container3.service

3.重启server验证

1
2
reboot
docker ps -a

源文件也可以在这里获取:https://github.com/byygyy/linux-best-practice/blob/master/podman-auto-start.service