浏览器自动化容器服务

基于Docker的Playwright MCP服务器,提供容器化环境下的浏览器自动化功能,包括HTTP API接口和优化的Chrome配置。

  • by michaail
  • Shell
  • 本地部署
  • 更新于
在 GitHub 查看 michaail/playwright-mcp-docker

使用说明

剧作家MCP Docker

一种基于Docker的设置,用于在容器化环境中运行微软的Playwright MCP(模型上下文协议)服务器和Chrome浏览器自动化。

概述

该项目提供了一个完整的Docker环境,用于通过MCP(模型上下文协议)接口运行Playwright浏览器自动化。它包括:

  • 剧作家MCP服务器:微软官方用于浏览器自动化的Playwright MCP服务器
  • Chromium浏览器:预先配置的Chrome/Chromium,具有针对容器化环境的优化标志
  • 自定义Chrome包装:一个包装器脚本,可以动态定位Chrome并使用适当的标志进行配置
  • HTTP API接口:用于浏览器自动化任务的RESTful API端点
  • 体积持久性:配置和输出文件持久性

建筑

┌─────────────────────────────────────────┐
│             Docker Container            │
│                                         │
│  ┌─────────────────┐                    │
│  │  MCP Server     │                    │
│  │  (Port 8831)    │◄──── HTTP API      │
│  └─────────────────┘                    │
│           │                             │
│           ▼                             │
│  ┌─────────────────┐                    │
│  │ chrome-wrapper  │                    │
│  │      .sh        │                    │
│  └─────────────────┘                    │
│           │                             │
│           ▼                             │
│  ┌─────────────────┐                    │
│  │   Chromium      │                    │
│  │   Browser       │                    │
│  └─────────────────┘                    │
└─────────────────────────────────────────┘

特性

  • 无头浏览器自动化:容器化环境中的完整编剧功能
  • HTTP API接口:易于与外部应用程序集成
  • 自定义Chrome配置:针对Docker环境优化了Chrome标志
  • 数据载体安装:持久配置和输出存储
  • 资源优化:禁用共享内存和GPU加速以保持稳定性
  • 交叉平台的:可在macOS、Linux和Windows上使用Docker

快速开始

先决条件

  • 已安装Docker和Docker Compose
  • 主机上的端口8831可用

1.克隆和导航

git clone <your-repository-url>
cd playwright-mcp-on-docker-vnc

2.构建和运行

# Build and start the container
docker compose up --build

# Or run in detached mode
docker compose up --build -d

3.验证安装

MCP服务器将在 http://localhost:8831

检查容器日志:

docker compose logs -f

配置

config.json

主配置文件位于 config/config.json:

{
  "browser": {
    "browserName": "chromium",
    "launchOptions": {
      "executablePath": "/usr/local/bin/chrome-wrapper.sh"
    },
    "contextOptions": {}
  },
  "server": {
    "host": "0.0.0.0",
    "port": 8831
  },
  "outputDir": "/config/output",
  "imageResponses": "omit"
}

Chrome包装器配置

这 chrome-wrapper.sh 脚本会自动为Chrome配置最佳标志:

  • --no-sandbox:禁用Chrome沙盒以兼容Docker
  • --disable-dev-shm-usage:共享内存使用/tmp而不是/dev/shm
  • --disable-gpu:禁用GPU加速
  • --user-data-dir=/tmp/shared-browser-data:设置用户数据目录
  • --disable-features=Vulkan:禁用Vulkan功能
  • --use-gl=swiftshader:使用基于软件的GL实现

用法示例

与MCP客户端集成

此服务器与MCP客户端兼容,如Claude Desktop、VS Code扩展或支持MCP协议的自定义应用程序。

VS代码MCP配置示例(mcp.json):

{
  "servers": {
    "playwright": {
      "url": "http://localhost:8831/mcp"
    }
  }
}

文件结构

playwright-mcp-on-docker-vnc/
├── docker-compose.yml          # Docker Compose configuration
├── Dockerfile                  # Container build instructions
├── startup.sh                  # Container startup script
├── chrome-wrapper.sh           # Chrome executable wrapper
├── config/
│   ├── config.json            # MCP server configuration
│   └── output/                # Output directory for screenshots, etc.
│       └── .gitkeep
└── README.md                  # This file

故障排除

常见问题

  1. 端口8831已在使用中

    # Check what's using the port
    lsof -i :8831
    
    # Kill the process or change the port in docker-compose.yml
    
  2. Chrome无法启动

    • 检查Docker日志: docker compose logs
    • 确保足够的内存分配
    • 验证chrome-wrapper.sh是否具有执行权限
  3. 集装箱立即出口

    # Check container logs
    docker compose logs
    
    # Run interactively for debugging
    docker run -it playwright-mcp-vnc /bin/bash
    

调试模式

以交互方式运行容器进行调试:

# Build the image
docker compose build

# Run interactively
docker run -it --rm \
  -p 8831:8831 \
  -v $(pwd)/config:/config \
  playwright-mcp-vnc /bin/bash

发展

构建自定义图像

# Build with custom tag
docker build -t my-playwright-mcp .

# Run with custom image
docker run -p 8831:8831 my-playwright-mcp

修改Chrome标志

编辑 chrome-wrapper.sh 添加或修改Chrome启动标志:

exec "$REAL_CHROME" \
  --no-sandbox \
  --disable-dev-shm-usage \
  --your-custom-flag \
  "$@"

配置更改

修改 config/config.json 并重新启动容器:

docker compose restart

性能注意事项

  • 记忆:为容器分配至少2GB的RAM
  • 共享内存:设置使用 /tmp 而不是 /dev/shm 避免内存问题
  • 中央处理器浏览器自动化可能是CPU密集型的;考虑专用资源
  • 网络:确保网页加载的稳定互联网连接

安全说明

  • 集装箱与 --no-sandbox Chrome兼容性标志
  • 仅将端口8831暴露给受信任的网络
  • 如果在生产环境中部署,请考虑使用身份验证
  • 定期更新Playwright基础映像以获取安全补丁

贡献

  1. 分叉存储库
  2. 创建功能分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add some amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

致谢

支持

对于问题和疑问:

  1. 检查 故障排除 章节
  2. 审查 剧作家MCP文档
  3. 在此存储库中打开问题

备注:此设置针对开发和测试环境进行了优化。对于生产部署,考虑额外的安全强化和资源优化。