Run Chrome Headless Shell in Docker: Step-by-Step Guide
In this article
Running the Chrome Headless Shell in Docker allows you to automate web browsing, scrape data, or perform end-to-end testing in a consistent, isolated environment without needing a graphical user interface. By containerizing the browser, you eliminate the "it works on my machine" problem, ensuring that the browser version and system dependencies remain identical across development, staging, and production environments.
What You'll Need
- Docker installed on your local machine or server.
- A basic understanding of the command line (Terminal, PowerShell, or Bash).
- A text editor (such as VS Code, Sublime Text, or Vim) to create the Dockerfile.
- Root or sudo privileges to run Docker commands.
Step-by-Step
- Choose a Base Image: Start with a stable Linux distribution. Debian or Ubuntu are recommended because they have comprehensive package repositories for the dependencies Chrome requires. In your Dockerfile, start with
FROM ubuntu:22.04or a similar stable version. - Install System Dependencies: Chrome requires several shared libraries to run, even in headless mode. You must update the package manager and install these dependencies. Add the following to your Dockerfile:
RUN apt-get update && apt-get install -y \n wget \n gnupg \n ca-certificates \n apt-transport-https \n libglib2.0-0 \n libnss3 \n libatk1.0-0 \n libatk-bridge2.0-0 \n libcups2 \n libdrm2 \n libxkbcommon0 \n libxcomposite1 \n libxdamage1 \n libxext6 \n libxfixes3 \n libxrandr2 \n libgbm1 \n libasound2
- Download and Install Chrome Headless Shell: Instead of the full Chrome browser, use the
chrome-headless-shellbinary for a smaller footprint and better performance in containers. Usewgetto pull the latest stable build from the Google storage APIs and extract it to a directory like/opt/chrome. - Configure the User Environment: Running Chrome as the root user is discouraged and often requires a specific flag to work. It is a best practice to create a non-privileged user within the Dockerfile using
RUN useradd -m chromeuserand then switch to that user withUSER chromeuser. - Set Up the Entry Point: Define how the container should start. If you are using a library like Puppeteer or Playwright, your entry point will likely be a Node.js script. If you are running the shell directly, your
CMDwill be the path to the chrome-headless-shell binary followed by the necessary flags. - Build the Image: Run the build command in your terminal from the directory containing the Dockerfile:
docker build -t chrome-headless-docker .
- Run the Container with Necessary Flags: When starting the container, you must pass specific flags to Chrome to bypass the sandbox restrictions inherent in Docker. Use the following command:
docker run -it chrome-headless-docker /opt/chrome/chrome-headless-shell --headless --no-sandbox --disable-gpu --remote-debugging-port=9222
Common Mistakes
- Forgetting the --no-sandbox Flag: This is the most frequent error. Chrome's sandbox requires privileges that Docker containers do not have by default. Without
--no-sandbox, the browser will crash immediately on startup. - Missing Shared Libraries: Many users install Chrome but forget the underlying X11 or GLib libraries. This results in "shared library not found" errors when attempting to launch the binary.
- Using a Full Browser Image: Installing the full Google Chrome browser instead of the headless shell increases image size by hundreds of megabytes without providing any benefit for automated tasks.
- Ignoring Zombie Processes: Chrome often leaves "zombie" processes behind. Failing to use an init system (like
--initin Docker run) can lead to memory leaks and container crashes over time.
Tips
- Use --init: Always run your container with the
--initflag (e.g.,docker run --init ...). This ensures that signal handling is correct and zombie processes are reaped. - Optimize Layer Caching: Group your
apt-get updateandapt-get installcommands into a singleRUNstatement to reduce the number of image layers and decrease the final image size. - Shared Memory: Chrome uses
/dev/shmfor its internal memory management. Docker's default size for this is often too small (64MB), which can cause crashes on heavy pages. Use--shm-size=2gbwhen running the container to prevent this. - Version Pinning: Instead of downloading "latest," pin the Chrome version in your download URL to ensure your automation scripts don't break when Google releases a new version.
Troubleshooting
If the container exits immediately, check the logs using docker logs [container_id]. If you see an error regarding libnss3.so or libgbm.so, it means a dependency is missing; return to the installation step and ensure all listed libraries are included.
If you encounter a "DevToolsActivePort file doesn't exist" error, this is usually a sign that the browser crashed before it could initialize. This is almost always caused by the lack of the --no-sandbox flag or insufficient shared memory (--shm-size). Verify these two settings first.
If the browser is running but pages are loading extremely slowly, check if you are hitting CPU or RAM limits imposed by the Docker daemon. Headless Chrome is resource-intensive; ensure the container has at least 2GB of RAM available.
Frequently Asked Questions
What is the difference between Chrome and Chrome Headless Shell?
Chrome Headless Shell is a stripped-down version of the browser specifically designed for automation. It removes the browser UI entirely, reducing memory overhead and startup time compared to running the full Chrome browser in headless mode.
Do I need to install a GPU driver in Docker?
No. Since you are running in headless mode, you should use the --disable-gpu flag. This tells Chrome to use software rendering instead of searching for hardware acceleration, which is not available in a standard container.
Can I run multiple Chrome instances in one container?
Yes, but it is not recommended. It is better to scale by running multiple containers. If you must run multiple instances, ensure you assign a unique --remote-debugging-port to each instance to avoid conflicts.
Why is my Docker image so large?
Chrome and its dependencies are heavy. To reduce size, use a multi-stage build or clean up the apt cache in the same layer where you installed packages using rm -rf /var/lib/apt/lists/*.
Is --no-sandbox safe to use?
In a controlled Docker environment where you only visit trusted URLs, it is acceptable. However, if you are browsing untrusted third-party content, be aware that disabling the sandbox removes a critical layer of security between the browser and the container OS.