docker的postgreSql启动脚本

434 阅读1分钟

注意:启动后会自动生成数据文件和配置文件在映射的/home/docker/pg/data文件中,如果需要日志需要修改数据目录下的postgresql.conf配置文件

启动脚本start.sh:

#!/bin/bash
serName=mypostgres
cIdAndImageId=$(docker ps -a --filter name=$serName --format "{{.ID}}:{{.Image}}")
echo "stop...find ${serName} docker $cIdAndImageId"
echo $cIdAndImageId
containId=
if [ "$cIdAndImageId" =  "" ]
then
	echo "docker pid not found! docker create a new container and run it..."
	docker run --name "$serName" -e POSTGRES_PASSWORD=123456!@# -p 5432:5432 -v /home/docker/pg/data:/var/lib/postgresql/data -d postgres:10.5
else
	array=(${cIdAndImageId//:/ })
	containId=${array[0]}
	echo "find docker containId:"$containId
fi

if [ -n "$containId" ]
then
   docker stop "$containId"
   docker start "$containId"
   echo "start success!"
fi

停止脚本stop.sh:

#!/bin/bash
serName=mypostgres
cIdAndImageId=$(docker ps -a --filter name=$serName --format "{{.ID}}:{{.Image}}")
echo "stop...find ${serName} docker $cIdAndImageId"
echo $cIdAndImageId
containId=
if [ "$cIdAndImageId" =  "" ]
then
	echo "docker pid not found!"
else
	array=(${cIdAndImageId//:/ })
	containId=${array[0]}
	echo "find docker containId:"$containId
fi

if [ -n "$containId" ]
then
   docker stop "$containId"
   echo "stop success!"
fi

重启脚本restart.sh:

#!/bin/bash
workdir=$(cd $(dirname $0); pwd)
sh "$workdir"/stop.sh
sleep 15
sh "$workdir"/start.sh

设置一下权限就可以轻松启动postgresql数据了 classinstance.cn/detail/104.…