#!/bin/bash

PG_CONTAINER_NAME="pg-dev-server"
PG_VERSION="17-bookworm"
DB_USER="postgres"
DB_PASSWORD="123456"
DB_PORT="5432"
VOLUME_NAME="pg_dev_data"

REDIS_CONTAINER_NAME="redis-dev-server"
REDIS_PORT="6379"
REDIS_VERSION="8"


docker pull postgres:$PG_VERSION

if [ "$(docker ps -aq -f name=$PG_CONTAINER_NAME)" ]; then
    docker rm -f $PG_CONTAINER_NAME
fi

docker run -d \
    --name $PG_CONTAINER_NAME \
    -e POSTGRES_USER=$DB_USER \
    -e POSTGRES_PASSWORD=$DB_PASSWORD \
    -p $DB_PORT:5432 \
    -v $VOLUME_NAME:/var/lib/postgresql/data \
    --restart unless-stopped \
    postgres:$PG_VERSION

echo "------------------------------------------------"
echo "PostgreSQL 已启动！"
echo "容器名称: $PG_CONTAINER_NAME"
echo "宿主机端口: $DB_PORT"
echo "用户名: $DB_USER"
echo "密码: $DB_PASSWORD"
echo "数据卷: $VOLUME_NAME (数据已持久化)"
echo "------------------------------------------------"

docker run -d \
    --name $REDIS_CONTAINER_NAME\
    -p $REDIS_PORT:6379 \
    --restart unless-stopped \
    redis:$REDIS_VERSION

echo "------------------------------------------------"
echo "Redis已启动！"
echo "容器名称: $PG_CONTAINER_NAME"
echo "宿主机端口: $PG_PORT"
echo "------------------------------------------------"
