0

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 and network_mode: host in compose
  • Installing xauth inside the container as shown in init-script.sh and Dockerfile 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.

usersina
  • 1,063
  • 11
  • 28
  • Use `ssh -Y` to connect to the containerized application from a X11 server (desktop); the `-Y` option will set `DISPLAY` for you and forward the X11 calls from the client (server) to the desktop (client). X11 uses server and client in the opposite sense of modern applications. – Elliott Frisch Oct 18 '22 at 13:51
  • I keep getting timeouts with `ssh -Y` to the container ip. Is there a way to pass it somehow when doing a `docker exec`? – usersina Oct 18 '22 at 14:17
  • No. Because the `ssh -Y` is being run from the host (not the container). Make sure you have ssh installed on the container. And [make sure X11 forwarding is enabled](https://unix.stackexchange.com/a/12772/52050). – Elliott Frisch Oct 18 '22 at 15:03
  • @AnisBenna hey, hope this will help https://stackoverflow.com/a/75271849/3153739 – KravAn Jan 30 '23 at 09:51

0 Answers0