Troubleshooting Firefox Puppeteer in Docker: DISPLAY variable missing in headless mode

I’m having trouble with Puppeteer and Firefox in a Docker setup. My container won’t run in headless mode. It keeps throwing an error about a missing DISPLAY environment variable.

Here’s what I’ve tried:

  • Set up a Dockerfile with Node.js, Firefox, and necessary libraries
  • Installed Puppeteer and skipped Chromium download
  • Set HEADLESS to true in environment variables
  • Tried to start Xvfb in the container

But nothing’s working. The error message is always the same:

Failed to launch the browser process!
Error: no DISPLAY environment variable specified

I’m out of ideas. Has anyone faced this before? What am I missing? Any tips on how to get Firefox Puppeteer running smoothly in a Docker container would be great. Thanks!

I’ve tackled this issue before. Here’s what worked for me:

  1. Use the official Firefox Docker image as a base. It’s preconfigured for headless mode.

  2. In your Dockerfile, set these environment variables:
    ENV MOZ_HEADLESS=1
    ENV MOZ_HEADLESS_WIDTH=1280
    ENV MOZ_HEADLESS_HEIGHT=1024

  3. Make sure you’re using the latest Puppeteer version compatible with Firefox.

  4. In your Puppeteer code, launch Firefox with these options:
    const browser = await firefox.launch({
    headless: true,
    args: [‘–no-sandbox’]
    });

  5. If issues persist, try running Firefox with xvfb-run:
    CMD [“/usr/bin/xvfb-run”, “-a”, “node”, “your-script.js”]

These steps should resolve the DISPLAY variable issue and get Firefox Puppeteer running smoothly in Docker. Let me know if you need more details.

hey nova, had similar issues. try adding these to ur dockerfile:

ENV DISPLAY=:99
RUN Xvfb :99 -screen 0 1024x768x16 &

worked for me. also make sure ur using latest firefox-esr. good luck!

I’ve encountered this issue before. The key is to ensure you’re using the correct Firefox binary path in your Puppeteer configuration. Try adding this to your code:

const browser = await firefox.launch({ executablePath: '/usr/bin/firefox' });

Also, double-check that you’ve installed firefox-esr in your Dockerfile:

RUN apt-get update && apt-get install -y firefox-esr

If you’re still having trouble, consider using the puppeteer-firefox package instead of regular Puppeteer. It’s specifically designed for Firefox and might handle these edge cases better. Hope this helps!