2021-05-17-podman添加开机自启

podman因为不像docker那样有daemon进程,启动容器不再支持 restart 参数,对于系统重启后,需要自启的容器需要手动做一些配置,如通过 systemd 来管理。

podman 提供了命令生成 systemd 配置文件。前提是容器已经创建。

# 先看下容器名称,最后一个参数  
 [root@bastion ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED       STATUS             PORTS                   NAMES
396f8e6b7bf3  docker.io/library/registry:2.6  /etc/docker/regis...  4 months ago  Up 11 minutes ago  0.0.0.0:5000->5000/tcp  mirror-registry

# systemd 后面跟容器名称  

[root@bastion ~]# podman generate systemd mirror-registry 
# container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service
# autogenerated by Podman 1.6.4
# Mon May 17 16:43:07 CST 2021

[Unit]
Description=Podman container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service
Documentation=man:podman-generate-systemd(1)

[Service]
Restart=on-failure
ExecStart=/usr/bin/podman start 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38
ExecStop=/usr/bin/podman stop -t 10 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38
KillMode=none
Type=forking
PIDFile=/var/run/containers/storage/overlay-containers/396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38/userdata/conmon.pid

[Install]
WantedBy=multi-user.target

生成配置文件,并移到目录 /usr/lib/systemd/system/ 并设置开机自启

podman generate systemd mirror-registry > mirror-registry.service
mv mirror-registry.service /usr/lib/systemd/system/

systemctl enable mirror-registry

测试一下,要确保此容器停止不影响业务。

第一次systemctl stop 没有成功,可能和当前还没有 PIDFile 有关,手动 stop 一下。 然后再测试 start, stop ,系统重启

[root@bastion ~]# systemctl stop mirror-registry
[root@bastion ~]# podman stop -t 10 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38

[root@bastion ~]# podman ps
CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES

[root@bastion ~]# systemctl start mirror-registry

[root@bastion ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED       STATUS            PORTS                   NAMES
396f8e6b7bf3  docker.io/library/registry:2.6  /etc/docker/regis...  4 months ago  Up 2 seconds ago  0.0.0.0:5000->5000/tcp  mirror-registry

[root@bastion ~]# systemctl stop  mirror-registry
[root@bastion ~]# podman ps
CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES

# 可以通过 systemctl 控制启动与停止

重启系统测试下能不能自启动

[root@bastion ~]# reboot

[root@bastion ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED       STATUS             PORTS                   NAMES
396f8e6b7bf3  docker.io/library/registry:2.6  /etc/docker/regis...  4 months ago  Up 10 minutes ago  0.0.0.0:5000->5000/tcp  mirror-registry


[root@bastion ~]# systemctl status mirror-registry.service 
 mirror-registry.service - Podman container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service
   Loaded: loaded (/usr/lib/systemd/system/mirror-registry.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-05-17 17:03:47 CST; 10min ago
     Docs: man:podman-generate-systemd(1)
  Process: 3337 ExecStart=/usr/bin/podman start 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38 (code=exited, status=0/SUCCESS)
 Main PID: 5847 (conmon)
   CGroup: /system.slice/mirror-registry.service
           ‣ 5847 /usr/bin/conmon --api-version 1 -s -c 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38 -u 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4...

May 16 20:58:37 bastion systemd[1]: Starting Podman container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service...
May 17 17:03:46 bastion podman[3337]: 2021-05-17 17:03:46.645432077 +0800 CST m=+5.691401362 system refresh
May 17 17:03:47 bastion podman[3337]: 2021-05-17 17:03:47.60543448 +0800 CST m=+6.651403749 container init 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156...r-registry)
May 17 17:03:47 bastion podman[3337]: 2021-05-17 17:03:47.674173834 +0800 CST m=+6.720143108 container start 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd44561...r-registry)
May 17 17:03:47 bastion podman[3337]: 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38
May 17 17:03:47 bastion systemd[1]: Started Podman container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service.
Hint: Some lines were ellipsized, use -l to show in full.

Last updated