User Agents

Web browsers and everyday web tech, explained

Fix Chrome Headless Unknown Error in Docker

· 4 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

When running Chrome in headless mode inside a Docker container, you may encounter an "unknown error" that often stems from missing dependencies, sandbox restrictions, or resource limits. This guide walks you through the most reliable fixes, from adding the right flags to installing required libraries, so you can get your headless browser working consistently.

What You'll Need

  • A Dockerfile or running container based on a Debian/Ubuntu image (e.g., python:3.9-slim, node:16)
  • Root or sudo access inside the container to install packages
  • Basic familiarity with editing Dockerfiles and running docker build / docker run

Step-by-Step

  1. Identify the exact error message. Run your script and capture the full output. Common messages include unknown error: DevToolsActivePort file doesn't exist or Failed to move to new namespace. This determines which fix applies.
  2. Add the recommended Chrome flags. In your code, when launching Chrome, include these flags:
    --no-sandbox, --disable-dev-shm-usage, --disable-gpu, --headless=new (or --headless for older versions), and --remote-debugging-port=9222. For example, in Puppeteer: puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'] }).
  3. Install missing system libraries. Chrome requires many shared libraries. Add this to your Dockerfile (for Debian/Ubuntu):
    RUN apt-get update && apt-get install -y \
      fonts-liberation \
      libasound2 \
      libatk-bridge2.0-0 \
      libatk1.0-0 \
      libcups2 \
      libdbus-1-3 \
      libdrm2 \
      libgbm1 \
      libgtk-3-0 \
      libnspr4 \
      libnss3 \
      libx11-xcb1 \
      libxcomposite1 \
      libxdamage1 \
      libxrandr2 \
      xdg-utils \
      --no-install-recommends
    Then rebuild your image.
  4. Increase shared memory size. The default /dev/shm in Docker is 64MB, which Chrome often exceeds. Either add --disable-dev-shm-usage to your Chrome flags (as in step 2), or run your container with --shm-size=1gb (e.g., docker run --shm-size=1gb your-image).
  5. Disable the sandbox explicitly. If you're not running as root, the sandbox may fail. Add --no-sandbox and --disable-setuid-sandbox to your Chrome launch arguments. If you're using Puppeteer, you can also set puppeteer.launch({ args: ['--no-sandbox'] }).
  6. Use a dedicated Chrome installation. If you're using Puppeteer, ensure the browser is downloaded during build. Add RUN npx puppeteer browsers install chrome (or puppeteer install for newer versions) to your Dockerfile after installing Puppeteer.
  7. Run as non-root (optional but recommended). Create a user and run Chrome with --no-sandbox only if necessary; otherwise, set USER node (or your user) in the Dockerfile and ensure permissions on the working directory.
  8. Test with a minimal script. Create a simple script that launches Chrome, navigates to a blank page, and exits. This isolates whether the issue is with your code or the environment.

Common Mistakes

  • Forgetting to add --no-sandbox when running as root. Root users need this flag because Chrome's sandbox cannot run as root.
  • Using an outdated Chrome version that doesn't support --headless=new. Check your Chrome version and use the appropriate flag.
  • Missing the --disable-dev-shm-usage flag, which causes crashes due to small /dev/shm.
  • Not installing all required libraries—missing libgbm1 or libnss3 often leads to cryptic errors.
  • Building the image without running apt-get update first, causing package installation failures.

Tips

  • Use the official puppeteer Docker image as a base if you're using Puppeteer; it already has all dependencies and Chrome installed.
  • Pin your Chrome version to avoid unexpected updates that break your setup.
  • Add --disable-extensions and --disable-background-networking to reduce resource usage.
  • If you need to run multiple Chrome instances, consider using a pool or increasing container memory.
  • Log the Chrome stderr output to a file to see more detailed errors.

Troubleshooting

  • If you still get "DevToolsActivePort file doesn't exist": This usually means Chrome failed to start. Check that all libraries are installed. Try running Chrome directly inside the container with google-chrome --headless --no-sandbox --disable-gpu --dump-dom about:blank to see the error output.
  • If Chrome crashes with a segmentation fault: This often indicates missing libgbm1 or libnss3. Reinstall those packages and ensure they are the correct architecture.
  • If the container runs out of memory: Increase the container memory limit with --memory=2g or add --disable-dev-shm-usage to reduce memory pressure.
  • If you're using Alpine Linux: Chrome is not officially supported on Alpine. Switch to a Debian-based image or use Chromium from the community repository, but expect more issues.
  • If the error mentions namespaces or permissions: Run the container with --cap-add=SYS_ADMIN or, more simply, use --no-sandbox.

Frequently Asked Questions

Why does Chrome need --no-sandbox in Docker?

Docker containers often run as root, and Chrome's sandbox refuses to run as root. The --no-sandbox flag disables this security feature, which is acceptable in a controlled container environment.

What does --disable-dev-shm-usage do?

It tells Chrome to use /tmp instead of /dev/shm for shared memory, avoiding the 64MB limit that causes crashes.

Can I use Chrome headless without installing all those libraries?

No, Chrome depends on many system libraries. You must install them or use a pre-built image like puppeteer that includes them.

Is it safe to run Chrome without the sandbox?

In a container that is isolated from the host, the risk is low. However, if you're processing untrusted web content, consider running as a non-root user and keeping the sandbox enabled if possible.

Why does my Chrome version not support --headless=new?

The --headless=new flag was introduced in Chrome 112. If you have an older version, use --headless instead.

How do I debug if Chrome starts but exits immediately?

Run Chrome manually with --dump-dom and --enable-logging=stderr --v=1 to see verbose errors. Also check your script for syntax errors.