Running Puppeteer in a Docker container can sometimes lead to unexpected errors, especially when interacting with the headless browser. One such error is the “ProtocolError: Network.enable timed out” message, which occurs when trying to get a new browser page using Puppeteer in a Docker environment. This issue can be particularly frustrating, but it’s often related to the browser’s inability to start properly within the container.
Thank me by sharing on Twitter 🙏
Error Description
The exact error message you may encounter looks like this:
ProtocolError: Network.enable timed out. Increase the 'protocolTimeout' setting in launch/connect calls for a higher timeout if needed.
This error typically occurs during the execution of await browser.newPage()
in your Puppeteer script, preventing you from successfully launching a new page in the browser.
Cause of the Error
The root cause of this error is likely that the Chrome executable is not starting properly within the Docker container. This can be due to a variety of factors, including resource constraints, missing dependencies, or the specific configuration of the Docker environment.
Solution
The solution that worked for me involved adding the --disable-gpu
flag to the Puppeteer launch options. Disabling GPU acceleration in a Docker environment can resolve issues related to Chrome’s startup, particularly when GPU resources are unavailable or unnecessary.
Logitech M185 Wireless Mouse, 2.4GHz with USB Mini Receiver, 12-Month Battery Life, 1000 DPI Optical Tracking, Ambidextrous PC/Mac/Laptop - Swift Grey
$13.95 (as of April 2, 2025 14:18 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Careless People: A Cautionary Tale of Power, Greed, and Lost Idealism
$17.71 (as of April 1, 2025 14:16 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)The AI-Driven Leader: Harnessing AI to Make Faster, Smarter Decisions
$17.05 (as of April 1, 2025 14:16 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Here’s a step-by-step guide to resolving this error:
- Modify Your Puppeteer Launch Configuration: In your TypeScript Puppeteer script, update the
launch
configuration to include the--disable-gpu
flag:
import puppeteer, { Browser } from 'puppeteer';
async function runPuppeteer(): Promise<void> {
const browser: Browser = await puppeteer.launch({
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/chromium-browser',
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu', // Disable GPU acceleration
],
protocolTimeout: 120000, // Optional: Increase the protocol timeout if needed
});
const page = await browser.newPage();
await page.goto('https://example.com');
// Your code here
await browser.close();
}
runPuppeteer().catch(error => {
console.error('Error running Puppeteer:', error);
});
- Rebuild and Run Your Docker Container: Make sure to rebuild your Docker image and rerun the container to apply the changes:
docker build -t my-puppeteer-app .
docker run my-puppeteer-app
- Alternative Fixes:
While the--disable-gpu
flag resolved the issue in this case, there could be other potential fixes, such as:
- Increasing the container’s CPU and memory limits.
- Ensuring all necessary dependencies for Chromium are installed.
- Using the
--no-sandbox
and--disable-setuid-sandbox
flags, which are commonly required in Docker environments.
Verification
After applying the fix, you can verify that the error has been resolved by running your Puppeteer script again. If successful, Puppeteer should no longer time out when opening a new page, and the script will proceed as expected.
Conclusion
Disabling GPU acceleration with the --disable-gpu
flag is a practical solution when running Puppeteer inside a Docker container. This fix addresses issues related to the browser’s startup and can prevent the frustrating “ProtocolError: Network.enable timed out” error. It’s a simple yet effective way to ensure that your Puppeteer scripts run smoothly in a containerized environment.
Tags/Keywords: Puppeteer, Docker, ProtocolError, Network.enable timed out, Chromium, Puppeteer in Docker, Headless Browser, Node.js, TypeScript
This TypeScript-based blog post should help others facing similar issues with Puppeteer in Docker containers.
What a lifesaver! Thank you for this amazing post.