I have a use-case where I need to run a GUI application from Docker and display it on either a Windows or a Linux host. (In this example I try it with firefox)
For Windows, it works as intended using vcxsrv. However, no matter what I try on Linux (Pop!_OS 22.04 LTS
specifically), I always get the following error:
Unable to init server: Could not connect: Connection refused
Error: cannot open display: <my_display>
Here is a list of the things I've tried so far to no avail
- Adding the
/tmp
to the mount path in Docker Desktop - mentioned here - Allowing global access to XServer
xhost +
and other variations - Setting
$DISPLAY
to various values (':0', '<my-ip>:1', '<my-ip>:0', etc...) - mentioned here - Setting
privileged: true
andnetwork_mode: host
in compose - Installing
xauth
inside the container as shown ininit-script.sh
andDockerfile
below - mentioned here - Creating and mounting the file
~/.Xauthority
since I don't have it on my system - Adding a
--runtime=runc
to the run command - mentioned here
Output of uname -a
Linux pop-os 5.19.0-76051900-generic #202207312230~1663791054~22.04~28340d4 SMP PREEMPT_DYNAMIC Wed S x86_64 x86_64 x86_64 GNU/Linux
Content of ls -l /tmp/.X11-unix
srwxrwxrwx 1 sina sina 0 Oct 18 13:48 X1
My file tree is as follows:
├── docker-compose.yml
├── Dockerfile
└── init-script.sh
The files and their content:
docker-compose.yml
version: '3.8'
services:
firefox:
build: .
image: firefox:latest
container_name: firefox-in-docker
environment:
- DISPLAY=${DISPLAY} # 'host.docker.internal:0.0' is what works for windows if you're curious
volumes:
- /tmp/.X11-unix:/tmp/.X11-unix
Dockerfile
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y firefox
# RUN apt-get update && apt-get install -y xauth
COPY init-script.sh /opt/program/init-script.sh
CMD ["/bin/bash", "/opt/program/init-script.sh"]
init-script.sh
#!/bin/bash
# HOST=root
# DISPLAY_NUMBER=$(echo $DISPLAY | cut -d. -f1 | cut -d: -f2)
# AUTH_COOKIE=$(xauth list | grep "^$(hostname)/unix:${DISPLAY_NUMBER} " | awk '{print $3}')
# xauth add ${HOST}/unix:${DISPLAY_NUMBER} MIT-MAGIC-COOKIE-1 ${AUTH_COOKIE}
/usr/bin/firefox
Of course, I also don't forget to re-build and run the image every time I make a change to the Dockerfile
with docker compose build && docker compose up
.
There is one thing I didn't try, which is running a VNC Server (scroll down close to the end) but I really wanted to know what's wrong with XServer.