Thanks so much @Lazarus535
I found that for me adding the following to my docker command worked:
--volume="$HOME/.Xauthority:/root/.Xauthority:rw"
I found this trick here
EDIT:
As Lazarus pointed out correctly you also have to set the --net=host option to make this work.

Answer from chron0x on Stack Overflow
Top answer
1 of 7
36

Thanks so much @Lazarus535
I found that for me adding the following to my docker command worked:
--volume="$HOME/.Xauthority:/root/.Xauthority:rw"
I found this trick here
EDIT:
As Lazarus pointed out correctly you also have to set the --net=host option to make this work.

2 of 7
19

Ok, here is the thing:

1) Log in to remote machine

2) Check which display was set with echo $DISPLAY

3) Run xauth list

4) Copy the line corresponding to your DISPLAY

5) Enter your docker container

6) xauth add <the line you copied>*

7) Set DISPLAY with export DISPLAY=<ip-to-host>:<no-of-display>

*so far so good right?

This was nothing new...however here is the twist: The line printed by xauth list for the login user looks something like this (in my case):

<hostname-of-machine>/unix:<no-of-display> MIT-MAGIC-COOKIE-1 <some number here>

Because i use the bridged docker setup, the X forwarding port is not listening locally, because the sshd is not running in the container. Change the line above to:

<ip-of-host>:<no-of-display> MIT-MAGIC-COOKIE-1 <some number here>

In essence: Remove the /unix part.

<ip-of-host> is the IP address where the sshd is running.

Set the DISPLAY variable as above.

So the error was that the DISPLAY name in the environment variable was not the "same" as the entry in the xauth list / .Xauthority file and the client could therefor not authenticate properly.

I switched back to an untrusted X11 forwarding setting.

The X11UseLocalhost no setting in the sshd_config file however is important, because the incomming connection will come from a "different" machine (the docker container).

🌐
Stack Overflow
stackoverflow.com › questions › 43457641 › x11-fowarding-from-windows-into-docker
ssh - X11 fowarding from Windows into Docker - Stack Overflow
Bottom line: enable x11 in sshd of whatever your docker image is running on. start docker with ssh port mapped to some port. You're done here if only accessing from local Windows machine, else add firewall exception for the port to which you ...
Discussions

X11 forwarding with docker - Containers - CERN Open Data
If you want to use the docker images to interface with the open data, you may sometimes want to use ROOT or some other program from within the container that needs X11-forwarding for the graphics to pop up. If so, you’ll want to start the container in a particular way. More on opendata-forum.cern.ch
🌐 opendata-forum.cern.ch
1
July 1, 2020
dockerfile - Run Omnet++ inside docker with x11 forwarding on windows. SSH not working - Stack Overflow
The Image uses phusions's Baseimage and sets up x11 forwarding via SSH like rogaha did in his docker-desktop image. The image works perfectly fine on a Linux Host System. But on Windows and OS X i was unable to ssh on the container from the host machine. More on stackoverflow.com
🌐 stackoverflow.com
ssh - X11 forwarding from a docker container in remote server - Unix & Linux Stack Exchange
Open up any firewall ports from ... the X11 port · Make sure the SSH server is configured to accept X11 TCP connections on a remote IP. See my question (and answer) here on StackOverflow for details of how it can be done: https://stackoverflow.com/questions/48235040/run-x11-application-in-a-docker-container-reliably-on-a-server-connected-via-ssh ... To run a GUI app in a remote docker container, you do not need X11 forwarding from the ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
November 8, 2017
X11 forwarding doesn't work when image runs in docker machine but it works fine if the image is run w/o a docker machine
Issue type: networking?? Docker ce 18.06.1-ce-win73 windows 10 I have a docker container based on debian and it works when I run it like, “docker run -ti --name devworkstation --rm --net=host -v /var/run/docker.sock:/var/run/docker.sock -e DISPLAY=$env:DISPLAY xxxx/xxxx:devworkstation” ... More on forums.docker.com
🌐 forums.docker.com
0
0
October 18, 2018
🌐
GitHub
gist.github.com › udkyo › c20935c7577c71d634f0090ef6fa8393
Basic container for X11 forwarding goodness · GitHub
Using the docker run -itd -p 2222:22 firefox to run above Docker image as the container. Using the ssh -X root@127.0.0.1 -p 2222 command on the container host and make sure SSH client enables X11 forwarding on the container host.
🌐
CERN Open Data
opendata-forum.cern.ch › cms › containers
X11 forwarding with docker - Containers - CERN Open Data
July 1, 2020 - If you want to use the docker images to interface with the open data, you may sometimes want to use ROOT or some other program from within the container that needs X11-forwarding for the graphics to pop up. If so, you’ll want to start the container in a particular way.
Top answer
1 of 3
8

It looks like you're doing all of the same stuff I'm doing EXCEPT you're sharing the .Xauthority at the time of container creation. That means if you ever ssh -X into your machine after creating the container the .Xauthority will not be valid anymore. You can't ssh -X from another terminal into the same machine and go back and use the .Xauthority, ssh -X changes the .Xauthority every time for the most recent terminal. I've only got it to work by copying the .Xauthority every time I ssh -X into my machine and try and share the screen with my container.

note: I'm sharing a device and a machine id because I was forwarding a webcam output

1.Create the container and tell xhost to allow forwarding from container id:

sudo docker run -it -d \
    --net=host \
    --env="DISPLAY" \
    --env="QT_X11_NO_MITSHM=1" \
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    --device="/dev/video0:/dev/video0" \
    --volume="/path/to/your/sharedDockerFiles:/root/sharedDockerFiles" \
    --volume="/etc/machine-id:/etc/machine-id" \
    yourdockerrepo/image:tag \
    bash
export containerId=$(docker ps -l -q)
sudo xhost +local:`sudo docker inspect --format='{{ .Config.Hostname }}' $containerId`
sudo docker start $containerId

2.Copy .Xauthority from host home to sharedDockerFiles directory:

sudo cp ~/.Xauthority /path/to/your/sharedDockerFiles

3.Start and attach your container

4.Copy the .Xauthority in your shared folder to your container home

sudo cp /root/sharedDockerFiles/.Xauthority ~/

5.(necessary once): Edit container's /etc/ssh/ssh_config under Host * to include:

   ForwardX11 yes
   X11Forwarding yes

6.Restart your container and reattach and run GUI app

7.If you still have problems, make sure $DISPLAY variable in the container is the same as the host's

echo $DISPLAY #do this in the container
exit
echo $DISPLAY #do this in the host, should be the same as container's
#if they aren't equal, start container and:
export DISPLAY= #put the output of your host's $DISPLAY variable here
2 of 3
1

Copy .Xauthority in container at the beginning of ssh session before using GUI:

sudo docker exec -i container_name bash -c 'cat > ~/.Xauthority' < ~/.Xauthority

Then, you can pass DISPLAY if using 'docker exec'. E.g. to open new bash:

sudo docker exec -it --env="DISPLAY" container_name bash

2 more possible causes for error: (in addition to accepted answer)

  • basic: You don't have ssh server or xauth in container (for ubuntu run 'apt install openssh-server xauth')

  • sneaky one: If your container hostname is different than the one from host (e.g. set via -h badge in 'docker run') you'll get the error and you have to deal with that (e.g. set the same hostname or add cookie to xauth)

🌐
Nicojane
nicojane.github.io › X11-GUI-Dev-Template-Stack › Howtos › howto_x11_wsl_in_Docker.html
Docker Template Stack X11-GUI | A DTS container for Desktop GUI Projects</span>
To enable X11 forwarding to Windows, the following is required: Docker: Docker Desktop must run using the WSL 2 backend (Enable via: Settings → General → Use the WSL 2 based engine). Host: XLaunch must be running on the Windows host to receive and display X11 output.
Find elsewhere
🌐
Docker Community
forums.docker.com › docker desktop
X11 forwarding doesn't work when image runs in docker machine but it works fine if the image is run w/o a docker machine - Docker Desktop - Docker Community Forums
October 18, 2018 - Issue type: networking?? Docker ce 18.06.1-ce-win73 windows 10 I have a docker container based on debian and it works when I run it like, “docker run -ti --name devworkstation --rm --net=host -v /var/run/docker.sock:/var/run/docker.sock -e DISPLAY=$env:DISPLAY xxxx/xxxx:devworkstation” ...
🌐
DZone
dzone.com › software design and architecture › cloud architecture › docker x11 client via ssh
Docker X11 Client Via SSH
November 19, 2015 - However, in this case the X server was on the other side of an SSH tunnel from the machine running the Docker container. With SSH X11 forwarding, instead of a UNIX domain socket, clients communicate with a TCP/IP socket on port 6000+(display number).
🌐
Docker Community
forums.docker.com › docker desktop
X11 forwarding using docker desktop for linux - Docker Desktop - Docker Community Forums
August 15, 2023 - I am attempting to run a docker container in docker desktop for linux that requires access to my host machine’s display. The docker image that the container is running on is based on the PX4 flight controller and runs a …
🌐
Docker Community
forums.docker.com › docker desktop
X11 forwarding issues with release 10 - Docker Desktop - Docker Community Forums
May 7, 2016 - Expected behavior Installed and running XQuartz, was working in prior release (beta 8) used to export the bridge device IP for X forward display $export XDISPLAY=$(ifconfig bridge100 | grep "inet " | cut -d " " -f2):0 now no longer a bridge100, switched to en0 $export XDISPLAY=$(ifconfig en0 | grep "inet " | cut -d " " -f2):0 $ docker run -d --name firefox -e DISPLAY=$XDISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix jess/firefox 77ec618eebc7a73a9209bd39ef62a54443879553f2d3326dcf0cbebac8cb2f78 $...
🌐
Medium
medium.com › @potatowagon › how-to-use-gui-apps-in-linux-docker-container-from-windows-host-485d3e1c64a3
How to use GUI apps in linux docker container from Windows Host | by potatowagon | Medium
April 12, 2020 - The format of the display variable is [host]:<display>[.screen] . When X11 forwarding over SSH, the TCP port number to open is 6000 + <display> .
🌐
Reddit
reddit.com › r/docker › x app running in container - forward x window via ssh?
r/docker on Reddit: X app running in container - forward x window via SSH?
September 6, 2023 -

Hi all

Due to company policy I have a requirement to run a legacy app inside a container, with X window support, where the X window can be run over an SSH connection with forwarding...

I found that if the dockerfile switches to the username that will run the container (provided as a build arg), and the following is added to the run command, I can get X working quite well locally.

-v /tmp/.X11-unix/:/tmp/.X11-unix -e DISPLAY=$DISPLAY -h $HOSTNAME -v $HOME/.Xauthority --ipc=host

Im not an X11 expert by any means, so im unsure how to make the SSH forwarding aspect of this work. If I try to SSH to the host with -XY and launch the container, I get this error:

Error: Can't open display: localhost:10.0

Any pointers?

🌐
Medium
medium.com › @mreichelt › how-to-show-x11-windows-within-docker-on-mac-50759f4b65cb
How to show X11 windows with Docker on Mac | by Marc Reichelt | Medium
November 7, 2018 - Ensure localhost is allowed X11 forwarding: xhost + 127.0.0.1 · Inside the docker image, install pygame and set the display variable: pip install pygame export DISPLAY=host.docker.internal:0
🌐
NVIDIA Developer Forums
forums.developer.nvidia.com › robotics & edge computing › jetson systems › jetson orin nano
X11 Forwarding with with Docker containers - Jetson Orin Nano - NVIDIA Developer Forums
March 25, 2025 - I got PuTTY working with Xming on my Windows machine to run GUI applications over SSH from my Jetson Orin Nano. Things are running smoothly outside of Docker — I can launch xeyes, xclock, etc. just fine. However, I’m hi…
🌐
Medium
medium.com › @paliwalsamriddhi › gui-apps-within-a-docker-container-971681838fda
GUI apps within a Docker container | by samriddhipaliwal | Medium
September 9, 2023 - You’ll need an X11 server on your host machine to display the GUI from the container. Xming is a popular choice for Windows, while XQuartz is commonly used on macOS. On Linux, you may already have X11 installed.