20

I need to run XVFB and docker with firefox but can't get them to work together

Here is my Dockerfile :

FROM abevoelker/ruby:latest # based on ubuntu
ENV TERM linux
RUN apt-get update && apt-get install -y .....

ENV DISPLAY :99

# Install Xvfb init script
ADD xvfb_init /etc/init.d/xvfb # default xvfb init.d
RUN chmod a+x /etc/init.d/xvfb

CMD ["firefox"]

The error message I get from Firefox is

 Error: cannot open display: :99
overlox
  • 794
  • 1
  • 5
  • 18

1 Answers1

21

I solved this by writing a startup script which will:

  1. start xvfb
  2. start firefox

Executing the script via CMD allows the proper sequence of commands to run on container startup.

Dockerfile

...
ENV DISPLAY :99

ADD run.sh /run.sh
RUN chmod a+x /run.sh

CMD /run.sh

run.sh

Xvfb :99 -screen 0 640x480x8 -nolisten tcp &
firefox
John
  • 1,322
  • 14
  • 26
overlox
  • 794
  • 1
  • 5
  • 18
  • 3
    why is putting it into its own script any different from your original Dockerfile ? – Scott Stensland Dec 08 '17 at 03:58
  • 7
    Docker has no service manager built-in, so the Xvfb startup script didn't have any effect in the initial example. By moving it to a script, which is executed when the container starts (CMD), Xvfb is correctly launched in the container. – baer Feb 08 '18 at 15:35
  • @baer could you elaborate more on that? Why is the script lunched correctly since there isn't still a service manager? – Charalamm May 13 '21 at 07:52
  • 2
    if you are getting `_XSERVTransmkdir: ERROR: euid != 0,directory /tmp/.X11-unix will not be created.` error or warning pass `-nolisten unix` to the command. `Xvfb :99 -screen 0 640x480x8 -nolisten tcp -nolisten unix` – arvin_v_s Mar 23 '22 at 05:59