Fix Chrome Timeouts in Headless Docker Selenium
In this article
If your Selenium tests run inside a Docker container with headless Chrome and you're hitting timeouts, the root cause is usually resource limits, missing dependencies, or incorrect Chrome flags. This guide walks you through the most effective fixes, from adjusting Chrome's startup options to tuning Docker's resource allocation. You'll need a working Docker setup and a Selenium project (Python, Java, or Node.js) that currently runs but occasionally or always times out.
What You'll Need
- A Dockerfile or docker-compose.yml for your Selenium environment
- Access to your Selenium test code (where you instantiate the WebDriver)
- Basic command-line knowledge to run Docker commands
- Optional: a tool like
docker statsto monitor resource usage
Step-by-Step
- Add the recommended Chrome flags. In your WebDriver setup, pass these flags to ChromeOptions:
--headless,--no-sandbox,--disable-dev-shm-usage,--disable-gpu, and--remote-debugging-port=9222. The--disable-dev-shm-usageflag forces Chrome to use/tmpinstead of the small/dev/shmthat Docker provides, which is a common cause of crashes and timeouts. Example (Python):from selenium import webdriver from selenium.webdriver.chrome.options import Options opts = Options() opts.add_argument('--headless') opts.add_argument('--no-sandbox') opts.add_argument('--disable-dev-shm-usage') opts.add_argument('--disable-gpu') opts.add_argument('--remote-debugging-port=9222') driver = webdriver.Chrome(options=opts) - Increase Docker shared memory. Even with
--disable-dev-shm-usage, some sites still need more shared memory. In yourdocker runcommand, add--shm-size=2g(or more). If using docker-compose, addshm_size: '2g'under the service. This gives Chrome enough space to render pages without hitting the default 64MB limit. - Set explicit timeouts in your WebDriver. Configure page load timeout, script timeout, and implicit wait to reasonable values (e.g., 30 seconds). This prevents infinite hangs. In Python:
driver.set_page_load_timeout(30) driver.set_script_timeout(30) driver.implicitly_wait(10)
- Use a lightweight base image. Instead of the full
selenium/standalone-chromeimage, consider usingselenium/node-chromewith a custom script, or even a minimal image likepython:3.9-slimand install Chrome manually. Fewer dependencies mean fewer chances of conflicts and faster startup. - Allocate enough CPU and memory. Check your Docker container's limits. If you're using Docker Desktop, go to Settings → Resources and increase memory (e.g., to 4GB) and CPU (e.g., to 2 cores). For Docker Engine on Linux, use
--cpus=2and--memory=4gin your run command. - Disable Chrome's background throttling. Add the flag
--disable-background-timer-throttlingand--disable-renderer-backgrounding. These prevent Chrome from slowing down background tabs, which can cause timeouts in headless mode. - Use the WebDriver manager. If you're manually downloading ChromeDriver, version mismatches can cause timeouts. Use a WebDriver manager like
webdriver-manager(Python) orchromedriver(Node.js) to automatically match the driver to your Chrome version. - Run a health check. Before running tests, verify Chrome starts correctly inside the container. Exec into the container and run
google-chrome --versionand a simple Selenium script that navigates to a known page. This isolates whether the timeout is from Chrome itself or your test code.
Common Mistakes
- Forgetting
--no-sandbox. Running as root in Docker requires this flag, otherwise Chrome fails to start and tests hang. - Ignoring
/dev/shm. Many people only add--disable-dev-shm-usagebut don't increase--shm-size. For heavy pages, both are needed. - Using outdated ChromeDriver. If your Chrome version updates but the driver doesn't, you'll get random timeouts.
- Setting too-short timeouts. A 5-second page load timeout might work locally but fail under Docker's slower I/O. Use 30 seconds or more.
- Not cleaning up the driver. Leaving browser processes running after a test can exhaust memory, causing subsequent timeouts. Always call
driver.quit()in afinallyblock.
Tips
- Use
--single-processflag in some cases to reduce memory usage, but note it can cause instability; test first. - If you're using Chrome 118+, consider the new headless mode (
--headless=new) which is more stable and supports more features. - Run Docker with
--initto handle zombie processes, which can accumulate and slow down the container. - Use a proxy or a local test server to reduce network latency if your tests hit external sites.
- Log Chrome's stderr output to a file to see any cryptic errors that might indicate the real problem.
Troubleshooting
If you still get timeouts after applying the steps above, try these diagnostics:
- Check container logs. Run
docker logs <container_id>to see if Chrome prints errors like "Failed to connect to the driver" or "DevToolsActivePort file doesn't exist." - Test with a simple page. Replace your test URL with a basic HTML page served locally. If that works, the issue is network or page complexity, not Chrome itself.
- Increase verbosity. Add
--verboseto the ChromeDriver log or useservice_log_pathin your WebDriver options to get more details. - Monitor resource usage. While a test runs, use
docker statsto see if memory or CPU hits 100%. If memory is maxed, increase--shm-sizeor reduce parallel tests. - Try a different base image. If you're using a custom image, switch to
selenium/standalone-chrometemporarily to see if the problem persists. If it doesn't, your image is missing something. - Check for DNS issues. Inside the container, run
getent hosts example.com. If it fails, your Docker network may need a custom DNS setting.
Frequently Asked Questions
Why does Chrome crash in Docker but not on my local machine?
Docker containers have limited shared memory (/dev/shm) by default (64MB), which Chrome uses heavily for rendering. The --disable-dev-shm-usage flag or increasing --shm-size fixes this.
Do I need both --no-sandbox and --disable-dev-shm-usage?
Yes, in most Docker setups. --no-sandbox is needed because you're running as root, and --disable-dev-shm-usage prevents Chrome from using the small shared memory space.
What is the best base image for Selenium in Docker?
The official selenium/standalone-chrome image is reliable and pre-configured. If you need a smaller image, use selenium/node-chrome with a custom script, but be prepared to handle dependencies yourself.
How do I increase Docker shared memory in docker-compose?
Add shm_size: '2g' to your service definition in the docker-compose.yml file, under the service name.
Why does my test pass locally but time out in Docker?
Local machines often have more memory and CPU, and Chrome runs in non-headless mode. In Docker, headless mode, limited resources, and network isolation can cause slower page loads, so you need longer timeouts and the flags mentioned above.
Can I use Chrome's new headless mode in Docker?
Yes, use --headless=new instead of --headless. It's more stable and supports newer features, but test it first as some versions may have quirks.