As the title says, I'm having trouble running Firefox in headless mode inside a Docker container as a non-root user. Consider the following Dockerfile, built with docker build -t firefox .
FROM python:3.8-buster
RUN apt-get update -qq \
&& apt-get install -qy \
libappindicator1 \
libasound2 \
libatk1.0-0 \
libc6 \
libcairo2 \
libcups2 \
libdbus-1-3 \
libexpat1 \
libfontconfig1 \
libgbm-dev \
libgcc1 \
libgconf-2-4 \
libgdk-pixbuf2.0-0 \
libglib2.0-0 \
libgtk-3-0 \
libnspr4 \
libnss3 \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libpci-dev \
libstdc++6 \
libx11-6 \
libx11-xcb1 \
libxcb1 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxi6 \
libxrandr2 \
libxrender1 \
libxss1 \
libxtst6 \
xdg-utils \
nano
RUN wget https://download-installer.cdn.mozilla.net/pub/firefox/releases/85.0.2/linux-x86_64/en-US/firefox-85.0.2.tar.bz2 -O /firefox.tar.bz2
RUN tar -xf /firefox.tar.bz2 --directory /
WORKDIR /firefox
RUN ./firefox -CreateProfile "headless /profile-headless" -headless
RUN chmod -Rf 777 /firefox && chmod -Rf 777 /profile-headless
cmd ["./firefox", "-profile", "/profile-headless", "-headless", "--screenshot", "https://example.org"]
If I run a container as root, all is good and the process finishes (a few warnings appear, but it works overall):
$ docker run --rm firefox
*** You are running in headless mode.
[GFX1-]: glxtest: Unable to open a connection to the X server
[GFX1-]: glxtest: libEGL missing
$
However, if I run it as a different user, the same output appears, but the process hangs.
$ docker run --rm --user=1001 firefox
*** You are running in headless mode.
[GFX1-]: glxtest: Unable to open a connection to the X server
[GFX1-]: glxtest: libEGL missing
I tried assigning 777 permissions to both the directory that holds the binaries (/firefox
) and the profile one (profile-headless
), that doesn't seem to work.
Probably some dependencies are not necessary, I just didn't want to spend time on that while I have bigger issues.
As a note, I initially encountered this while trying to run playwright-python
inside Docker as non-root. The Chromium browser works just fine, but Firefox fails to initialize and playwright
ends up throwing a timeout error. I dug deeper and realized that standalone Firefox was failing for me as well.
I suppose I must be missing some configuration, env variables or such. Any help would be much appreciated, thanks in advance!