|
| 1 | +# Docker Support for Playwright MCP Server |
| 2 | + |
| 3 | +This document provides detailed instructions for running the Playwright MCP Server in Docker. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Playwright MCP Server can be containerized using Docker, providing: |
| 8 | +- Isolated execution environment |
| 9 | +- Consistent runtime across different systems |
| 10 | +- Easy deployment and distribution |
| 11 | +- Simplified dependency management |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- Docker installed on your system ([Install Docker](https://docs.docker.com/get-docker/)) |
| 16 | +- Docker Compose (optional, usually included with Docker Desktop) |
| 17 | +- The project built locally (`npm run build`) |
| 18 | + |
| 19 | +## Building the Docker Image |
| 20 | + |
| 21 | +The Dockerfile is designed to work with pre-built artifacts and dependencies from your local build. Follow these steps: |
| 22 | + |
| 23 | +1. **Install production dependencies and build the project:** |
| 24 | + ```bash |
| 25 | + npm install --omit=dev |
| 26 | + npm run build |
| 27 | + ``` |
| 28 | + |
| 29 | + Or use the provided build script: |
| 30 | + ```bash |
| 31 | + chmod +x docker-build.sh |
| 32 | + ./docker-build.sh |
| 33 | + ``` |
| 34 | + |
| 35 | +2. **Manually build the Docker image:** |
| 36 | + ```bash |
| 37 | + docker build -t mcp-playwright:latest . |
| 38 | + ``` |
| 39 | + |
| 40 | + Or with a specific tag: |
| 41 | + ```bash |
| 42 | + docker build -t mcp-playwright:1.0.6 . |
| 43 | + ``` |
| 44 | + |
| 45 | +**Important**: The Dockerfile copies `node_modules` and `dist` from your local build directory. Make sure you have installed dependencies with `--omit=dev` flag before building the Docker image to keep the image size minimal. |
| 46 | + |
| 47 | +## Running the Server |
| 48 | + |
| 49 | +### Interactive Mode (Recommended for MCP) |
| 50 | + |
| 51 | +Since MCP servers communicate via stdin/stdout, run the container in interactive mode: |
| 52 | + |
| 53 | +```bash |
| 54 | +docker run -i --rm mcp-playwright:latest |
| 55 | +``` |
| 56 | + |
| 57 | +Flags explained: |
| 58 | +- `-i`: Keep STDIN open for interactive communication |
| 59 | +- `--rm`: Automatically remove the container when it exits |
| 60 | +- `mcp-playwright:latest`: The image name and tag |
| 61 | + |
| 62 | +### Using Docker Compose |
| 63 | + |
| 64 | +A `docker-compose.yml` file is provided for convenience: |
| 65 | + |
| 66 | +```bash |
| 67 | +# Start the server |
| 68 | +docker compose run --rm playwright-mcp |
| 69 | + |
| 70 | +# Build and run |
| 71 | +docker compose build |
| 72 | +docker compose run --rm playwright-mcp |
| 73 | +``` |
| 74 | + |
| 75 | +## Integration with MCP Clients |
| 76 | + |
| 77 | +### Claude Desktop Configuration |
| 78 | + |
| 79 | +To use the Dockerized MCP server with Claude Desktop, update your configuration file: |
| 80 | + |
| 81 | +**Location**: |
| 82 | +- macOS: `~/Library/Application Support/Claude/claude_desktop_config.json` |
| 83 | +- Windows: `%APPDATA%\Claude\claude_desktop_config.json` |
| 84 | +- Linux: `~/.config/Claude/claude_desktop_config.json` |
| 85 | + |
| 86 | +**Configuration:** |
| 87 | +```json |
| 88 | +{ |
| 89 | + "mcpServers": { |
| 90 | + "playwright-docker": { |
| 91 | + "command": "docker", |
| 92 | + "args": ["run", "-i", "--rm", "mcp-playwright:latest"] |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### VS Code MCP Extension |
| 99 | + |
| 100 | +For VS Code with MCP extension: |
| 101 | + |
| 102 | +```json |
| 103 | +{ |
| 104 | + "name": "playwright-docker", |
| 105 | + "command": "docker", |
| 106 | + "args": ["run", "-i", "--rm", "mcp-playwright:latest"] |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +## Environment Variables |
| 111 | + |
| 112 | +You can pass environment variables to configure the server: |
| 113 | + |
| 114 | +```bash |
| 115 | +docker run -i --rm \ |
| 116 | + -e PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 \ |
| 117 | + mcp-playwright:latest |
| 118 | +``` |
| 119 | + |
| 120 | +In docker-compose.yml: |
| 121 | +```yaml |
| 122 | +services: |
| 123 | + playwright-mcp: |
| 124 | + environment: |
| 125 | + - PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 |
| 126 | + - NODE_ENV=production |
| 127 | +``` |
| 128 | +
|
| 129 | +## Volume Mounts |
| 130 | +
|
| 131 | +If you need to persist data or share files with the container: |
| 132 | +
|
| 133 | +```bash |
| 134 | +docker run -i --rm \ |
| 135 | + -v $(pwd)/data:/app/data \ |
| 136 | + mcp-playwright:latest |
| 137 | +``` |
| 138 | + |
| 139 | +In docker-compose.yml: |
| 140 | +```yaml |
| 141 | +services: |
| 142 | + playwright-mcp: |
| 143 | + volumes: |
| 144 | + - ./data:/app/data |
| 145 | + - ./screenshots:/app/screenshots |
| 146 | +``` |
| 147 | +
|
| 148 | +## Troubleshooting |
| 149 | +
|
| 150 | +### Container Exits Immediately |
| 151 | +
|
| 152 | +MCP servers wait for input on stdin. Ensure you're running with `-i` flag: |
| 153 | +```bash |
| 154 | +docker run -i --rm mcp-playwright:latest |
| 155 | +``` |
| 156 | + |
| 157 | +### Browser Not Found |
| 158 | + |
| 159 | +The Docker image skips browser downloads by default to reduce size. Playwright will download browsers on first use. To pre-install browsers, create a custom Dockerfile: |
| 160 | + |
| 161 | +```dockerfile |
| 162 | +FROM mcp-playwright:latest |
| 163 | +
|
| 164 | +# Install Playwright browsers |
| 165 | +RUN npx playwright install chromium --with-deps |
| 166 | +``` |
| 167 | + |
| 168 | +### Permission Issues |
| 169 | + |
| 170 | +If you encounter permission issues with mounted volumes: |
| 171 | +```bash |
| 172 | +docker run -i --rm \ |
| 173 | + -v $(pwd)/data:/app/data \ |
| 174 | + --user $(id -u):$(id -g) \ |
| 175 | + mcp-playwright:latest |
| 176 | +``` |
| 177 | + |
| 178 | +## Advanced Usage |
| 179 | + |
| 180 | +### Custom Network Configuration |
| 181 | + |
| 182 | +To run the server on a custom network: |
| 183 | + |
| 184 | +```bash |
| 185 | +docker network create mcp-network |
| 186 | +docker run -i --rm --network mcp-network mcp-playwright:latest |
| 187 | +``` |
| 188 | + |
| 189 | +### Resource Limits |
| 190 | + |
| 191 | +Limit CPU and memory usage: |
| 192 | + |
| 193 | +```bash |
| 194 | +docker run -i --rm \ |
| 195 | + --cpus="2.0" \ |
| 196 | + --memory="2g" \ |
| 197 | + mcp-playwright:latest |
| 198 | +``` |
| 199 | + |
| 200 | +In docker-compose.yml: |
| 201 | +```yaml |
| 202 | +services: |
| 203 | + playwright-mcp: |
| 204 | + deploy: |
| 205 | + resources: |
| 206 | + limits: |
| 207 | + cpus: '2.0' |
| 208 | + memory: 2G |
| 209 | +``` |
| 210 | + |
| 211 | +### Health Checks |
| 212 | + |
| 213 | +Add a health check to your docker-compose.yml: |
| 214 | + |
| 215 | +```yaml |
| 216 | +services: |
| 217 | + playwright-mcp: |
| 218 | + healthcheck: |
| 219 | + test: ["CMD", "node", "-e", "process.exit(0)"] |
| 220 | + interval: 30s |
| 221 | + timeout: 10s |
| 222 | + retries: 3 |
| 223 | +``` |
| 224 | + |
| 225 | +## Image Size Optimization |
| 226 | + |
| 227 | +The current Dockerfile is optimized for size: |
| 228 | +- Uses Alpine Linux base image (~50MB) |
| 229 | +- Multi-stage build support (when building from source) |
| 230 | +- Production dependencies only |
| 231 | +- Skips browser downloads by default |
| 232 | + |
| 233 | +Current image size: ~200MB (without browsers) |
| 234 | + |
| 235 | +## Security Considerations |
| 236 | + |
| 237 | +1. **Run as non-root user** (optional but recommended): |
| 238 | + ```dockerfile |
| 239 | + FROM mcp-playwright:latest |
| 240 | + USER node |
| 241 | + ``` |
| 242 | + |
| 243 | +2. **Read-only root filesystem** (if applicable): |
| 244 | + ```bash |
| 245 | + docker run -i --rm --read-only mcp-playwright:latest |
| 246 | + ``` |
| 247 | + |
| 248 | +3. **Scan for vulnerabilities:** |
| 249 | + ```bash |
| 250 | + docker scan mcp-playwright:latest |
| 251 | + ``` |
| 252 | + |
| 253 | +## Building from Source in Docker |
| 254 | + |
| 255 | +If you prefer to build inside Docker (not using pre-built dist): |
| 256 | + |
| 257 | +```dockerfile |
| 258 | +FROM node:20-alpine AS builder |
| 259 | +
|
| 260 | +WORKDIR /app |
| 261 | +COPY package*.json ./ |
| 262 | +ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 |
| 263 | +RUN npm ci |
| 264 | +
|
| 265 | +COPY src ./src |
| 266 | +COPY tsconfig.json ./ |
| 267 | +RUN npm run build |
| 268 | +
|
| 269 | +FROM node:20-alpine |
| 270 | +WORKDIR /app |
| 271 | +COPY --from=builder /app/dist ./dist |
| 272 | +COPY --from=builder /app/package*.json ./ |
| 273 | +RUN npm ci --only=production |
| 274 | +CMD ["node", "dist/index.js"] |
| 275 | +``` |
| 276 | + |
| 277 | +## Support |
| 278 | + |
| 279 | +For issues related to Docker: |
| 280 | +- Check the main [README.md](./README.md) for general information |
| 281 | +- Report Docker-specific issues on [GitHub Issues](https://github.com/executeautomation/mcp-playwright/issues) |
| 282 | +- Tag your issue with `docker` label |
0 commit comments