Fix Chrome Headless Unknown Error in Docker
In this article
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
- Identify the exact error message. Run your script and capture the full output. Common messages include
unknown error: DevToolsActivePort file doesn't existorFailed to move to new namespace. This determines which fix applies. - 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--headlessfor older versions), and--remote-debugging-port=9222. For example, in Puppeteer:puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'] }). - Install missing system libraries. Chrome requires many shared libraries. Add this to your Dockerfile (for Debian/Ubuntu):
Then rebuild your image.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 - Increase shared memory size. The default
/dev/shmin Docker is 64MB, which Chrome often exceeds. Either add--disable-dev-shm-usageto your Chrome flags (as in step 2), or run your container with--shm-size=1gb(e.g.,docker run --shm-size=1gb your-image). - Disable the sandbox explicitly. If you're not running as root, the sandbox may fail. Add
--no-sandboxand--disable-setuid-sandboxto your Chrome launch arguments. If you're using Puppeteer, you can also setpuppeteer.launch({ args: ['--no-sandbox'] }). - Use a dedicated Chrome installation. If you're using Puppeteer, ensure the browser is downloaded during build. Add
RUN npx puppeteer browsers install chrome(orpuppeteer installfor newer versions) to your Dockerfile after installing Puppeteer. - Run as non-root (optional but recommended). Create a user and run Chrome with
--no-sandboxonly if necessary; otherwise, setUSER node(or your user) in the Dockerfile and ensure permissions on the working directory. - 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-sandboxwhen 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-usageflag, which causes crashes due to small/dev/shm. - Not installing all required libraries—missing
libgbm1orlibnss3often leads to cryptic errors. - Building the image without running
apt-get updatefirst, causing package installation failures.
Tips
- Use the official
puppeteerDocker 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-extensionsand--disable-background-networkingto 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:blankto see the error output. - If Chrome crashes with a segmentation fault: This often indicates missing
libgbm1orlibnss3. Reinstall those packages and ensure they are the correct architecture. - If the container runs out of memory: Increase the container memory limit with
--memory=2gor add--disable-dev-shm-usageto 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_ADMINor, 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.