User Agents

Web browsers and everyday web tech, explained

Self-Host Headless Chrome: Docker, Puppeteer, Selenium

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Self-hosting headless Chrome with Docker gives you a consistent, isolated browser environment for automated testing, web scraping, or screenshot generation. You run a single container that exposes a debugging port, and then connect Puppeteer, Selenium, or Playwright to it—no local browser installation needed. This guide walks you through the setup, common pitfalls, and fixes.

What You'll Need

  • Docker installed and running on your machine (Docker Desktop on Windows/macOS, or Docker Engine on Linux).
  • Basic familiarity with the command line.
  • Node.js (if using Puppeteer or Playwright) or Python (if using Selenium) installed locally for the client scripts.
  • For Playwright, you'll also need the Playwright package installed in your project.

Step-by-Step

  1. Pull the official Chrome image. The most reliable image is zenika/alpine-chrome (supports both Chromium and Chrome). Run: docker pull zenika/alpine-chrome. Alternatively, use browserless/chrome for a pre-configured API, but the Zenika image is simpler for direct protocol access.
  2. Run the container with the debugging port exposed. Headless Chrome uses the DevTools Protocol on port 9222. Start the container with: docker run -d -p 9222:9222 --name chrome zenika/alpine-chrome --no-sandbox --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222. The --no-sandbox flag is required because running as root inside the container (common in Docker) disables the sandbox.
  3. Verify Chrome is running. Open http://localhost:9222/json/version in your browser. You should see a JSON response with the browser version and webSocket URL. If not, check the container logs: docker logs chrome.
  4. Connect Puppeteer. Install Puppeteer in your Node project (the package downloads its own Chrome, but you'll point it to the remote one instead). In your script, use: const browser = await puppeteer.connect({ browserURL: 'http://localhost:9222' });. Then create a page and use it as usual. Note: do not use puppeteer.launch()—that starts a new browser.
  5. Connect Selenium. Selenium uses the WebDriver protocol, not the DevTools protocol, so you need a separate WebDriver server. The easiest way is to use the selenium/standalone-chrome image, which includes both Chrome and the WebDriver. Run: docker run -d -p 4444:4444 --name selenium selenium/standalone-chrome. Then in your Selenium code, set the remote URL to http://localhost:4444/wd/hub with desired capabilities for Chrome.
  6. Connect Playwright. Playwright can also connect to a remote Chrome via the DevTools Protocol. Install Playwright in your Node project, then use: const browser = await playwright.chromium.connectOverCDP('http://localhost:9222');. This gives you a Browser object that works like a local one.
  7. Scale and manage the container. For production, consider using Docker Compose to define the service with resource limits (e.g., mem_limit: 1g) and restart policies. You can also run multiple containers on different ports for parallel workloads.

Common Mistakes

  • Forgetting --no-sandbox: Without it, Chrome fails to start with a sandbox error in most Docker containers.
  • Using localhost inside the container: If your client script runs inside another container, use the host machine's IP or the container name (if on the same Docker network) instead of localhost.
  • Not exposing the port: The -p 9222:9222 flag is essential; otherwise, the port is not accessible from the host.
  • Mixing protocols: Selenium cannot talk to the DevTools port directly; it needs a WebDriver server. Similarly, Puppeteer/Playwright cannot talk to the WebDriver port.
  • Running out of memory: Chrome is memory-hungry. If your container crashes, check docker stats and increase memory limits.

Tips

  • Use a named volume or bind mount to persist browser profiles if you need to keep cookies or local storage between sessions.
  • Add --disable-dev-shm-usage to the Chrome flags. The default /dev/shm size in Docker is 64MB, which causes crashes on heavy pages. This flag makes Chrome use /tmp instead.
  • For screenshots or PDFs, set a viewport size in your client code; the headless browser defaults to 800x600.
  • Use a healthcheck in Docker Compose to ensure Chrome is ready before your script runs.
  • If you need to run multiple concurrent sessions, use a pool of containers or a tool like browserless that handles queuing.

Troubleshooting

  • Chrome fails to start: Check logs with docker logs chrome. Common errors: missing --no-sandbox, or --remote-debugging-address=0.0.0.0 not set (so it only listens on localhost inside the container).
  • Cannot connect from client: Ensure the port is mapped correctly (docker ps to see ports). If using a remote server, open the firewall port. Test with curl to http://localhost:9222/json/version.
  • Puppeteer/Playwright connect but pages don't load: Check if the page requires JavaScript or has heavy resources. Increase the container's memory and add --disable-dev-shm-usage.
  • Selenium times out: The Selenium image may take longer to start. Wait for the log line "Selenium Server is up and running" before connecting. Also ensure you're using the correct URL (/wd/hub).
  • Container restarts repeatedly: Check if it's OOM-killed. Use docker inspect to see exit code (137 = OOM). Increase memory or reduce concurrent tabs.

Frequently Asked Questions

Can I use the same container for Puppeteer and Playwright simultaneously?

Yes, both can connect to the same Chrome instance via the DevTools protocol, but they will share the same browser context. For isolation, use separate containers or create new browser contexts in your code.

Is it safe to run Chrome as root in Docker?

It's common but not ideal. For better security, create a non-root user in your Dockerfile or use a user namespace. The --no-sandbox flag is needed when running as root, which reduces security. In production, run as a non-root user.

How do I keep the browser alive between script runs?

As long as the container is running, the browser stays up. You can reuse the same connection across multiple scripts. To keep it alive after a script exits, just don't call browser.close().

Can I take screenshots with this setup?

Yes, both Puppeteer and Playwright have page.screenshot() methods. Selenium also has get_screenshot_as_file(). Works exactly as with a local browser.

What if I need a specific Chrome version?

You can tag the image with a version, e.g., zenika/alpine-chrome:latest or a specific version like zenika/alpine-chrome:87. Check Docker Hub for available tags.

How do I use this with CI/CD?

In CI, run the container as a service in your pipeline (e.g., GitHub Actions services or GitLab services). Then connect to it using the service name as the host.