add
This commit is contained in:
44
docker-compose-dev.yaml
Normal file
44
docker-compose-dev.yaml
Normal file
@@ -0,0 +1,44 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17.4-alpine
|
||||
container_name: ai_trading_db
|
||||
restart: always
|
||||
ports:
|
||||
- 20001:5432
|
||||
entrypoint:
|
||||
- /scripts/db-lanuch-entrypoint.sh
|
||||
environment:
|
||||
POSTGRES_USER: ${DB_USER}
|
||||
POSTGRES_PASSWORD: ${DB_PASSWORD}
|
||||
POSTGRES_DB: ${DB_NAME}
|
||||
TZ: ${TZ}
|
||||
volumes:
|
||||
- ./shared_data/ai_trading_db:/var/lib/postgresql/data
|
||||
- ./sql:/docker-entrypoint-initdb.d
|
||||
- ./scripts:/scripts
|
||||
networks:
|
||||
- ai-trading-network
|
||||
ai_trading:
|
||||
image: golang:1.25.0-alpine3.22
|
||||
container_name: ai_trading_api
|
||||
restart: always
|
||||
ports:
|
||||
- 20000:80
|
||||
depends_on:
|
||||
- postgres
|
||||
networks:
|
||||
- ai-trading-network
|
||||
environment:
|
||||
DB_HOST: postgres
|
||||
DB_PORT: ${DB_PORT}
|
||||
DB_USER: ${DB_USER}
|
||||
DB_PASSWORD: ${DB_PASSWORD}
|
||||
DB_NAME: ${DB_NAME}
|
||||
TZ: ${TZ}
|
||||
volumes:
|
||||
- ./src:/app
|
||||
command: sh -c "cd /app && go mod tidy && go run main.go"
|
||||
networks:
|
||||
asset_assistant-network:
|
||||
driver: bridge
|
||||
volumes: {}
|
||||
44
docker-compose.yaml
Normal file
44
docker-compose.yaml
Normal file
@@ -0,0 +1,44 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17.4-alpine
|
||||
container_name: asset_assistant_db
|
||||
restart: always
|
||||
ports:
|
||||
- 20001:5432
|
||||
entrypoint:
|
||||
- /scripts/db-lanuch-entrypoint.sh
|
||||
environment:
|
||||
POSTGRES_USER: ${DB_USER}
|
||||
POSTGRES_PASSWORD: ${DB_PASSWORD}
|
||||
POSTGRES_DB: ${DB_NAME}
|
||||
TZ: ${TZ}
|
||||
volumes:
|
||||
- ./shared_data/asset_assistant_db:/var/lib/postgresql/data
|
||||
- ./sql:/docker-entrypoint-initdb.d
|
||||
- ./scripts:/scripts
|
||||
networks:
|
||||
- asset-assistant-network
|
||||
asset_assistant:
|
||||
image: asset-assistant-api:1.0.0
|
||||
container_name: asset_assistant_api
|
||||
restart: always
|
||||
ports:
|
||||
- 20000:80
|
||||
depends_on:
|
||||
- postgres
|
||||
networks:
|
||||
- asset-assistant-network
|
||||
environment:
|
||||
DB_HOST: postgres
|
||||
DB_PORT: ${DB_PORT}
|
||||
DB_USER: ${DB_USER}
|
||||
DB_PASSWORD: ${DB_PASSWORD}
|
||||
DB_NAME: ${DB_NAME}
|
||||
TZ: ${TZ}
|
||||
volumes:
|
||||
# 挂载添加日志目录挂载,将容器内日志日志目录映射到宿主机的 ./logs 目录
|
||||
- ./logs:/app/logs # 假设代码中日志存储路径为 /app/logs
|
||||
networks:
|
||||
asset-assistant-network:
|
||||
driver: bridge
|
||||
volumes: {}
|
||||
59
scripts/db-lanuch-entrypoint.sh
Executable file
59
scripts/db-lanuch-entrypoint.sh
Executable file
@@ -0,0 +1,59 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
# 日志函数(带时间戳)
|
||||
log_info() {
|
||||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] [DB_INIT] $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] [DB_ERROR] $1" >&2
|
||||
}
|
||||
|
||||
# 1. 启动PostgreSQL服务
|
||||
log_info "启动PostgreSQL服务(后台运行)"
|
||||
docker-entrypoint.sh postgres &
|
||||
PG_PID=$!
|
||||
log_info "PostgreSQL主进程ID: $PG_PID"
|
||||
|
||||
# 2. 等待数据库就绪
|
||||
log_info "等待PostgreSQL服务就绪(主机: localhost, 端口: 5432)"
|
||||
retry_count=0
|
||||
max_retries=30 # 最多等待30秒
|
||||
until pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" -h "localhost" -p "5432"; do
|
||||
retry_count=$((retry_count + 1))
|
||||
if [ $retry_count -ge $max_retries ]; then
|
||||
log_error "等待PostgreSQL超时(超过30秒)"
|
||||
exit 1
|
||||
fi
|
||||
log_info "数据库未就绪,等待1秒(重试次数: $retry_count)"
|
||||
sleep 1
|
||||
done
|
||||
log_info "PostgreSQL服务已就绪"
|
||||
|
||||
# 3. 执行SQL脚本
|
||||
log_info "开始执行/docker-entrypoint-initdb.d目录下的SQL脚本"
|
||||
script_count=0
|
||||
for script in /docker-entrypoint-initdb.d/*.sql; do
|
||||
if [ -f "$script" ]; then
|
||||
script_count=$((script_count + 1))
|
||||
log_info "执行脚本 ($script_count): $script"
|
||||
if psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -h "localhost" -p "5432" -f "$script" --set=ON_ERROR_STOP=1; then
|
||||
log_info "脚本执行成功: $script"
|
||||
else
|
||||
log_error "脚本执行失败: $script"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $script_count -eq 0 ]; then
|
||||
log_info "未发现需要执行的SQL脚本"
|
||||
else
|
||||
log_info "所有SQL脚本执行完成(共$script_count个)"
|
||||
fi
|
||||
|
||||
# 4. 等待主进程
|
||||
log_info "等待PostgreSQL主进程结束(PID: $PG_PID)"
|
||||
wait $PG_PID
|
||||
log_info "PostgreSQL进程已退出"
|
||||
Reference in New Issue
Block a user