188 lines
4.3 KiB
Markdown
Executable File
188 lines
4.3 KiB
Markdown
Executable File
喵~ 当然可以!这是个很棒的方案!(兴奋地竖起尾巴) 通过 SSH 隧道将远程服务器的 Docker 服务映射到本地,这样你就能在 Termux 上愉快地使用这些服务啦!
|
||
|
||
## 🔧 配置步骤
|
||
|
||
### 1️⃣ 在服务器上部署 Docker
|
||
|
||
先把你的 `docker-compose.dev.yml` 上传到服务器并启动:
|
||
|
||
```bash
|
||
# 在服务器上
|
||
scp docker-compose.dev.yml user@your-server:~/
|
||
ssh user@your-server
|
||
docker compose -f docker-compose.dev.yml up -d
|
||
```
|
||
|
||
### 2️⃣ 创建 SSH 隧道脚本
|
||
|
||
在你的 Termux 本地,创建一个隧道脚本 `tunnel.sh`:
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
|
||
# SSH 隧道映射 - 开发基础设施
|
||
# 用法: bash tunnel.sh [start|stop|status]
|
||
|
||
SERVER="user@your-server.com" # 改成你的服务器地址
|
||
PORTS=(
|
||
"5432:5432" # PostgreSQL
|
||
"6379:6379" # Redis
|
||
"6333:6333" # Qdrant (gRPC)
|
||
"6334:6334" # Qdrant (HTTP)
|
||
"9000:9000" # MinIO API
|
||
"9001:9001" # MinIO Console
|
||
"4222:4222" # NATS
|
||
"8222:8222" # NATS HTTP
|
||
)
|
||
|
||
case "$1" in
|
||
start)
|
||
echo "🔌 启动 SSH 隧道..."
|
||
# 构建端口转发参数
|
||
ARGS=""
|
||
for port in "${PORTS[@]}"; do
|
||
ARGS="$ARGS -L $port"
|
||
done
|
||
|
||
# 后台运行 SSH 隧道
|
||
ssh -N $ARGS $SERVER &
|
||
echo $! > .tunnel_pid
|
||
echo "✅ 隧道已启动 (PID: $(cat .tunnel_pid))"
|
||
;;
|
||
|
||
stop)
|
||
if [ -f .tunnel_pid ]; then
|
||
kill $(cat .tunnel_pid) 2>/dev/null
|
||
rm .tunnel_pid
|
||
echo "🛑 隧道已停止"
|
||
else
|
||
echo "❌ 没有运行中的隧道"
|
||
fi
|
||
;;
|
||
|
||
status)
|
||
if [ -f .tunnel_pid ] && kill -0 $(cat .tunnel_pid) 2>/dev/null; then
|
||
echo "✅ 隧道运行中 (PID: $(cat .tunnel_pid))"
|
||
else
|
||
echo "❌ 隧道未运行"
|
||
fi
|
||
;;
|
||
|
||
*)
|
||
echo "用法: $0 [start|stop|status]"
|
||
;;
|
||
esac
|
||
```
|
||
|
||
### 3️⃣ 更专业的方案:使用 autossh 自动重连
|
||
|
||
```bash
|
||
# 安装 autossh
|
||
pkg install autossh
|
||
|
||
# 创建 autossh 隧道脚本
|
||
cat > auto-tunnel.sh << 'EOF'
|
||
#!/bin/bash
|
||
|
||
SERVER="user@your-server.com"
|
||
|
||
# 使用 autossh 自动重连
|
||
autossh -M 0 \
|
||
-o "ServerAliveInterval=30" \
|
||
-o "ServerAliveCountMax=3" \
|
||
-o "ExitOnForwardFailure=yes" \
|
||
-N \
|
||
-L 5432:localhost:5432 \
|
||
-L 6379:localhost:6379 \
|
||
-L 6333:localhost:6333 \
|
||
-L 6334:localhost:6334 \
|
||
-L 9000:localhost:9000 \
|
||
-L 9001:localhost:9001 \
|
||
-L 4222:localhost:4222 \
|
||
-L 8222:localhost:8222 \
|
||
$SERVER
|
||
EOF
|
||
|
||
chmod +x auto-tunnel.sh
|
||
```
|
||
|
||
### 4️⃣ 配置 SSH 免密登录
|
||
|
||
为了更方便,设置 SSH 密钥:
|
||
|
||
```bash
|
||
# 生成密钥
|
||
ssh-keygen -t ed25519
|
||
|
||
# 复制到服务器
|
||
ssh-copy-id user@your-server.com
|
||
|
||
# 或者手动复制
|
||
cat ~/.ssh/id_ed25519.pub | ssh user@your-server.com "cat >> ~/.ssh/authorized_keys"
|
||
```
|
||
|
||
### 5️⃣ 高级用法:配合 tmux 持久运行
|
||
|
||
```bash
|
||
# 安装 tmux
|
||
pkg install tmux
|
||
|
||
# 在 tmux 会话中运行隧道
|
||
tmux new -s docker-tunnel
|
||
./auto-tunnel.sh
|
||
# Ctrl+B, D 分离会话
|
||
|
||
# 重新连接
|
||
tmux attach -t docker-tunnel
|
||
```
|
||
|
||
### 6️⃣ 一键启动脚本
|
||
|
||
创建一个完整的 `start-dev.sh`:
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
|
||
echo "🐱 Cyrene AI 开发环境启动脚本"
|
||
|
||
# 检查 SSH 连接
|
||
echo "🔍 检查服务器连接..."
|
||
ssh -o ConnectTimeout=5 user@your-server.com "echo '✅ 服务器连接正常'" || {
|
||
echo "❌ 无法连接服务器"
|
||
exit 1
|
||
}
|
||
|
||
# 检查 Docker 服务
|
||
echo "🔍 检查 Docker 服务..."
|
||
ssh user@your-server.com "docker compose -f docker-compose.dev.yml ps" || {
|
||
echo "❌ Docker 服务未运行,正在启动..."
|
||
ssh user@your-server.com "cd ~ && docker compose -f docker-compose.dev.yml up -d"
|
||
}
|
||
|
||
# 启动隧道
|
||
echo "🔌 启动 SSH 隧道..."
|
||
screen -dmS docker-tunnel ./auto-tunnel.sh
|
||
|
||
echo "✅ 环境就绪!"
|
||
echo "📊 服务列表:"
|
||
echo " PostgreSQL: localhost:5432"
|
||
echo " Redis: localhost:6379"
|
||
echo " Qdrant: localhost:6333"
|
||
echo " MinIO API: localhost:9000"
|
||
echo " MinIO Web: localhost:9001"
|
||
echo " NATS: localhost:4222"
|
||
|
||
chmod +x start-dev.sh
|
||
```
|
||
|
||
## 🎯 使用方式
|
||
|
||
```bash
|
||
# 启动所有服务
|
||
./start-dev.sh
|
||
|
||
# 或者手动
|
||
./tunnel.sh start
|
||
```
|
||
|
||
这样就完美解决 Termux 权限问题了!而且服务器性能更好,还能随时随地从本地连接开发环境呢!(开心地转圈圈) 🌟🐱 |