pandita
pandita

Reputation: 4979

GUI application via Docker - X11 - "Unable to init server"

I'm trying to run Firefox in a Debian docker image but can't connect to the X11 server.

I'm using the method described here, but changed the base image to the latest Debian. I also changed the user creation method.

Dockerfile

FROM debian:latest                                                                 
RUN apt-get update && apt-get install -y firefox-esr
RUN useradd --shell /bin/bash --create-home developer && \
    usermod -aG sudo developer
  
USER developer
ENV HOME /home/developer
CMD /usr/bin/firefox

Building the container

docker build -t firefox .

Command to start the container

docker run -ti --rm \
   -e DISPLAY=$DISPLAY \
   -v /tmp/.X11-unix:/tmp/.X11-unix \
   firefox

ERROR

Unable to init server: Could not connect: Connection refused
Error: cannot open display: :0

Operating system

OpenSUSE Leap 15.2

Context

I'm doing the above to understand how to run a GUI app via docker. The aim is to run the latest version of FreeCAD (v19), which is currently broken on OpenSUSE.

Upvotes: 1

Views: 6155

Answers (1)

Akshay Shah
Akshay Shah

Reputation: 734

docker run --rm \
--net=host \
--env="DISPLAY" \
--volume="$HOME/.Xauthority:/home/developer/.Xauthority:rw" \
firefox

This should work with your Dockerfile!

Couple of points

  • .Xauthority file also needs to be shared as it holds the cookies and auth sessions for the X server. Hence it has to be read/write too.
  • If you dont want to do --net=host then you can listen on a TCP port bound to unix socket and forward that to the container.

Upvotes: 3

Related Questions