Docker应用栈服务

一个基于Docker的应用栈,包含PostgreSQL数据库、Nginx HTTPS服务器和Go语言开发的MCP服务器,适用于本地开发和网络服务部署。

  • by billy1234
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 billy1234/ClaudeJarvis

使用说明

贾维斯

基于Docker的应用程序堆栈,包含PostgreSQL、Nginx(HTTPS)和基于Go的MCP服务器。

建筑

  • PostgreSQL 16:数据库服务器
  • Nginx:仅支持HTTPS的web服务器和反向代理
  • MCP服务器:基于Go-based API服务器,具有数据库连接

先决条件

  • 码头工人
  • Docker Compose
  • OpenSSL(用于生成SSL证书)

设置

1.环境配置

复制示例环境文件并配置凭据:

cp .env.example .env

编辑 .env 并设置数据库凭据和端口:

DB_USER=your_db_user
DB_PASSWORD=your_secure_password
DB_NAME=your_db_name
DB_PORT=5432
WEB_HTTPS_PORT=443
MCP_PORT=8080

2.SSL证书

快速入门: 使用提供的辅助脚本以交互方式生成证书:

# Linux/macOS
./generate-ssl.sh

# Windows PowerShell
.\generate-ssl.ps1

或者手动选择以下选项之一:

选项A:具有可信证书的本地网络(建议用于开发)

使用 mkcert 创建在浏览器中工作且没有警告的本地受信任证书:

# Install mkcert (one-time setup)
# macOS: brew install mkcert
# Windows: choco install mkcert
# Linux: See https://github.com/FiloSottile/mkcert#installation

# Install local CA
mkcert -install

# Generate certificate for your domain
mkdir -p nginx/ssl
mkcert -key-file nginx/ssl/key.pem -cert-file nginx/ssl/cert.pem \
  localhost jarvis.local 192.168.1.100 "*.jarvis.local"

替换 192.168.1.100 使用您机器的本地IP。添加您要在网络上使用的任何自定义域。

选项B:自签名证书(快速入门)

为自定义域生成自签名证书:

mkdir -p nginx/ssl

# Replace jarvis.local with your desired domain
DOMAIN="jarvis.local"

openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout nginx/ssl/key.pem \
  -out nginx/ssl/cert.pem \
  -subj "/C=US/ST=State/L=City/O=Jarvis/CN=${DOMAIN}" \
  -addext "subjectAltName=DNS:${DOMAIN},DNS:*.${DOMAIN},DNS:localhost,IP:127.0.0.1"

注: 浏览器将显示自签名证书的安全警告。您需要接受警告才能继续。

使用自定义域,如 jarvis.local 在您的网络上,将其添加到您的主机文件或DNS服务器中。

选项C:使用Cloudflare或Let’s Encrypt进行生产

对于公共互联网访问,请使用受信任的证书颁发机构:

使用Cloudflare:

  1. 将您的域名指向Cloudflare DNS中的服务器IP
  2. 在Cloudflare SSL/TLS设置中生成源证书
  3. 下载证书和密钥,另存为:
    • nginx/ssl/cert.pem (证书)
    • nginx/ssl/key.pem (私钥)

使用Let's Encrypt(certbot):

# Install certbot
sudo apt-get install certbot

# Generate certificate (standalone mode - stop nginx first)
sudo certbot certonly --standalone -d yourdomain.com -d www.yourdomain.com

# Copy certificates
mkdir -p nginx/ssl
sudo cp /etc/letsencrypt/live/yourdomain.com/fullchain.pem nginx/ssl/cert.pem
sudo cp /etc/letsencrypt/live/yourdomain.com/privkey.pem nginx/ssl/key.pem
sudo chown $USER:$USER nginx/ssl/*.pem

使用certbot的续订挂钩设置自动续订。

3.启动服务

docker-compose up -d

4.验证服务

检查所有服务是否正在运行:

docker-compose ps

测试运行状况端点:

curl -k https://localhost/health

预期响应:

{
  "status": "healthy",
  "database": "connected"
}

服务端点

  • web界面: https://localhost:443
  • MCP API: https://localhost/api/mcp
  • 健康检查: https://localhost/health
  • PostgreSQL: localhost:5432
  • MCP服务器(直接): localhost:8080

发展

查看日志

# All services
docker-compose logs -f

# Specific service
docker-compose logs -f mcp-server
docker-compose logs -f postgres
docker-compose logs -f web

重建服务

# Rebuild all
docker-compose up -d --build

# Rebuild specific service
docker-compose up -d --build mcp-server

停止服务

docker-compose down

停止并删除卷

docker-compose down -v

项目结构

.
├── docker-compose.yml          # Docker Compose configuration
├── .env.example               # Environment template
├── .env                       # Your environment (not in git)
├── generate-ssl.sh            # SSL certificate generator (Linux/macOS)
├── generate-ssl.ps1           # SSL certificate generator (Windows)
├── README.md                  # This file
├── mcp-server/               # MCP Go server
│   ├── Dockerfile
│   ├── main.go
│   ├── go.mod
│   └── go.sum
└── nginx/                    # Nginx configuration
    ├── nginx.conf            # Nginx config
    ├── html/                 # Static files
    │   └── index.html
    └── ssl/                  # SSL certificates
        ├── cert.pem
        └── key.pem

数据库访问

连接到PostgreSQL:

docker-compose exec postgres psql -U <DB_USER> -d <DB_NAME>

故障排除

证书问题

如果遇到证书错误,请确保在 nginx/ssl 目录。

连接被拒绝

如果服务无法连接,请检查:

  1. 所有容器都在运行: docker-compose ps
  2. 检查日志: docker-compose logs
  3. 验证网络: docker network ls

数据库连接问题

如果MCP服务器无法连接到PostgreSQL:

  1. 验证中的凭据 .env
  2. 检查PostgreSQL日志: docker-compose logs postgres
  3. 确保数据库健康: docker-compose ps

安全须知

  • 默认配置仅将自签名证书用于开发
  • 更改生产中的所有默认凭据
  • 在生产中使用来自受信任CA的正确SSL证书
  • 考虑在生产环境中对敏感数据使用Docker secrets
  • PostgreSQL端口公开用于开发;限制生产
  • MCP服务器端口(8080)公开用于开发/调试。在生产中,从中删除端口映射 docker-compose.yml 因此MCP服务器只能通过Nginx HTTPS访问。更改:
    # Remove this in production:
    ports:
      - "${MCP_PORT:-8080}:${MCP_PORT:-8080}"
    
    这确保了所有流量都通过具有适当SSL加密的反向代理