Skip to content

Commit 756e449

Browse files
Add Docker support for MCP Playwright Server
- Add Dockerfile for containerized execution - Add .dockerignore to optimize Docker builds - Add docker-compose.yml for easy container management - Add docker-build.sh script for simplified building - Add comprehensive DOCKER.md documentation - Update README.md with Docker usage instructions Co-authored-by: executeautomation <10337030+executeautomation@users.noreply.github.com>
1 parent eb7df71 commit 756e449

File tree

6 files changed

+448
-29
lines changed

6 files changed

+448
-29
lines changed

.dockerignore

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Node modules - commented out to allow copying from host
2+
# node_modules
3+
npm-debug.log
4+
5+
# Build output - commented out to allow copying pre-built dist
6+
# dist
7+
8+
# Test and coverage
9+
coverage
10+
*.test.ts
11+
__tests__
12+
13+
# Git
14+
.git
15+
.gitignore
16+
.gitattributes
17+
18+
# GitHub
19+
.github
20+
21+
# Documentation
22+
docs
23+
*.md
24+
!README.md
25+
26+
# IDE
27+
.vscode
28+
.idea
29+
30+
# OS
31+
.DS_Store
32+
Thumbs.db
33+
34+
# Temporary files
35+
*.tmp
36+
*.log
37+
38+
# Test files
39+
test-import.js
40+
run-tests.cjs
41+
run-tests.js
42+
jest.config.cjs
43+
44+
# Config files not needed in container
45+
tsconfig.test.json

DOCKER.md

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
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

Dockerfile

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,24 @@
1-
# Generated by https://smithery.ai. See: https://smithery.ai/docs/config#dockerfile
2-
# Use Node.js image for building the project
3-
FROM node:20-alpine AS builder
1+
# Multi-stage Dockerfile for MCP Playwright Server
2+
# This Dockerfile expects the project to be built before running docker build
3+
# Run `npm install --omit=dev && npm run build` before building the image
44

5-
# Set the working directory
6-
WORKDIR /app
7-
8-
# Copy package.json and package-lock.json
9-
COPY package.json package-lock.json ./
10-
11-
# Install dependencies without running scripts to prevent automatic build
12-
RUN npm install --ignore-scripts
5+
FROM node:20-slim AS base
136

14-
# Copy the entire source directory
15-
COPY src ./src
16-
COPY tsconfig.json ./
17-
18-
# Build the project
19-
RUN npm run build
7+
# Set working directory
8+
WORKDIR /app
209

21-
# Use a minimal Node.js image for running the project
22-
FROM node:20-alpine AS release
10+
# Copy package files for reference
11+
COPY package*.json ./
2312

24-
# Set the working directory
25-
WORKDIR /app
13+
# Copy node_modules from host (production dependencies only)
14+
# Make sure to run `npm install --omit=dev` before building
15+
COPY node_modules ./node_modules
2616

27-
# Copy the built files from the builder stage
28-
COPY --from=builder /app/dist ./dist
29-
COPY --from=builder /app/package.json ./package.json
30-
COPY --from=builder /app/package-lock.json ./package-lock.json
17+
# Copy the pre-built application
18+
COPY dist ./dist
3119

32-
# Install production dependencies
33-
RUN npm ci --ignore-scripts --omit=dev
20+
# Expose stdio for MCP communication
21+
# MCP servers communicate via stdio, so no port exposure needed
3422

35-
# Set the command to run the server
36-
ENTRYPOINT ["node", "dist/index.js"]
23+
# Run the server
24+
CMD ["node", "dist/index.js"]

0 commit comments

Comments
 (0)