podman 编译并安装为服务

Nov 21, 2023

使用 podman 创建的应用无法自动启动,需要安装为服务,下面的代码将编译 docker 文件,并安装服务。 普通用户 需要 systemctl –user … , root 用户则不需要.

Shell 脚本文件:deploy.sh

#!/bin/bash -ex

CURRENT=`pwd`

cd $(dirname $0)
if [ $# -lt 3 ] ; then
  echo "ERROR ERROR ERROR : $0 xxxx.jav app_config_dir deploy_name"
  exit 1;
fi

DOCKER_NAME=$3
PORT=$4

set -e

TMP=`mktemp -d /tmp/docker.deploy.XXXXXXXX`

## 
cp $CURRENT/$1 "$TMP/app.jar"
cp -r $CURRENT/$2 "$TMP/config/"
cp Dockerfile "$TMP/Dockerfile"

cd $TMP

TAG_NAME=`date +%Y%m%d-%H%M%S`
/usr/bin/podman build . -t $DOCKER_NAME:$TAG_NAME 
#/usr/bin/podman build . -t $DOCKER_NAME:latest
/usr/bin/podman tag $DOCKER_NAME:$TAG_NAME $DOCKER_NAME:latest

/usr/bin/podman ps | grep $DOCKER_NAME && /usr/bin/podman stop $DOCKER_NAME
/usr/bin/podman ps -a | grep $DOCKER_NAME && /usr/bin/podman rm $DOCKER_NAME
/usr/bin/podman run -d --net host --restart always \
                         --name $DOCKER_NAME \
                         -v /home/app/logs/$DOCKER_NAME/:/var/log/tomcat/ \
                         $DOCKER_NAME:$TAG_NAME


/usr/bin/podman generate systemd --files --name $DOCKER_NAME
sv="container-${DOCKER_NAME}.service"
if [ `id -u` == 0 ]; then
    mv -f $sv /usr/lib/systemd/system/
    systemctl daemon-reload
    systemctl enable $sv
else
    mkdir -p  ~/.config/systemd/user/
    mv -f $sv ~/.config/systemd/user/
    systemctl --user daemon-reload
    systemctl --user enable $sv
fi

cd ~
rm -rf $TMP

<完>