Portainer 与 Docker

Podman加入Portainer管理

之前一直以为是podman不支持Restfull API,在查阅资料后发现是支持的,但本人对linux不甚了解,导致在这里绕了很多弯路。后来查阅网文并在对比docker.service后发现,两者提供Restfull API的方式是相同的,并且Podman也提供了兼容的API,而导致Portainer无法添加Podman的问题是没有将socket暴露的API,再通过HTTP方式暴露出来而已,随后又经过一番探索后找到方法:

podman system service tcp:0.0.0.0:2375 --time=0 &

修改服务,/usr/lib/systemd/system/podman.service

[Unit]
Description=Podman API Service
Requires=podman.socket
After=podman.socket
Documentation=man:podman-system-service(1)
StartLimitIntervalSec=0

[Service]
Delegate=true
Type=exec
KillMode=process
Environment=LOGGING="--log-level=info"
ExecStart=/usr/bin/podman $LOGGING system service tcp:0.0.0.0:2375 --time=0

[Install]
WantedBy=default.target

自动启动容器

批量启动容器

/opt/podman-app.sh文件内容

#!/bin/bash

start(){
  podman start $(podman ps -a | grep -wv Up  | awk 'FNR!=1 {print($1)}')
  // 或
  // podman start $(podman ps -a | grep Exited  | awk 'FNR!=1 {print($1)}')
  // 或
  // podman start $(podman ps -a | grep 'Created \| Exited'  | awk '{print($1)}')
}

stop(){
  podman stop $(podman ps -a | grep Up  | awk '{print($1)}')
}

restart(){
  stop
  echo 'Podman container stoped!'
  start
  echo 'Podman container started!'
}

case $1 in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  *)
    start
esac

添加自启动服务

/usr/lib/systemd/system/podman-app.service 文件内容

[Unit]
Description=Podman Container Service
Requires=podman.socket
After=podman.service
StartLimitIntervalSec=0

[Service]
Delegate=true
Type=exec
KillMode=process
ExecStart=/opt/podman-app.sh

[Install]
WantedBy=default.target

参考资料